Why is my plot not showing for improved Euler method?

1 view (last 30 days)
I am currently learning about the improved Euler formula and so I am testing it with the DE shown below. However, I am trying to obtain the graph for it but it does not show up when I try to plot it. I am new to MATLAB so I would appreciate if someone could explain why this is happening and how I could fix it. This is what I'm working with:
dy=@(t,y) -(0.1/10^(1/2))^2*(2*9.8*y)^(1/2); %ODE
t=0; %initial t
y=2*sqrt(10); %Initial y
h=0.05; %Half step
a=1136; %approximate value
fprintf('\t(Euler Improved)\n');
fprintf('t \t\t y\n');
while a-t >= -10^(-5) %loop until given approximate value
dty = dy(t,y);
tn = t+h; %new t value
yp = y + h*dty; %placeholder y value
dtnyp = dy(tn,yp);
yn = y + (h/2)*(dty + dtnyp); %new y value
fprintf('%0.2f\t %f\n',t ,y)
t = tn; %update t value
y = yn; %update y value
end
plot(t, y, 'b')
grid

Accepted Answer

Sarvesh Kale
Sarvesh Kale on 6 Mar 2023
You are updating the time t and y values, they are of size 1x1 so the final plot will only show you a point, you have to append to the time vector and y vector and then use it to plot, here is your own modified code, I have defined two empty arrays t1 and y1 and then appending to them inside the while loop.
dy=@(t,y) -(0.1/10^(1/2))^2*(2*9.8*y)^(1/2); %ODE
t=0; %initial t
y=2*sqrt(10); %Initial y
h=0.05; %Half step
a=1136; %approximate value
t1=[];y1=[]; %%%% added line %%%%%%%
while a-t >= -10^(-5) %loop until given approximate value
dty = dy(t,y);
tn = t+h; %new t value
yp = y + h*dty; %placeholder y value
dtnyp = dy(tn,yp);
yn = y + (h/2)*(dty + dtnyp); %new y value
t = tn; %update t value
y = yn; %update y value
t1=[t1,t]; %%% append to time vector
y1=[y1,y]; %%% append to ouptput y vector
end
figure ;
grid on ;
title ('Solution using Euler Imrpoved method')
plot(t1, y1, 'b','lineWidth',1.5) %% the vectors accumulated are used in plotting
I hope this answers your query, please accept the answer if it does
Thank you

More Answers (2)

Alan Stevens
Alan Stevens on 6 Mar 2023
You need to keep track of each step. Your original simply overwrites the values of t and y at each step.
dy=@(t,y) -(0.1/10^(1/2))^2*(2*9.8*y)^(1/2); %ODE
t(1)=0; %initial t
y(1)=2*sqrt(10); %Initial y
h=0.05; %Half step
a=1136; %approximate value ... of time???
% fprintf('\t(Euler Improved)\n');
% fprintf('t \t\t y\n');
step = 0;
err = 1;
while err >= 10^(-5) %loop until given approximate value
step = step+1;
dty = dy(t(step),y(step));
tn = t(step)+h; %new t value
yp = y(step) + h*dty; %placeholder y value
dtnyp = dy(tn,yp);
yn = y(step) + (h/2)*(dty + dtnyp); %new y value
% fprintf('%0.2f\t %f\n',t ,y)
t(step+1) = tn; %update t value
y(step+1) = yn; %update y value
err = abs(a-t(step+1));
end
plot(t, y, 'b')
grid
xlabel('t'), ylabel('y')

FERNANDO CALVO RODRIGUEZ
FERNANDO CALVO RODRIGUEZ on 6 Mar 2023
Good morning Anthony,
From what I have seen in your code, the result you finally get for t and y is a single value because t and y are variables and not vectors.
Try designating them as zeros vectors with the necessary length:
t = zeros(N,1) % Create a 0's vector with N rows and 1 cols.
y = ones(N,1) % Create a 1's vector with N rows and 1 cols.
To multiply or divide a number between each position of the vector you must use ".*", "./", ".^".
I have given some numerical methods and it is more advisable to use a for loop followed by the while loop as it facilitates the indexing of the vectors.
If you want to plot each iteration, I still recommend you to use the for loop and save each iteration in a vector to plot it later.
Sorry for not being able to help you more.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!