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

Plotting an integral-defined function in MATLAB

  1. Aug 5, 2015 #1
    At this exact moment, the proper name of functions defined by:

    f(x) = ∫0xf(t)dt escapes me, so I apologize for the title maybe not being as clear as possible.

    What I'd like to know is how to go about defining such a function so that I can plot it. Do I just put the variable x into the upper bound of integration, then define the function to be whatever comes from that? Thank you.
  2. jcsd
  3. Aug 5, 2015 #2


    User Avatar
    Homework Helper

    To plot this, you might want to use some sort of iterative scheme.
    The most simple is the trapezoid rule.
    x= linspace(0, 1) ;
    dx= t(2) - t(1);
    y = zeros(length(x));
    y(1) = 0;
    for i = 2:length(x)
    y(i) = y(i-1) + dx/2*(f((x(i))+f(x(i-1)))


    Are the functions f referring to the same thing?
    If so, notice that you have
    ##f(x) = \int_0^x f(t) dt ##
    ##\frac{d}{dx}f(x) = \frac{d}{dx}\int_0^x f(t) dt = f(x) ##
    This implies that ##f(x) = e^x##.
  4. Aug 6, 2015 #3


    User Avatar
    Gold Member

    You can do this using a function handle. Consider this code for the function [itex]f(x) = \int _0^x \sin(t) e^t dt[/itex].

    Code (Text):

    f = @(x) integral(@(t) sin(t).*exp(t), 0, x);
    To evaluate, you can just call the function with a value for the limit of integration:
    Code (Text):

    To plot, you can use ezplot. It returns a warning since f isn't vectorized (needs to loop over values of x to pass to integral one at a time), but it still works and generates a plot.

    Code (Text):

  5. Aug 6, 2015 #4
    Excellent. Thanks guys, that did it.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook