How to see graphs when you run your code?
Afficher commentaires plus anciens
I dont know if it doesn’t run and show me graphs because there is an error in the code or because I dont press the right keys. Can someone tell me how we go about executing the program to see graphs?
2 commentaires
dpb
le 27 Fév 2016
Which "program" are you speaking of, specifically?
Rena Berman
le 13 Mar 2017
(Answers Dev) Restored edit
Réponses (1)
Walter Roberson
le 27 Fév 2016
Are the axes showing up but there appears to be no content in them? If that is happening and you are using plot(), then you have probably called plot on only one point when you thought you were creating a line plot. By default plot() uses no Marker so plotting of a single point does not show up at all. You can check if this is the case by adding a marker to the call, such as
plot(x, y, 'Marker', '*')
then if a single dot shows up you know that something was plotted but it was only one point.
It is common for beginners to use a loop that looks something like
for K = 1 : 10
x = K * 5; %x is a single value
y = x.^2 - 3; %y is a single value that is some function of x
plot(x, y);
end
or they might add a "hold on" after the plot(). This has the difficulty that I noted above: only one point at a time is being plotted and no marker has been given so by default plot does not draw anything.
When plot() is used in a loop to draw one point at a time, it does not instruct the graphics system to join the points. Points are only automatically joined when they are all plotted together.
The way to resolve this is to record the x and y values and do the plot afterwards, such as
for K 1 : 10
x(K) = K * 5; %save this particular x value
y(K) = x(K).^2 - 3; %use this particular x value, compute this y, and save it
end
plot(x, y)
then x and y are vectors of values, and plot() automatically joins points when vectors of values are given.
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!