Whats wrong with my quadratic formula?
조회 수: 6 (최근 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?
댓글 수: 0
채택된 답변
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.
댓글 수: 0
추가 답변 (1개)
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!