MATLAB: 4 different wrong eduations instead of one right after using solve

empty sym

>> syms g h q sx sy v;
>> sy = (-g/2) * ( sx/(cos(q)*v) )^2 + v * sin(q) * ( sx/(cos(q)*v) )^2 + h;
>> sx=solve(sy,sx)
sx =
(2^(1/2)*v*cos(q)*(g*h – 2*h*v*sin(q))^(1/2))/(g – 2*v*sin(q))
-(2^(1/2)*v*cos(q)*(g*h – 2*h*v*sin(q))^(1/2))/(g – 2*v*sin(q))
>> sx = ( 2^(1/2) * v * cos(q) * (g * h – 2*h*v*sin(q) )^(1/2) ) / (g – 2*v*sin(q));
>> sx = diff(sx,q);
>> q=solve(sx,q);
>> q
q = asin((g + (g^2 – 4*v^2)^(1/2))/(2*v))
pi – asin((g – (g^2 – 4*v^2)^(1/2))/(2*v))
asin((g – (g^2 – 4*v^2)^(1/2))/(2*v))
pi – asin((g + (g^2 – 4*v^2)^(1/2))/(2*v))
The right equation is:
q = arcsin (v/(2*v^2+2*h*g))
Is there a way in matlab to get this equation?

Best Answer

The web page is not clear, but this seems to be a simple projectile problem where you're trying to maximize the horizontal distance. Suppose the projectile is ejected at an angle q with respect to the vertical at a speed v and from a height h above the ground. The height at time t is z = h + v*t*sin(q) - 0.5*g*t^2. The time t0 at which this height is zero is the positive root of the RHS: t0 = (v*sin(q) + sqrt(v^2*sin(q)^2-2*g*h))/g.
The object is to maximize the horizontal distance x = v*t*cos(theta). This is the solution of dx/dt = 0, but the solutions also include minima and other local maxima that are not the global maximum.
t0 = (v*sin(q) + sqrt(v^2*sin(q)^2-2*g*h))/g;
x = t0*cos(q)
x =
2 2 1/2
cos(q) (sin(q) v + (sin(q) v - 2 g h) )
-------------------------------------------
g
Note that since we are trying to maximize this distance, we can normalize by the positive factor v/g, leaving x0 = cos(q)*(sin(q) + sqrt(sin(q)^2 - 2*B^2)), where B^2 = g*h/v^2. Try plotting this function of angle for a given B:
B = 1;
f = @(q) cos(q).*(sin(q) + sqrt(sin(q).^2 - 2*B^2));
x = (0:.01:1)*2*pi; y = f(x); plot(x,y)
As you can see, there are two maxima and two minima.
Now try the symbolic solution.
syms B
x = cos(q).*(sin(q) + sqrt(sin(q).^2 - 2*B^2));
dddq = diff(x,q);
qsols = solve(dddq,q)
You can play around with different values of B to see which solution is the one you want:
double(subs(qsols,B,0.5))
ans =
-2.1863
-0.9553
2.1863
0.9553
Note that only the fourth solution is between 0 and pi/2. The symbolic solution is
disp(qsols(4))
1/2 2 2 1/2
1 2 ((B - 1) (2 B - 1))
atan(--------------, - 1/2 -----------------------------)
2 1/2 2
(-2 B + 2) B - 1
This atan is the two-argument arctangent atan(y,x).
Related Question