MATLAB Plotting an integral-defined function in MATLAB

AI Thread Summary
The discussion centers on defining and plotting a function represented by the integral f(x) = ∫0^x f(t) dt. Participants clarify that to define such a function, the variable x should be placed in the upper limit of integration. They suggest using numerical methods, like the trapezoid rule, for plotting the function. One participant provides a code snippet to implement this using MATLAB, demonstrating how to compute the integral and plot it. The conversation also touches on the relationship between the function and its derivative, concluding that f(x) can be expressed as e^x. Additionally, a function handle is introduced for evaluating the integral, and while there are warnings about vectorization, the method is confirmed to work for generating plots.
jack476
Messages
327
Reaction score
125
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 f(x) = \int _0^x \sin(t) e^t dt.

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.
 

Similar threads

Replies
1
Views
5K
Replies
4
Views
2K
Replies
3
Views
2K
Replies
4
Views
1K
Replies
2
Views
3K
Replies
2
Views
3K
Back
Top