Whats wrong with my quadratic formula?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Collin Kerr
el 19 de Abr. de 2016
Editada: Walter Roberson
el 13 de Nov. de 2024
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?
0 comentarios
Respuesta aceptada
James Tursa
el 19 de Abr. de 2016
Editada: James Tursa
el 19 de Abr. de 2016
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.
0 comentarios
Más respuestas (2)
Roger Stafford
el 19 de Abr. de 2016
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.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!