How would I plot this using step sizes (h values) of 0.5, 0.1, and 0.05?
    8 次查看(过去 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
    
 2019-8-5
        
      编辑: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 !!
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!