Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

b.)Make a Matlab function that calculates the integral approximately using the t

ID: 1719572 • Letter: B

Question

b.)Make a Matlab function that calculates the integral approximately using the trapezoid rule. You don’t need to program the trapezoid rule from scratch. Instead, inform yourself about the Matlab command trapz and try to use it. Your trapezoid method should use about 100 trapezoids (i.e., around 100 breakpoints in the interval [0,1]). Do the result for the trapezoid rule approximation of I20.

c.) Notice how the solutions for part 1 and part 2 diverge as n increases. What is the first value of n for which the answers are more than 103 apart? Finish the quantity n that you find, and an explanation of which method you think is more correct.

Explanation / Answer

a)

MATLAB Code

function [I_n] = integral_rec(n)
I_n=log(6/5);
for i=1:n
I_n=(1/i)-(5*I_n);
end
end

Output :

>> integral_rec(20)

ans =

0.0042

_______________________________________________________________________________________________

b)

MATLAB Code

function [ I_n ] = ntrapz( n )
x= 0:1/100:1;
y=(x.^n)./(5.+x);
I_n= trapz(x,y);
end

Output :

>> ntrapz(20)

ans =

0.0080

_______________________________________________________________________________________________

c)

MATLAB Code

function [n] = Find_n(val)
n=1;
while 1
if abs(integral_rec(n)-ntrapz(n))>=val
return
else
n=n+1;
end
end
end

Output :

>> Find_n(103)

ans =

27

_______________________________________________________________________________________________

TRAPEZOID RULE IS BETTER.