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

19 Ansichten (letzte 30 Tage)
%% 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.

Antworten (1)

Sai Bhargav Avula
Sai Bhargav Avula am 5 Aug. 2019
Bearbeitet: Sai Bhargav Avula am 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; 
end 
plot(xout,yout,'Color',pre_color(i)); 
end 
Hope this helps !!

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by