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

  • MATLAB
  • Thread starter NINHARDCOREFAN
  • Start date
  • Tags
    Integral
In summary, the conversation revolved around approximating the integral of f(x)=exp(-x) on the interval x=[1,5] by choosing different values of N (number of sub-intervals). The method used was to divide the interval into smaller sub-intervals and calculate the contribution from the left and right endpoints, as well as the intermediate points. The output of the code resulted in a row vector, which was explained to be due to a multiplication error in the code.
  • #1
NINHARDCOREFAN
118
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
  • #2
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 ?
 
  • #3
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);
 

What is the integral of exp(-x)?

The integral of exp(-x) is equal to -exp(-x) + C, where C is a constant.

What is the significance of approximating the integral of exp(-x)?

Approximating the integral of exp(-x) allows us to find the approximate area under the curve of the function. This can be useful in many applications, such as calculating the probability density function for a continuous random variable.

How does the value of N affect the accuracy of the approximation?

The larger the value of N, the more accurate the approximation will be. This is because as N increases, the number of rectangles used in the approximation also increases, resulting in a better approximation of the area under the curve.

What is the formula for approximating the integral of exp(-x) for a specific value of N?

The formula for approximating the integral of exp(-x) for a specific value of N is given by:

(1/N) * (exp(-0) + exp(-1/N) + exp(-2/N) + ... + exp(-(N-1)/N) + exp(-N/N))

How can we improve the accuracy of the approximation?

One way to improve the accuracy of the approximation is to increase the value of N. Another way is to use more advanced numerical integration techniques, such as Simpson's rule or Gaussian quadrature.

Similar threads

Replies
20
Views
2K
  • Programming and Computer Science
Replies
1
Views
749
  • Differential Equations
Replies
1
Views
664
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
569
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • Calculus and Beyond Homework Help
Replies
1
Views
896
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
6K
Back
Top