How do I change my polynomial plot from a straight line to a curve?

2 views (last 30 days)
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')

Answers (1)

John D'Errico
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
Josie
Josie on 31 Jul 2014
I can't really explain what I'm doing. Just trying to plot a polynomial for my MSc thesis. I know I'm awful at coding but come September I'll never have to do it again so I appreciate the help! This looks a lot more like what it's supposed to. I definitely don't know the difference between * vs .*, / vs ./, and ^ versus .^ . Where can I read up on this?
FYI M=mass, G=gravity, N=normal force, theta=angle of rotation - I understand what they mean!
Thanks for the help!
John D'Errico
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!