solving a function with indecis

I want to compute a function f, for example, f=sin(t*x) with this recursive algorithm.
h=.01;y0=1;t0=0;
for i=0:10
t(i+1)=ti+h;
c1=func(ti, yi);
c2=func(ti+(h/2),yi+(h/2)*c1);
y(i+1)=yi+h/6*(c1+2*c2);
end
And I defined function func in the separate file. This loop doesn't run from the first line. I don't know what is the problem. First I want to know this way of writing indices is correct? second, how can I get the value of y at the end? Could you please help me with this? Thank you

2 Commenti

what are yi, c1 and c2. Are c1 and and c2 even defined. Also, do you mean y(i) instead of yi.

RSHU FA
RSHU FA il 23 Apr 2018
Modificato: RSHU FA il 23 Apr 2018
Thank you for your comment. You mean I have to write y_(i+1)as yi+1? without parenthesis? These indices don't produce during the for loop?

Accedi per commentare.

 Risposta accettata

Torsten
Torsten il 23 Apr 2018

0 voti

All variables must be defined before you use them.
Look at the first line of your loop: here, yi, c1 and c2 are not yet defined to calculate y(2).
Best wishes
Torsten.

12 Commenti

RSHU FA
RSHU FA il 23 Apr 2018
Thank you for your answer. These indices don't produce during the for loop? I started i=0 instead 1.
Torsten
Torsten il 23 Apr 2018
yi remains yi - i=0 is not substituted here.
Furthermore, c1 and c2 are not yet defined when you try to evaluate y(i+1).
RSHU FA
RSHU FA il 23 Apr 2018
Excuse me I don't get it! I have y0 and t0. then for c1=func(t0,y0)for first time and again second line,... Besides, defining index in this way is correct? t(i+1) or ti+1?
h=.01;
t(1)=0;
y(1)=1;
for i=1:10
   c1=func(t(i),y(i));
   c2=func(t(i) + h/2,y(i) + h/2*c1);
   y(i+1) = y(i) + h/6*(c1+2*c2);
   t(i+1) = t(i) + h;
end
RSHU FA
RSHU FA il 23 Apr 2018
Modificato: RSHU FA il 23 Apr 2018
Thank for your help, I really appreciate that. Now to see final y, I have to use which one? fprint, display,...? All these get me an error!
Torsten
Torsten il 23 Apr 2018
Modificato: Torsten il 23 Apr 2018
plot(t,y) 

after the loop.

I got:

   Error using plot
Vectors must be the same length.
Torsten
Torsten il 23 Apr 2018
Please show your code.
t(1)=0;
h=.1;
y(1)=3;
for i=1:100
t(i+1)=t(i) + h;
 c1=functionq1(t(i),y(i));
 c2=functionq1(t(i)+h/2,y(i) +h/2*c1);
 c3=functionq1(t(i)+h/2,y(i) +h/2*c2);
 c4=functionq1(t(i) +h, y(i) +h*c3);
     y(i+1)=y(i) + h/6*(c1+2*c2+2*c3+c4);
  end
  tspan=0:0.1:10;
  y0=3;
  [X,Y]=ode45(@funcq1, tspan, y0);
  plot(X,Y,t,y);
RSHU FA
RSHU FA il 23 Apr 2018
Thank you a lot.I couldn't even thank you. The problem was only because of i should be from 1 to 100!interestin!
Now how can I compare the maximum difference between this two? with max(Y-y) I don't get the right result

with max(Y-y) I don't get the right result

Why ? What's the problem with

max(abs(Y(:,1)-y(:))) 

?

RSHU FA
RSHU FA il 23 Apr 2018
Modificato: RSHU FA il 23 Apr 2018

with max(Y-y) I get two many numbers like:

    Columns 1 through 19
   56.1546   56.0333   55.8888   55.7191   55.5210   55.2904   ..   
Columns 20 through 38
   43.7418   42.1609   40.5228   38.7424   36.9352   35.2361   ..   

with max(abs(Y(:,1)-y(:))) I get one number, it's OK. But w.r.t the figure I think the bigger difference happens in the maximum of this function but max(Y)-max(y) is not equal to max(Y-y). how can I get the t component for max(y) and max(Y)? Maybe because y is w.r.t t and Y is w.r.t X.

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 23 Apr 2018

Modificato:

il 23 Apr 2018

Community Treasure Hunt

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

Start Hunting!

Translated by