How to plot a piece-wise function using FPLOT
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ali Remo
el 29 de En. de 2017
Comentada: Star Strider
el 29 de En. de 2017
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.
0 comentarios
Respuesta aceptada
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
Star Strider
el 29 de En. de 2017
My pleasure.
If my Answer helped you solve your problem, please Accept it.
Más respuestas (0)
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!