Matlab Approximate the Integral Assignment

Click For Summary
SUMMARY

The discussion focuses on approximating the integral of the function f(x) = e^(3x) over the interval -1 < x < 3 using MATLAB. Participants are tasked with running code for N values of 10, 100, and 1000 to determine when the approximation agrees with the exact integral value to four decimal places. The conversation also compares the accuracy of the trapezoidal rule against other methods, specifically the left-endpoint and right-endpoint rules, using MATLAB's built-in functions.

PREREQUISITES
  • Understanding of numerical integration methods, specifically the left-endpoint, right-endpoint, and trapezoidal rules.
  • Familiarity with MATLAB programming, including vector creation and loop structures.
  • Knowledge of the exponential function and its properties.
  • Basic understanding of error analysis in numerical methods.
NEXT STEPS
  • Learn how to implement the trapezoidal rule in MATLAB for different functions.
  • Explore MATLAB's built-in functions for numerical integration, such as integral and trapz.
  • Study the convergence of numerical integration methods as N increases.
  • Investigate the theoretical foundations of numerical integration errors and their implications.
USEFUL FOR

Students in numerical analysis, MATLAB users looking to understand numerical integration, and anyone interested in comparing different numerical methods for approximating integrals.

ialan731
Messages
25
Reaction score
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
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.
 
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.
 

Similar threads

Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
1
Views
2K