Matlab Approximate the Integral Assignment

In summary, the assignment is to approximate the integral of the function f(x) = e^(3x) with the given code, using different values of N. The code uses the left-endpoint rule, right-endpoint rule, and trapezoid rule to approximate the integral. The questions ask when the results agree with the exact value of the integral to 4 digits, and how much better the trapezoid rule is compared to the other two. The code can be edited to change the values of "a", "b", and "f" in order to find the actual answer.
  • #1
ialan731
25
0

Homework Statement


Hey Everyone! So I have an assignment that says to approximate the integral f(x)=e^(3x), -1<x<3. Answer the following questions

Run the code with N=10, N=100, and N=1000.
For each approximation, when does the result agree with the exact value of the integral to 4 digits?
How much better is the trapezoidal rule than the other two? Explain this result using the theory given in the textbook and in lecture.

Homework Equations



I was given a sample code:The sample program below uses the left-endpoint rule, the right-endpoint rule and the trapezoid rule to approximate the definite integral of the function.

f(x)=x^2, 0<x<1

Matlab comments follow the percent sign (%)

a= 0;
b= 1;
N = 10;
h=(b-a)/N;
x=[a:h:b]; %creates a vector of n+1 evenly spaced points
f=x.^2;
IL=0;
IR=0;
IT=0;
for k=1:N; %Note that the vector f has (N+1) elements
IL=IL+f(k);
IR=IR+f(k+1);
IT=IT+(f(k)+f(k+1))/2;
end;
IL=IL*h;
IR=IR*h;
IT=IT*h;

fprintf(' When N = %i, we find:\n',N);
fprintf(' Left-endpoint approximation = %f.\n',IL);
fprintf('Right-endpoint approximation = %f.\n',IR);
fprintf(' Trapezoidal approximation = %f.\n',IT);
% Output from this program:
When N = 10, we find:
Left-endpoint approximation = 0.285000.
Right-endpoint approximation = 0.385000.
Trapezoidal approximation = 0.335000.

The Attempt at a Solution



I don't really have an idea to this. We were never taught it and I don't have any prior experience. What I got so far is a=-1;
b=3;
N=10;
h=(b-a)/N;
x=[a:h:b];
f=e.^3x;
Z = trapz(X,Y)

Please help! Thanks in advance!
 
Physics news on Phys.org
  • #2
It looks like you're merely required to take the existing code and edit just the three lines where "a", "b" and "f" are defined.
 
  • #3
Yea, that's what I figured. Now I just have to find the actual answer somehow. I don't actually have Matlab, so that might be an issue lol.
 

FAQ: Matlab Approximate the Integral Assignment

1. What is Matlab Approximate the Integral Assignment?

Matlab Approximate the Integral Assignment is a task given to students in a Matlab programming course where they have to use numerical techniques to approximate the value of an integral.

2. Why is Matlab used for this assignment?

Matlab is used for this assignment because it is a powerful programming language and has built-in functions and tools for numerical computations, making it an ideal choice for approximating integrals.

3. What are the common numerical techniques used in Matlab for approximating integrals?

Some common numerical techniques used in Matlab for approximating integrals include the Trapezoidal Rule, Simpson's Rule, and the Midpoint Rule.

4. How accurate are the results obtained from Matlab Approximate the Integral Assignment?

The accuracy of the results obtained from Matlab Approximate the Integral Assignment depends on the chosen numerical technique and the number of intervals used. Generally, the more intervals used, the more accurate the results will be.

5. How can I check if my Matlab Approximate the Integral Assignment is correct?

You can check the correctness of your Matlab Approximate the Integral Assignment by comparing your results with the exact value of the integral, if known. You can also use built-in functions in Matlab, such as "quad", to compute the exact value and compare it with your approximation.

Similar threads

Replies
3
Views
1K
Replies
6
Views
2K
Replies
1
Views
2K
Replies
8
Views
2K
Replies
3
Views
2K
Replies
1
Views
1K
Replies
6
Views
4K
Replies
2
Views
1K
Back
Top