Whats wrong with my quadratic formula?

1 次查看(过去 30 天)
V_o = input('Velocity at launch = ');
Theta = input('Elevation angle at launch = ');
a=9.81; %Its a given
b=V_o*sind(Theta) %It makes the coding a bit less confusing.
c=0; %Also a given
fprintf('The velocity for the launch is %d meters per second. \n', V_o)
fprintf('The elevation angle for the launch is %d degrees. \n', Theta)
Q1=(-b-sqrt(b^2-4*a*c))/2*a;
disp(Q1)
For some reason the code is not lining up with a calculators numbers. I was wondering if someone could explain what specifically is wrong with the quadratic formula and how could I be able to fix it?

采纳的回答

James Tursa
James Tursa 2016-4-19
编辑:James Tursa 2016-4-19
You need to enclose the denominator in parentheses:
Q1=(-b-sqrt(b^2-4*a*c))/(2*a);
The way you have it coded, since / and * have the same precedence the operations are performed left to right and MATLAB sees your current coded expression as:
Q1=((-b-sqrt(b^2-4*a*c))/2) * a;
Which is not what you wanted.

更多回答(1 个)

Roger Stafford
Roger Stafford 2016-4-19
Apparently you are trying to see how many seconds the object in question would take to return to earth. The velocity equation is:
v = -a*t+b
If you integrate with respect to t, you get
x = int(v,t) = -a*t^2/2+b*t
The solution is either t = 0 (the beginning) or t = b/(2*a) (at the end.) Your mistake was twofold. You had a positive value for a, and you didn't include the 1/2.

Community Treasure Hunt

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

Start Hunting!

Translated by