Borrar filtros
Borrar filtros

Why isn't my code working?

1 visualización (últimos 30 días)
Collin Kerr
Collin Kerr el 18 de Abr. de 2016
Comentada: James Tursa el 19 de Abr. de 2016
%8 to 15 meters is the range
%Velocity is 0,15,30,45,60,75,90
clc,clear
F = input('Targeted Distance = ');
V_o = input('Velocity at launch = ');
S_o = input('Elevation angle at launch = ');
a=9.81; %Its a given
b=V_o*sind(S_o) %It makes the coding a bit less confusing.
c=0; %Also a given
fprintf('The targeted distance for the launch is %d meters. \n', F)
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', S_o)
Q1=(-b-sqrt(b^2-4*a*c))/2*a;
disp(Q1)
  2 comentarios
Collin Kerr
Collin Kerr el 18 de Abr. de 2016
Editada: Collin Kerr el 18 de Abr. de 2016
The answer is .264 I don't understand why its not working. The inputs to put in are V_o as 5 and S_o as 15. F is not important at this time.
James Tursa
James Tursa el 19 de Abr. de 2016
Duplicate Question.

Iniciar sesión para comentar.

Respuestas (1)

James Tursa
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.

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by