My graph won't plot

조회 수: 6 (최근 30일)
Wilma Sunnerberg
Wilma Sunnerberg 2021년 10월 7일
댓글: Cris LaPierre 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