MATLAB Answers


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

Asked by Vincenzo Dragone on 31 Jul 2019
Latest activity Edited by Sai Bhargav Avula on 5 Aug 2019
%% 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
% 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 Comment

This question is a duplicate and was addressed here

Sign in to comment.

1 Answer

Answer by Sai Bhargav Avula on 5 Aug 2019
Edited by Sai Bhargav Avula on 5 Aug 2019

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; 
Hope this helps !!


Sign in to comment.