My graph won't plot
조회 수: 15 (최근 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!


