line in my graph does not show

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 Comments

Niki
Niki on 22 Feb 2024
Moved: Voss on 22 Feb 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.
@Niki: What happens if you run the code again?
If that doesn't work, post the code in a new question.

Sign in to comment.

 Accepted Answer

Chad Greene
Chad Greene on 3 Dec 2014
Edited: Chad Greene on 3 Dec 2014
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.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

on 3 Dec 2014

Edited:

on 22 Feb 2024

Community Treasure Hunt

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

Start Hunting!