kill(all)$

max_range and optimal angle    'version 1.0'
peter.vlasschaert@gmail.com,27/06/2018.

with : use command : find_root( f(θ),θ,begin value from θ,end value of θ )

load :packages  use : lratsubst([,,,],eq)

load(lrats)$

motion : 2D

depends([x,y],t);

equation motion 2D,gravity field. (- y : direction)

p1:x=x_0+v_x0*t;
p2:y=y_0+v_y0*t-1/2*g*t^2;

initial position,initial velocity :cannon.

initial position : cannon

p3:x_0 =x0;
p4:y_0 =y0;

initial velocity : cannon  ( v0 ,angle = θ )

p5:v_x0=v0*cos(θ);
p6:v_y0=v0*sin(θ);

result : equation of motion.  2D

p7:[p3,p4,p5,p6]$

p8 : lratsubst(p7,p1)$
p8a:expand(p8);
p9:  lratsubst(p7,p2)$
p9a:expand(p9);

hit target : y=0 ,time.

p10:solve(rhs(p9a)=0,t);

rem : time pos. value. ' second value', [2]

p11:lratsubst([t=t_hit],p10[2]);

range of projectile :  use ' p11'  => value for  : 'x=x(t)'

p12:lratsubst([t=t_hit],p8a);

p13:lratsubst([p11],p12);
p13a:expand(p13);

rem : x0,v0 ,g are constant

depends(x,θ);

p14:trigreduce(p13a);

rem : x=x_max ,   -1 = sin(angle) =< 1
rem : sin(2*θ) =1 , find :  θ

p15:solve(sin(2*θ)=1,θ);
p15a:p15[1];

p16:lratsubst([x=x_max,p15a],p14)$
p16a:expand(p16);

when : cannon ,position (x0,y0) =(0,0)

p17:[x0 = 0, y0 =0];

assume(v0>0);

p18:lratsubst(p17,p16a);

x = x(θ),from p13a

p19:diff(p13a,θ);

p19a :ratsimp(p19);

find : 'diff(x,θ,1) = 0
? which θ =  gives Max ranges for a target.

suppose : cannon in the orgin (x0,y0) = (0,0)
                    given : v0 = 2,g=9.81.

p20:[x0=0,y0=0,v0=3,g=9.81];

p20a:lratsubst(p20,p19a);

p20b:float(p20a);

rad :  maximizes,above values.

p20c:find_root(rhs(p20b),θ,0.001,%pi/2);

deg : maximizes (°),above values

deg :180/%pi;

p20d:p20c*deg;

p20e:float(p20d);

wxdraw2d(explicit(rhs(p20b),θ,0,%pi/2));

see : fig  , change sign  , around value = 0.8 corresponding with 'p20c'
rem : pos. sign  => neg.sign, root  = 'p20c'  : x 'value'