How to Adjust y axis plot range in Matlab?

2020-03-20 04:19发布

I need to plot the following functions in matlab

y1=sign(x)
y2=tanh(x)
y3=(x)/(x+1)

The x-range is -5,5 with 0.1 spacing The y-plot range should be between -1.5 to 1.5.

Each plot should have a labeled x and y axis and a legend in the lower right corner.

The only things I cant figure out is how to adjust the y plot range. Ive tried editing the actual figure but all that seems to do is distort the graph. Is there a command within matlab that will let me adjust the y axis plot range?

The other thing I havent figured out yet is adding a legend, I can do it after the figure is created but I guess it needs to be done by matlab command.

标签: matlab plot
2条回答
够拽才男人
2楼-- · 2020-03-20 04:37

If you only want to set the y-range without setting the x-range you can use

ylim([-1.5 1.5])

or alternatively axis([-inf inf -1.5 1.5]). I found this from the original MATLAB-source: https://de.mathworks.com/help/matlab/ref/ylim.html

PS: For trigonometric functions I would recommend to use axis equal to have equally spaced x and y-axis (see MATLAB)

查看更多
Luminary・发光体
3楼-- · 2020-03-20 04:41

Yes, use axis after the plot command:

axis([-5 5 -1.5 1.5])
查看更多
登录 后发表回答