solving a function with indecis

조회 수: 2 (최근 30일)
RSHU FA
RSHU FA 2018년 4월 23일
편집: RSHU FA 2018년 4월 23일
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
Ameer Hamza
Ameer Hamza 2018년 4월 23일

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 2018년 4월 23일
편집: RSHU FA 2018년 4월 23일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2018년 4월 23일
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
Torsten
Torsten 2018년 4월 23일

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 2018년 4월 23일
편집: RSHU FA 2018년 4월 23일

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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by