Please use MATLAB to integrate fxcosx from 0 to pi2 using th
Please use MATLAB to integrate f(x)=cos(x) from 0 to pi/2 using the TRAPEZOID RULE. Repeat the computation for 3 different numbers of panels: N=20, 30, 50. Then, show the errors between the results from the numerical calculation and the precise value of the integral.
Solution
%Matlab Code here
clc;
clear all;
close all;
f=@(x)cos(x); %Change here for different function
a=0;b=pi/2; %Given limits
n=30; %Number of intervals
h=(b-a)/n;
p=0;
for i=a:h:b
p=p+1;
x(p)=i;
y(p)=cos(i); %Change here for different function
end
l=length(x);
x
y
answer=(h/2)*((y(1)+y(l))+2*(sum(y)-y(1)-y(l)))
--------------------------------------------------------------------------------------------------
change Number of intervals \'n\' value to 20,30 and 50
Theoretical or precise value is integration of cos(x)=sin(x) as limits from 0 to pi/2
answer is =sin(pi/2)-sin(0)=1
From matlab code we get
for n=20 answer =0.9995
for n=30 answer =0.9998
for n=50 answer =0.9999
