In: Advanced Math
Numerical Analysis:
Make a matlab code that computes the Midpoint rule/method for a
given function f'(t,y) = y' = t + y from 0 < t < 4
(inclusive) with h=0.5 and with initial condition y(0) = 0.
Please make output display in tabular form and not
in a plot, that doesn't help show the actual values.
clc;
clear all;
f=@(t,y) t+y;% given function
h=0.5; %step length
a=0; % starting point
b=4; %ending point
N=(b-a)/h; % number of intervals
t=a:h:b;
y_mid(1)=0; %initial point
% Midpoint formula
for i=1:N
k(i)=y_mid(i)+(h/2)*f(t(i),y_mid(i));
y_mid(i+1)=y_mid(i)+h*f(t(i)+h/2,k(i));
end
disp('------------------------------')
disp( 'tn y_Midpoint ')
disp('-----------------------------')
for i=1:N+1
fprintf(' %f \t %15f \n',t(i),y_mid(i))
end
--------------------------------------
tn y_Midpoint
-------------------------------------
0.000000 0.000000
0.500000 0.125000
1.000000 0.640625
1.500000 1.791016
2.000000 3.972900
2.500000 7.830963
3.000000 14.412815
3.500000 25.420825
4.000000 43.621340