Question
How would you code this on matlab?
Wirite a code to find an approximation for the composite trapezoidal rule. dr witiferent mumber of sabintervals using Use the
0 0
Add a comment Improve this question Transcribed image text
Answer #1


n = input('number of intervals:');
ar = zeros(1,n+1);
b = input('upper limit:');
a = input('lower limit:');
f = @(x) sin(x.^3)./(x + 2);
h = (b - a)/n;
ar(1) = a;
% for loop to create array of points
for i=1:n
ar(i+1) = ar(1) + h*i;
end
% following is the composite trapezoidal rule
T = @(h, ar) h./2.*(f(ar(1)) + f(ar(n+1))) + h.*sum(f(ar(2:n)));

% evaluating result
answer = T(h, ar);
disp('the answer is');
disp(answer);
disp('the global error is');
q = integral(f, 0, 2);
disp(abs((answer - q)));

ANSWERS:

>> cheg_composite_rule
number of intervals:10
upper limit:2
lower limit:0
the answer is
0.1546

the global error is
0.0015

>> cheg_composite_rule
number of intervals:20
upper limit:2
lower limit:0
the answer is
0.1557

the global error is
4.1113e-04

>> cheg_composite_rule
number of intervals:40
upper limit:2
lower limit:0
the answer is
0.1560

the global error is
1.0362e-04

>> cheg_composite_rule
number of intervals:10
upper limit:2
lower limit:0
the answer is
0.1546

the global error is
0.0015

Add a comment
Know the answer?
Add Answer to:
How would you code this on matlab? Wirite a code to find an approximation for the composite trapezoidal rule. dr witiferent mumber of sabintervals using Use the following number of subintervals na...
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
ADVERTISEMENT