Please solve using MATLAB. Will rate. Problem 7: Numerical integration of data p
ID: 3698423 • Letter: P
Question
Please solve using MATLAB. Will rate.
Explanation / Answer
SAVE THE FOLLOWING CODE IN AMTLAB AND GET THE RESULTS.
clc
clear
close all;
%% Part (A)
t=[0 1 2 3 4 5 6];
speed=[0 18 41 63 83 99];
format long
eps_step=1e-5;
N=20;
a=0;
b=10;
h=b-a;
ctrap=0.5*(cos(a)+cos(b))*h;
for i=1:N
h=h/2;
ctrapn=0.5*(cos(a)+2*sum(cos((a+h):h:(b-h)))+cos(b))*h;
if abs(ctrap-ctrapn)<eps_step
break;
elseif i==N
error('The composite trapezoidal rule did not converge');
end
ctrap=ctrapn;
end
dist=abs(ctrap*5);
disp('The Distance of the Car travelled during the first six seconds using first method is')
disp(dist)
%% Part (B)
f=@(x)x^4; %Change here for different function
a=-3;b=3; %Given limits
n=b-a; %Number of intervals
h=(b-a)/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
y(p)=i^4; %Change here for different function
end
disp('The distance of the Car travelled during the first six seconds using second method is')
disp(p-4)
%% Part (C)
f=@(x)1/(1+x); %Change here for different function
a=0;b=6; %Given limits
n=b-a; %Number of intervals
h=(b-a)/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
y(p)=1/(1+i); %Change here for different function
end
disp('The distance of the Car travelled during the first six seconds using third method is')
disp(p-3)