= #JxnPortable\docs\programmer_examples\commons_math_examples\CommonsMath_ODE_WurfReibung.jxn

#ifndef owner
owner = @java.awt.Frame( "Wurfparabel | Balistic Trajectory" )
#endif
#JXN:include/SliderInclude.jxn

tmax0 = slider.add( "tmax / s", 0., 0., 10. )
g  = slider.add( "g / m/s\#178;",  10, 0, 20 )
h0 = slider.add( "ho / m", 35, 0, 150 )
v0 = slider.add( "vo / m/s", 28.28, 0, 100 )
alp0 = slider.add( "\#945;o / \#176;", 45, -90, 90 )
c  = slider.add( "c ", 0., 0., 0.1 )  ! Newton drag ~v^2
d  = slider.add( "d ", 0., 0., 2.5 )  ! Stokes drag ~v

v0x = v0 cosD alp0
v0y = v0 sinD alp0

! 0 = h0 + v0y tmax - 0.5 g tmax^2
p = -2 v0y / g
q = -2 h0  / g
! TODO: Behandlung g == 0
! tmax = sw( tmax0, tmax0, max( 1., -p/2 + sqrt max( 0., p^2/4 - q ) ), tmax0 ) 
! tmax = max( 1., sw( tmax0, tmax0, -p/2 + sqrt max( 0., p^2/4 - q ), tmax0 ) ) 
tmax = max( 1., sw( tmax0, tmax0, -p/2 + max( -p/2, sqrt max( 0., p^2/4 - q ) ), tmax0 ) ) 

tt = t tmax
x =      v0x tt
y = h0 + v0y tt - 0.5 g tt^2

ti = @JxnRealArrayAlgebra( 0, floor tmax, 1. )
xi =      v0x ti
yi = h0 + v0y ti - 0.5 g ti^2

txt = @JxnTextPainter( round ti[1:] + "s", xi[1:], yi[1:] ).setAnchor( KmgGraphicsScaler.NORTHEAST ).setSpace(0.2).setBackground()


#import org.apache.commons.math3.ode
#import org.apache.commons.math3.ode.nonstiff

dp853 = @DormandPrince853Integrator( 1.E-8, 100., 0.0001, 0.0001 )
com = @ContinuousOutputModel()
dp853.addStepHandler( com )

! define the ODEs
init = "cv = c sqrt( $y[2]^2 + $y[3]^2 ) + d;"
xdot = "$y[2];"
ydot = "$y[3];"
vxdot = "-cv $y[2];" 
vydot = "-cv $y[3] - g;"
ode = @JxnODE( $this, init, { xdot, ydot, vxdot, vydot }, null )

t0 = 0.
tend = tmax
y0 = { 0., h0, v0x, v0y }
yy = { 0., 0., 0.,  0. }
dp853.integrate( ode, t0, y0, tend, yy )

! retrieve output
t_ = tend { 0 : 1000 } / 1000
xt = JxnCOMAnalyzer.analyze( com, t_, 0 )
yt = JxnCOMAnalyzer.analyze( com, t_, 1 )
tj = ti.toArray()
xj = JxnCOMAnalyzer.analyze( com, tj, 0 )
yj = JxnCOMAnalyzer.analyze( com, tj, 1 )


pf1 = plot( xi, yi, R, 11, 1. ).add( x, y ).add( txt )  ! .setXYLabels( "m", "m" )
pf1.add( curve( xj, yj, B, 11 ) ).add( curve( xt, yt, B ) )
pf1.add @JxnTextPainter( round tj[1:] + "s", xj[1:], yj[1:] ).setAnchor( KmgGraphicsScaler.NORTHEAST ).setSpace(0.2).setBackground()
pf1.add( phasor( 5 + 0j, h0 j, "ho", B ) ).add( phasor( h0 j, @JxnComplexAlgebra( v0, alp0, true ), "vo", G ) )
pf1.add( angle( h0 j, 7, 0, alp0, "\#945;o", G ) ).add( @JxnGraphicsPainter( 0, 0, h0, 12, h0 ).setColor( G ) )
pf1.setAutoScale(1)
!
#JXN:include/Center1Include.jxn
#JXN:include/FrameInclude.jxn