Approximating Integral of exp(-x) for N=10, 100, 1000

  • Context: MATLAB 
  • Thread starter Thread starter NINHARDCOREFAN
  • Start date Start date
  • Tags Tags
    Integral
Click For Summary
SUMMARY

The forum discussion focuses on approximating the integral of the function f(x) = exp(-x) over the interval [1, 5] using MATLAB for different values of N (10, 100, 1000). The user encountered an issue where the output was a row vector instead of a single scalar value due to incorrect vector multiplication in the code. The solution involves modifying the line of code from f = exp(-n) * x; to f = exp(-n) * x(n); to ensure proper element-wise multiplication, thus yielding the correct integral approximation.

PREREQUISITES
  • Understanding of numerical integration techniques
  • Familiarity with MATLAB programming
  • Knowledge of vector and matrix operations in MATLAB
  • Basic concepts of the exponential function
NEXT STEPS
  • Learn MATLAB's vectorization techniques for efficient computation
  • Explore numerical integration methods such as the Trapezoidal Rule and Simpson's Rule
  • Study MATLAB's built-in functions for numerical integration, such as integral()
  • Investigate the implications of using different values of N on the accuracy of integral approximations
USEFUL FOR

Mathematicians, engineers, and students interested in numerical methods, particularly those using MATLAB for computational mathematics and integral approximations.

NINHARDCOREFAN
Messages
118
Reaction score
0
Approximate the integral on f(x)=exp(-x) on the interval x=[1,5]. Choose N=10, 100, 1000.

Okay here is what I did:
% a,b limits of integration

% x variable of integration

% f integrand

% N is the number of sub-intervals

% h width of each sub-interval

% TL, TR contribution from left and right endpoints

%TI contribution from the intermediate points

a= 1;

b= 5;

N = 10;

h=(b-a)/N;

x=[a:h:b];

TL=exp(-a);

TR=exp(-b);

TI=0;

for n=2:N

f = exp(-n) * x;

TI=TI+f;

end
I=(TL+2*TI+TR)*h*.5

I =

Columns 1 through 5

0.1606 0.1948 0.2291 0.2633 0.2976

Columns 6 through 10

0.3318 0.3661 0.4003 0.4346 0.4688

Column 11

0.5031

Aren't I suppose to get only one answer? Why am I getting all this?
 
Physics news on Phys.org
My experience with MATLAB is very limited but by the looks of it you have defined x as a row matrix, meaning that f will also be a row matrix and so will TI since your answer is a function of TI yo uare bound to end up with a row matrix. I haven't really looked at the maths involved, what method for integrating did you use ?
 
NINHARDCOREFAN said:
Aren't I suppose to get only one answer? Why am I getting all this?

Code:
a= 1;
b= 5;
N = 10;
h=(b-a)/N;
x=[a:h:b];
TL=exp(-a);
TR=exp(-b);
TI=0;
for n=2:N
f = exp(-n) * x;
TI=TI+f;
end
I=(TL+2*TI+TR)*h*.5
I = {row vector}

The problem arises here:
f = exp(-n) * x;

You're multiplying exp(-n) which is a scalar times x which is a vector.

To fix this you want to access the individual cell of the x vector.

Try replacing f = exp(-n) * x; with f = exp(-n) * x(n);
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
8K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
6
Views
2K
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K