My graph won't plot

10 次查看(过去 30 天)
Wilma Sunnerberg
Wilma Sunnerberg 2021-10-7
f=@(x)((3+sin(2.*x))/(1+exp(0.03.*x.^2)))-1.2;
x=linspace(-6,6);
plot(x,f(x))
axis([-6 6 -1 1])
This is the code I've put in MATLAB. When i run the script an empty figure shows up without the graph. How can i fix it to show the graph in the figure?

回答(2 个)

Cris LaPierre
Cris LaPierre 2021-10-7
编辑:Cris LaPierre 2021-10-7
The issue is that you are performing a matrix division in your function, which results in a single value. It looks like you want element-wise. Use "./" instead. See here for more details.
f=@(x)((3+sin(2.*x))./(1+exp(0.03.*x.^2)))-1.2;
% ^^
x=linspace(-6,6);
plot(x,f(x))
axis([-6 6 -1 1])

Voss
Voss 2021-10-7
Your expression for f contains matrix division (/), which causes f(x) to return a scalar, so the vectors you're plotting (x and f(x)) have different lengths, which is why the plot is empty. To fix it, change the matrix division to element-wise division (./), like this:
f=@(x)((3+sin(2.*x))./(1+exp(0.03.*x.^2)))-1.2;
  2 个评论
Wilma Sunnerberg
Wilma Sunnerberg 2021-10-7
Okay, thank you so much for the help!
Cris LaPierre
Cris LaPierre 2021-10-7
Just one clarification. When plotting with a vector for x and a scalar for y, MATLAB reuses the same y value for each x value. The reason nothing appears is because each value is plotted as a separate data series of a single point. The default plot format is a solid line without a marker, so nothing is visible for a single point. If you include a marker style in your plot syntax, you will see what is happening.
f=@(x)((3+sin(2.*x))/(1+exp(0.03.*x.^2)))-1.2;
x=linspace(-6,6);
plot(x,f(x),'-o')
% ^
axis([-6 6 -1 1])

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by