No points without MATLAB setup and run well, please: Write ONE MATLAB script tha
ID: 2965907 • Letter: N
Question
No points without MATLAB setup and run well, please:
Write ONE MATLAB script that does the recursive trapezoid rule using the function: f(x) = 2sin(x)cos(x), evaluating the integral between 0 and pi/2. Iterate until you achieve a relative error of 10^-4.
1) Perform the exact integration result analytically, and script produces the following:.
2) The resulting integral, with the final relative error in two ways, the final trapezoidal result relative to the exact result and the final trapezoidal result relative to the previous iteration
Explanation / Answer
e=exact value of integral= (-1/2)*cos(2*x)|0pi/2 = 1
re1=relative error w.r.t exact value
re2=relative error w.r.t previous iteration value
clc
clear all
e=1;
re1=.00001;
re2=1;
sum(1)=0;
i=2;
while abs(re2)>.0001
x=zeros(i,1);
y=zeros(i,1);
for j=2:i
x(j)=((j-1)*(pi/2))/(i-1);
y(j)=sin(2*x(j));
end
sum(i)=0;
for k=2:i-1
sum(i)=sum(i)+((x(2)-x(1))*y(k));
sum(i)=vpa(sum(i));
end
if i>=3
re2=(sum(i)-sum(i-1))/sum(i);
else
re2=1;
end
re1=(e-sum(i))/e;
i=i+1;
end
fprintf('Number of iterations = ')
disp(i-2)
fprintf('Number of terms in final sum = ')
disp(i-1)
fprintf('Value of integral = ')
disp(vpa(sum(i-1)))
output
Number of iterations =
26
Number of terms in final sum =
27
Value of integral =
0.99878303663202083750860538202687
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.