Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!


  1. Sep 17, 2004 #1
    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;






    for n=2:N

    f = exp(-n) * x;



    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


    Aren't I suppose to get only one answer? Why am I getting all this?
  2. jcsd
  3. Sep 21, 2004 #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 ?
  4. Sep 21, 2004 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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);
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Similar Threads for MATLAB Help Date
MATLAB MATLab: Not enough inputs for nlinfit Nov 16, 2017
MATLAB Help needed to fix a matlab code Nov 8, 2017
New to Matlab, help with vectors Feb 20, 2016
Need help with integral Dec 23, 2015
MATLAB 2D diffusion equation, need help for matlab code. Nov 7, 2015