How to plot a piece-wise function using FPLOT

Hello Please state if possible, how to use fplot to have the graph of a piecewise function. Actually the code below is exactly according to the matlab page: https://uk.mathworks.com/help/matlab/ref/fplot.html
fplot(@(x) exp(x),[-3 0],'b')
hold on
fplot(@(x) cos(x),[0 3],'b')
hold off
grid on
But, it does not work for me. The code plots only the second function, cos(x). Please help me if possible.

 Respuesta aceptada

Star Strider
Star Strider el 29 de En. de 2017
Editada: Star Strider el 29 de En. de 2017
It is easiest to use ‘logical indexing’ in your function.
This works:
f = @(x) exp(x).*(x<=0) + cos(x).*(x>0);
figure(1)
fplot(f, [-3, 3])
grid
EDIT The code you posted actually works for me without error, producing the correct plot. The online documentation is for the current release (that I believe is still R2016b), so if you have an earlier version, that could be a problem. My code should work.

6 comentarios

Thank you for your answer, but my problem is: I have , for example, one hundred of these functions that are produced in a FOR LOOP, each interval for each function will be produced in the FOR Loop:
syms x
for k = 1 : 100
f(x) = k*x^2 + (k-1)*x ;
fplot ( f , [k k+1] )
hold on
end
hold off
My pleasure.
The Symbolic Math Toolbox is not at all efficient for iterative problems. I would write ‘f’ as an anonymous function and do this instead:
figure(1)
hold on
for k = 1 : 100
f = @(x) k*x.^2 + (k-1)*x ;
fplot ( f , [k k+1] )
end
hold off
grid
This is much faster, although if you are plotting on a continuous interval and want your result to be continuous rather than appearing to be stepwise, it is possible to write much more efficient code.
With respect to the anonymous function, see the section on ‘Anonymous Functions’ in the documentation for Function Basics (link) for details.
No loop:
f = @(x) floor(x).*x.*x + (floor(x) - 1).*x
fplot(f, 1,101)
The expression could be factored for performance
Star Strider
Star Strider el 29 de En. de 2017
@Walter — I quite definitely agree with you.
I assume this is a ‘proxy’ for a different problem, so the loop could be unavoidable. We don’t know.
@Star Strider , Thank you very much for your help. Yes actually the main code contains functions which are continuous in continuous intervals. The code above which I wrote was only to say what my problem is actually. In each interval I have a different function, but finally they generate a continuous function, for example:
in the interval [0 1] ....... f1(x) = x
in the interval [1 2] ....... f2(x) = x^2
.
.
.
and as you can see, these two samples are continuous at the point x=1. The point is that I have to produce these function in a FOR LOOP and plot them in a single graph. Thank you
Star Strider
Star Strider el 29 de En. de 2017
My pleasure.
If my Answer helped you solve your problem, please Accept it.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Centro de ayuda y File Exchange.

Preguntada:

el 29 de En. de 2017

Comentada:

el 29 de En. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by