Question

Question: Code your own MATLAB program to find the approximate value of the Integeral 0 to...

Question:

Code your own MATLAB program to find the approximate value of the

Integeral 0 to 2 ex dx with Trapezoidal rule for N = 20, i.e., t20. Also give the relative error of t20 with

Integeral 0 to 2 ex dx = e2 − 1.

(Hint: For-end loops or Elementwise operations)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

To calculate the integration we shall use the trapezoidal rule given below:

%%%SCRIPT STARTS%%%

upperLimit = 2;
lowerLimit = 0;
N = 20;
h = (upperLimit - lowerLimit) / N;

y = []; % will store values of function at differnt points
for i = lowerLimit:h:upperLimit
    y = [y, exp(i)];
end
%Trapezoidal rule starts
sum = y(1) + y(length(y)); %add first and last element

for i = 2:length(y)-1
    sum = sum + 2*y(i); %add remaining items multiplied by 2
end

results = sum * h / 2 %get results

actualResult = exp(2) - 1;%get actual result of the integration


absoluteError = abs(results - actualResult); %absolute error
relativeError = absoluteError/actualResult %calculate relative error

I tried to keep the code as simple as possible. I have also commented almost every line of the code to make things easy. If incae you face trouble with the code, please feel free to comment below. I shall be glad to help you :)

Add a comment
Know the answer?
Add Answer to:
Question: Code your own MATLAB program to find the approximate value of the Integeral 0 to...
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