this is my file . when i plot my function the figure wil occur but no line is showed in it. what did i do wrong. it is driving me crazy
clear all clc
format compact
for x = 0:1:6;
V = (5/3 * x^3) ;
M = (5/4 * x^4);
end
plot ( x,V,x,M );
and this is what i'm getting out of the plot

2 commentaires

Niki
Niki le 22 Fév 2024
Déplacé(e) : Voss le 22 Fév 2024
My graph was there and now it is gone. No idea what I hit or what I did. Any help is appreciated. I did not change my code.
Voss
Voss le 22 Fév 2024
@Niki: What happens if you run the code again?
If that doesn't work, post the code in a new question.

Connectez-vous pour commenter.

 Réponse acceptée

Chad Greene
Chad Greene le 3 Déc 2014
Modifié(e) : Chad Greene le 3 Déc 2014

2 votes

The data you've created in the for loop is indeed shown as tiny little dots! Unfortunately, you've overwritten V and M with every iteration of the for loop, so all you're left with is the last set of entries: x=6, V=360, and M=1620. Look close and you'll see dots at those locations in the image you posted.
To do what you're trying to do in a for loop, try this:
x = 0:1:6;
for k = 1:length(x)
V(k) = (5/3 * x(k)^3) ;
M(k) = (5/4 * x(k)^4);
end
plot ( x,V,x,M );
But the above solution is inefficient, ugly, and generally bad practice. It'd be really slow if you had a big data set. A better way is to vectorize:
x = 0:1:6;
V = (5/3 * x.^3) ;
M = (5/4 * x.^4);
plot ( x,V,x,M );
Notice that in the vectorized solution we had to switch to the dot operator .^ because with multiplication and powers the dot or no dot forms are equivalent to a dot product versus cross product.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots 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!

Translated by