Use Matlab to integrate fx cosx from 0 to phi2 using the tr

Use Matlab to integrate f(x) = cos(x) from 0 to phi/2. using the trapezoid rule. Please repeat the computation for three 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=50; %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 no of intervals or panels \'n\' for more accuracy

Integration of cos(x) is sin(x) as limits are 0 to pi/2

its precise value is sin(pi/2)-sin(0)=1

From matlab we get 0.9995 for n=20

0.9998 for n=30

0.9999 for n=50

 Use Matlab to integrate f(x) = cos(x) from 0 to phi/2. using the trapezoid rule. Please repeat the computation for three different numbers of panels: N = 20,30

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site