How do I change my polynomial plot from a straight line to a curve?
2 views (last 30 days)
Show older comments
I don't know what I've done wrong but my plot is supposed to come out slightly curved and at the moment comes out as a straight line. Is it the linspace function because I don't really understand what this does, and if so, what do I change it to instead?
M=linspace(0,0.1,10)
g=10;
for A=0.1:0.1:0.9
s=(A-0.5)*M*g-(A^2)/4;
theta=s/(2*M*(A-0.5)^2+2*M+(A^2-A+1/3)/2);
N=((2*M-0.5*(A^2-A+1/6))*theta-0.25*(A^2-A))/(A-0.5);
plot(M,N)
axis([0,0.1,-0.4,1])
hold all
end
legend('A=0.1','A=0.2','A=0.3','A=0.4','A=0.5','A=0.6','A=0.7','A=0.8','A=0.9')
0 Comments
Answers (1)
John D'Errico
on 31 Jul 2014
I have NO idea what it is you are doing here, soI don't really know if you have any idea what you are doing either. Only your comments tend to imply that you don't know the difference between * vs .*, / vs ./, and ^ versus .^ .
So my guess is you are mightily misusing those operators. This might be a bit better, but since I don't know what all this code does, it really is a guess.
M=linspace(0,0.1,10);
g=10;
for A=0.1:0.1:0.9
s=(A-0.5)*M*g-(A^2)/4;
theta=s./(2*M.*(A-0.5)^2+2*M+(A^2-A+1/3)/2);
N=((2*M-0.5*(A^2-A+1/6)).*theta-0.25*(A^2-A))/(A-0.5);
plot(M,N)
axis([0,0.1,-0.4,1])
hold all
end
legend('A=0.1','A=0.2','A=0.3','A=0.4','A=0.5','A=0.6','A=0.7','A=0.8','A=0.9')
Next, I would STRONGLY suggest that you learn that variable names can be more than ONE character. Use names that actually mean something. This makes your code more readable. It makes it easier to debug. Anyway, they don't charge by the umber of characters you use.
Also, your code will improve once you start to use comments in your code. Again, this makes the code easy to debug when you find a bug in it next week or next month.
Finally, if you don't even know what linspace does, it is time for you to start reading the help. Try a few things out. See what linspace does at the command line.
2 Comments
John D'Errico
on 31 Jul 2014
Edited: John D'Errico
on 31 Jul 2014
* is a matrix multiply operator, a dot product. If one of the arguments is a scalar, then it works like you might want.
But IF you really just wanted to do a element-wise multiply, then you need to use .* instead.
The same applies to ./ and .^, both of which are the element-wise operations.
So while you can often get away with using * ^ and / instead of .* .^ and ./ you will often get burnt when you use them on matrices and vectors. Much of the time, it just results in a somewhat confusing error message, telling you that the dimensions do not conform for whatever operation you did. But sometimes the code runs as it did here, yet the results are not what you expected.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!