Plotting an integral-defined function in MATLAB

In summary, the conversation discusses how to define and plot a function f(x) = ∫0xf(t)dt, which seemed unclear at first due to the lack of a proper name. It is determined that the function can be defined by putting the variable x into the upper bound of integration and that the trapezoid rule can be used for plotting. It is also mentioned that f(x) = e^x can be achieved using a function handle and that ezplot can be used for plotting, despite a warning being generated due to the function not being vectorized. It is then determined that the desired outcome has been achieved.
  • #1
jack476
328
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
  • #2
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##.
 
  • #3
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)
 
  • #4
Excellent. Thanks guys, that did it.
 

1. How do I plot an integral-defined function in MATLAB?

To plot an integral-defined function in MATLAB, you can use the fplot function. This function takes two arguments - the function to be plotted and the range of values for the independent variable. For example, if you want to plot the function f(x) = ∫(x^2 + 1)dx from 0 to 5, you would use the command fplot(@(x)integral(@(t)t.^2+1,0,x), [0,5]).

2. Can I plot multiple integral-defined functions on the same graph?

Yes, you can plot multiple integral-defined functions on the same graph by using the hold on command after each fplot function. This will allow you to plot each function on the same figure without overwriting the previous plot. For example, if you want to plot the function f(x) = ∫(x^2 + 1)dx and g(x) = ∫(x^3 + 2)dx on the same graph, you would use the commands fplot(@(x)integral(@(t)t.^2+1,0,x), [0,5]) and hold on followed by fplot(@(x)integral(@(t)t.^3+2,0,x), [0,5]).

3. How can I change the color or style of the plotted integral-defined function?

You can change the color or style of the plotted integral-defined function by specifying additional arguments in the fplot function. For example, to plot the function f(x) = ∫(x^2 + 1)dx in red with a dashed line, you would use the command fplot(@(x)integral(@(t)t.^2+1,0,x), [0,5], 'r--'). The 'r--' argument specifies the color as red and the line style as dashed.

4. How can I add a title and labels to my plot?

You can add a title and labels to your plot by using the title, xlabel, and ylabel functions. For example, if you want to add a title of "Plot of Integral-Defined Function" and labels for the x and y-axis, you would use the commands title('Plot of Integral-Defined Function'), xlabel('x-axis'), and ylabel('y-axis') after your fplot function.

5. How can I save my plot as an image file?

To save your plot as an image file, you can use the saveas function. This function takes two arguments - the figure handle and the file name. For example, if you want to save your plot as a PNG file named "myplot.png", you would use the command saveas(gcf, 'myplot.png') after your fplot and title functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
844
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top