My graph won't plot

15 vues (au cours des 30 derniers jours)
Wilma Sunnerberg
Wilma Sunnerberg le 7 Oct 2021
Commenté : Cris LaPierre le 7 Oct 2021
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?

Réponses (2)

Cris LaPierre
Cris LaPierre le 7 Oct 2021
Modifié(e) : Cris LaPierre le 7 Oct 2021
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 le 7 Oct 2021
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 commentaires
Wilma Sunnerberg
Wilma Sunnerberg le 7 Oct 2021
Okay, thank you so much for the help!
Cris LaPierre
Cris LaPierre le 7 Oct 2021
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])

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by