How to Solve equation using Eulers method in Matlab?
9 views (last 30 days)
Show older comments
Samson David Puthenpeedika
on 14 Nov 2021
Commented: Alan Stevens
on 14 Nov 2021
Question is as follows:-
Solve the following initial value problem over the interval from t = 0 to 1 where y(0) = 1.
dy/dt = yt^2 - 1.1y
• (a) analytically (showing the intermediate steps in the comments),
• (b) using the explicit Euler’s method with h = 0:5,
• (c) using the explicit Euler’s method with h = 0:25
Note: The Symbolic Math Toolbox should NOT be used.
Below is my code . I wanted to know my mistake if any.
%for h=0.5
h=0.5;
t=0:h:1;
y=zeros(size(t));
y(1)=1;
n=numel(y);
for i = 1:n-1
dydt= (y(i)*t(i).^2)-(1.1*y(i))
y(i+1) = y(i)+(dydt*h)
disp(y(i));
end
%for h=0.25
h1=0.25;
t1=0:h1:1;
y1=zeros(size(t1));
y1(1)=1;
n1=numel(y1)
for i = 1:n1-1
dydt1= (y1(i)*t1(i).^2)-(1.1*y1(i))
y1(i+1)=y1(i)+(dydt1*h1)
disp(y1(i));
end
Display in a plot
• the analytical result (a) as a black solid line, and
• the numerical results (b - c) as solid lines with point markers in different color
• with the corresponding labels of the axes and legend.
% analytical sol(plz check for this too)
T=[0 1]
Y=[1 -0.766]
% Graph
plot(t,y,'r',"DisplayName","h=0.5");
hold on
plot(t1,y1,'b',"DisplayName","h=0.25");
plot(T,Y,'k',"DisplayName","Anlaytical sol");
legend
grid on;
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox 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!