In: Advanced Math
% We will solve this problem by Euler's Method
clc;
clear all;
c=1;
h = 0.1; % initial step size
for j=1:2
t = 0:h:5; % the range of t
y = zeros(size(t)); % allocate the result y
y(1) = 1; % the initial y value
n = numel(y); % the number of y values
% The loop to solve the ODE
for i=1:n-1
f = 4-t(i)+2*y(i); % The given function
y(i+1) = y(i) + h * f; % formula for Euler's method.
end
fprintf('The solutions with step size %f\n',h)
fprintf('t \t y(t)\n')
fprintf('------------------------------\n')
a=0;
b=1;
for i=1:6
fprintf('%.0f \t %f\n',a,y(b))
a=a+1;
b=b+(c*10);
end
h=h*0.1;
c=10;
end