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

Below is what I have so far. %step size and limits, a and b, are user inputs. %

ID: 1817912 • Letter: B

Question

Below is what I have so far.

 

%step size and limits, a and b, are user inputs.
%
%(h/2)*[f0+2f2+2f3+....+2fn-1+fn)

function y=f(x);

y=1/log(x);

    a=input('Insert lower limit ')
    b=input('Insert upper limit ')
    h=input('Insert step size ')

 

 

I know that the number of "n" terms will be =[(b-a)/h]+1 and will need the program to use the equation "(h/2)*[f0+2f2+2f3+....+2fn-1+fn)" n times, where f0=f(a), f1=f(a)+h, f2=f(a)+2h, f3=f(a)+3h....fn=f(a)+(n*h)

The problem I am having is getting the Trapezoid rule into the program and getting it to work. 

 

I greatly appreciate any help or thoughts!

Explanation / Answer

Save this function separately: and note the ./ is used because i am passing a vector into it to evaluate f(x_2) through f(x_n) creating a solution vector.

function [y]=fcneval(x)
y=1./log(x);

Then here is "a code" using the number of intervals instead of step size to make it easy to use.

a=input('lower bound ');
b=input('upper bound ');
n=input('number of intervals ');
dx=(b-a)/n;
x=a:dx:b;
outer=fcneval(a)+fcneval(b); (this sums up f(a) and f(b))
inner=sum(fcneval(x(2:n))); (this sums up everything in between)
approximation=dx/2*(outer+2*inner) (and this is the trapezoid equation)

I tested it with y=x and it worked perfectly

lower bound 0
upper bound 5
number of intervals 5

approximation =

12.5000

That is the exact area under a 5 wide by 5 tall triangle.

Thus now tested with the intended equation we have:

lower bound 5
upper bound 10
number of intervals 100

approximation =

2.5310

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote