Plotting an integral-defined function in MATLAB

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
jack476
Messages
327
Reaction score
124
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.
 
Physics news on Phys.org
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)))
end
plot(x,y)

---

Are the functions f referring to the same thing?
If so, notice that you have
##f(x) = \int_0^x f(t) dt ##
so
##\frac{d}{dx}f(x) = \frac{d}{dx}\int_0^x f(t) dt = f(x) ##
This implies that ##f(x) = e^x##.
 
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:
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:
f(10)

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:
ezplot(f)
 
Excellent. Thanks guys, that did it.