Approximate the integral on f(x)=exp(-x) on the interval x=[1,5]. Choose N=10, 100, 1000.(adsbygoogle = window.adsbygoogle || []).push({});

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 Forums | Science Articles, Homework Help, Discussion**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# MATLAB Help

**Physics Forums | Science Articles, Homework Help, Discussion**