How would I plot this using step sizes (h values) of 0.5, 0.1, and 0.05?

18 次查看(过去 30 天)
%% Euler's Method
%Pick a single initial condition from Task 1
%The initial condition we will use is y(0)=1
%Use Euler's method to approximate the solution to this IVP over some t
%range which you choose
%Create a figure called Task 2
figure ('Name','Task 2')
%The range of t is from 0 to 1
%We are using the initial condition that y(0)=1, but we want to know y(t)
%when t=1
%In other words we want to find y(1)
y = y0; % y0 is the initial condition
yout = y;
t0 = 0; % initial value of t
h = 0.5; % step size
tfinal = 1; % final value of t
for t = t0 : h : tfinal-h %range of t from 0 to 1 with a step size of 0.5
y = ode*h + y; % so like in our handwritten calculations...
% y is the value of y
% h is the step size or also referred to as delta x
% we want to find y(t) where t = 1 so in other words we
% are finding y(1)
yout = [yout;y] % y output
end
% Approximate using Euler's Method with h = 0.5, 0.1, and 0.05
% The approximations should be plotted using discrete points. Each should be
% a different color. Create a legend which indicates the h value.

回答(1 个)

Sai Bhargav Avula
编辑:Sai Bhargav Avula 2019-8-5
You can use nested for loop for plotting. For this case  
h= [0.5,0.1,0.05]; 
pre_color =['g','b','r'];
hold on; 
for i = 1:length(h) 
for t = t0:h:tfinal-h 
Your way to calculate the xout and yout; 
end 
plot(xout,yout,'Color',pre_color(i)); 
end 
Hope this helps !!

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by