Question

Write a MATLAB function/script that performs the following tasks. Approximate: 2+2 (a) Using the composite Trapezoidal rule w
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

function t=comptraprule(f,a,b,n)
h=(b-a)/n;
exactVal=log(b^2+2)-log(a^2+2);
sum=0;
for i=1:n-1
x(i)=a+i*h;
sum=sum+f(x(i));
end
Itrap=h*(f(a)+2*sum+f(b))/2;
disp(['Itrap:'' ',num2str(Itrap)]);
disp(['exactVal:'' ',num2str(exactVal)]);
end

b)

function s=simprl(f,a,b,n)
h=(b-a)/(2*n);
exactVal=log(b^2+2)-log(a^2+2);
s1=0;
s2=0;
for k=1:n
x=a+h*(2*k-1);
s1=s1+f(x);
end
for k=1:(n-1)
x=a+h*2*k;
s2=s2+f(x);
end
Isimp=h*(f(a)+f(b)+4*s1+2*s2)/3;
disp(['Isimp:'' ',num2str(Isimp)]);
disp(['exactVal:'' ',num2str(exactVal)]);
end

c) After saving both the above codes in the script window as (comptraprule.m) and simprl.m respectively (don't rule) and type the below code:

>> simprl (@(x) (2*x)/(x^2+2), 0,4,8) Isimp: 2.1973 exactVal: 2.1972 >> comptraprule (@(x) (2*x)/(x^2+2), 0,4,8) Itrap: 2.

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB function/script that performs the following tasks. Approximate: 2+2 (a) Using the composite Trapezoidal...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT