g=9.8;                 ## m/sec^2
vd=35;                 ## m/sec
D=5;                   ## m/sec
S0pm=4e-4;             ## 1/m
B2pm=4e-4;             ## 1/sec

theta=5;               ## angle of throw -- degrees
phi=0;                 ## angle of the axis of rotation -- degrees
theta*=pi/180;         ## rad
phi*=pi/180;           ## rad

rate=1800;             ## rpm
omega=2*pi*rate/60;    ## rad/sec

v0=140;                ## kmph
v0*=1000/3600;         ## m/sec
dt=0.01;

vx=v0*cos(theta); vy=0; vz=v0*sin(theta);
x=0; y=0; z=1.5;
n=1;

## && --> Logical AND operator : We want both conditions to be satisfied

while ( (x(n)<20) && z(n)>0 )

  v=sqrt(vx(n)^2+vy(n)^2+vz(n)^2);

  x=[x;x(n)+vx(n)*dt];
  y=[y;y(n)+vy(n)*dt];
  z=[z;z(n)+vz(n)*dt];
  vx=[vx;vx(n)-B2pm*v*vx(n)*dt+S0pm*omega*(vz(n)*sin(phi)-vy(n)*cos(phi))*dt];
  vy=[vy;vy(n)-B2pm*v*vy(n)*dt+S0pm*omega*vx(n)*cos(phi)*dt];
  vz=[vz;vz(n)-g*dt-B2pm*v*vz(n)*dt-S0pm*omega*vx(n)*sin(phi)*dt];
  n++;

endwhile

plot(x,y,';Horizontal deflection;',x,z,';Vertical deflection;');
total_time=n*dt;

printf("The total time of flight is %3.2f sec and the lateral displacement due to the Magnus force is about %3.2f m.\n",total_time,abs(y(n)));
