Improved Eulers method help

5 views (last 30 days)
androSLO
androSLO on 8 Dec 2016
Commented: Image Analyst on 14 Dec 2016
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.

Answers (1)

Image Analyst
Image Analyst on 9 Dec 2016
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 Comments
androSLO
androSLO on 13 Dec 2016
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 on 14 Dec 2016
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

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!