Reputation: 19
I tried to plot a piecewise function in matlab.
syms x
y = piecewise(-1<x<0, x^2+2*x, 0<=x<1, 0);
fplot(y)
as the plot came is correct but visually its no good.
I want to set its origin at the center of the plot. How to do it?
Upvotes: 1
Views: 791
Reputation: 19689
Set same minimum and maximum space on both sides of the origin.
ax = gca;
MaxX = max(abs(ax.XLim)); MaxY = max(abs(ax.YLim));
axis([-MaxX MaxX -MaxY MaxY]);
If you also want (psuedo)-axis lines at origin, you may further use:
xline(0,'--'); yline(0,'--'); %requires R2018b
But if you actually want to move the axis location to origin then you may use the properties
XAxisLocation
and YaxisLocation
instead of pseudo axis lines.
ax.XAxisLocation='origin'; ax.YAxisLocation='origin';
Upvotes: 4