Improved Eulers method help

5 Ansichten (letzte 30 Tage)
androSLO
androSLO am 8 Dez. 2016
Kommentiert: Image Analyst am 14 Dez. 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.

Antworten (1)

Image Analyst
Image Analyst am 9 Dez. 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 Kommentare
androSLO
androSLO am 13 Dez. 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 am 14 Dez. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by