Improved Eulers method help

조회 수: 5 (최근 30일)
androSLO
androSLO 2016년 12월 8일
댓글: Image Analyst 2016년 12월 14일
Hello,
I am beginner at matlab and i want to do myself eulers methods but it's stopped at this one. Cause i want data in a row. In first Eulers method results are correct and everything shows like it should. Can somebody correct me cause i cannot find solution to it?
First Eulers method program is this:
h=0.1; x=0:h:0.5; y=1; n=5; for i=1:n
f =(x(i)).^3-(5*(x(i)).^2)+7;
y(i+1)=y(i)+h*f
end
disp(y(i))
And this is where i can't get all data in row and correct the first result.
h=0.1;
x=0:h:0.5;
y=1;
n=1;
for i=0:1:n
for i=0:1:n
j=((i+(i+1))/2);
end
f =(x).^3-(5*(x).^2)+7;
y=y+(h*f);
end
disp(y(i))
It shows me just the result of y(n)..
Thank you for help in advance.

답변 (1개)

Image Analyst
Image Analyst 2016년 12월 9일
If you want to store the y from every iteration, do this:
y(i) = y(i-1) + (h*f);
Instead of having
disp(y(i))
just put y
y
  댓글 수: 2
androSLO
androSLO 2016년 12월 13일
Yes i have figured it out 2 hours later when i posted question.
Here it is working now:
h=0.1; x=0:h:0.5; y=1; n=1:1:6; for i=1:length(n)-1;
for ii=1:(length(n)-1)/2;
f(ii) =(x(ii)).^3-(5*(x(ii)).^2)+7;
y(i+1)=y(i)+h*f(ii);
end
end
disp(y)
Image Analyst
Image Analyst 2016년 12월 14일
It's recommended not to use i (the imaginary variable) as a loop index.
Also, to format your code, you can see how to do it here: http://www.mathworks.com/matlabcentral/answers/13205#answer_18099

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by