I get "index must be a positive integer or logical. " Error, please help

1 vue (au cours des 30 derniers jours)
hgrlk
hgrlk le 27 Mai 2019
Commenté : dpb le 28 Mai 2019
Hello, I write a code that use third-order Runge Kutta method. I want my result in tabulated form which i starting with 0, ti, yi (represents numerically estimated value), yei(true value) and et ( true percent absolute relative error).
Finally, I want to plot the true solution together with the numerical solution in the same place.
I have dy/dt = t*e^(3*t)-2*y , step size 0.1 , the true solution ye(t) = 1/5*t*e^(3*t)-(1/25)*e^(3*t)+(1/25)*e^(-2*t)
I write a code but when I run that, I get
i ti yi yei |Et|
Attempted to access t(0); index must be a positive integer or logical.
Error in hw5 (line 22)
k1(i)=f(t(i),y(i));
>>
MY i HAS TO BE START WITH 0.
HOW CAN I SOLVE THAT? THANKS
clc
clear all
close all
fprintf('%10s %10s %10s %10s %10s\n','i','ti','yi','yei','|Et|')
h=0.1; %initial step size
t=0:h:1; %time array with initial step size
c=length(t); %number of time values with 0.1 step size
y=zeros(1,c); %creating an empty array for numerically calculated 'y' values
%y(0)=0 in this definition. so, initial value is already included.
y_2=zeros(1,c); %creating an empty array for numerically calculated 'y' values
y_true=zeros(1,c); %creating an empty array for exact 'y' values
E=zeros(1,c); %creating an empty array for estimating local truncation errors
%defining the ODE, f = dy/dt
f = @(t,y) t*exp(3*t) - 2*y;
%% Part a
klmn=zeros(1,c-1);
for i=0:1:(c-1)
%defining k1,k2 and k3 for each case
k1(i)=f(t(i),y(i));
k2(i)=f((t(i)+h/2),(y(i)+h/2*k1(i)));
k3(i)=f((t(i)+h/2),(y(i)+h/2*k2(i)));
%defining next value of dy/dt, y(i+1) numerically
y(i+1)=y(i)+(h/6)*(k1(i)+2*k2(i)+2*k3(i));
%real value of y(i+1)
y_true(i+1)=1/5*t(i+1)*exp(3*t(i+1))-(1/25)*exp(3*t(i+1))+(1/25)*exp(-2*t(i+1));
%true error calculation
et(i+1)=abs(y(i+1)-y_true(i+1))./y_true(i+1);
fprintf('%10s %10s %10s %10s %10s\n',i,t,y,y_true,et)
end

Réponses (1)

dpb
dpb le 27 Mai 2019
Modifié(e) : dpb le 27 Mai 2019
"MATLAB is not C"
...
for i=0:1:(c-1)
%defining k1,k2 and k3 for each case
k1(i)=f(t(i),y(i));
...
Matlab arrays are one-based, not 0-based. Write
for i=1:c
%defining k1,k2 and k3 for each case
k1(i)=f(t(i),y(i));
...
instead (and fix up any other references that assume 0-based arrays as well, of course).
  2 commentaires
hgrlk
hgrlk le 28 Mai 2019
When i start from 1, my output looks wrong. I think my code doesn't print the i value.
And I change fprintf('%10s %10s %10s %10s %10s\n',i,t,y,y_true,et) to fprintf('%15s %15s %15s %15s %15s\n',i,t(i),y(i),y_true(i),et(i)).
But my output looks wrong. Where is my problem?
dpb
dpb le 28 Mai 2019
The first fprintf is before any variables are defined.
You still have an indexing issue when i==c, i+1 will be outside array boundaries.
Other than that, nothing terribly obvious in "just looking"; use the debugger and step through your code looking for logical issues...

Connectez-vous pour commenter.

Produits


Version

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by