My graph won't plot
10 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
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])
0 个评论
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 个评论
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])
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!