Hi everyone, here is my code :
class Variables : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.variables)
fun main(args: Array<Double>) {
fun rungeKutta4(t0: Double, tp: Double, h: Double, f: F, g: G, z: Z) {
var t = t0
var x = 0.0
var xp = 0.0
var y = 0.0
var yp = 0.0
var xna = 0.0
var xnap = 0.0
val p = ((tp-t0)/h).toInt()
**val tv = mutableListOf(t)**
** val xv = mutableListOf(x)**
** val xnav = mutableListOf(xna)**
** val yv = mutableListOf(y)**
for (i in 0..p) {
if (i == p) break
val K1 = f(t,x,xp,y,yp)
val L1 = g(x,xp,y,yp)
val M1 = z(t,xna,xnap)
val Kx = h*xp/2.0 + h*h*K1/8.0
val Ky = h*yp/2.0 + h*h*L1/8.0
val Kxna = h*xna/2.0 + h*h*M1/8.0
val K2 = f(t+h/2.0,x+Kx,xp+h*K1/2.0,y+Ky,yp+h*L1/2.0)
val L2 = g(x+Kx,xp+h*K1/2.0,y+Ky,yp+h*L1/2.0)
val M2 = z(t+h/2.0,xna+Kxna,xnap+h*M1/2.0)
val K3 = f(t+h/2.0,x+Kx,xp+h*K2/2.0,y+Ky,yp+h*L2/2.0)
val L3 = g(x+Kx,xp+h*K2/2.0,y+Ky,yp+h*L2/2.0)
val M3 = z(t+h/2.0,xna+Kxna,xnap+h*M2/2.0)
val Lx = h*xp + h*h*K3/2.0
val Ly = h*yp + h*h*L3/2.0
val Lxna = h*xnap + h*h*M3/2.0
val K4 = f(t+h,x+Lx,xp+h*K3,y+Ly,yp+h*L3)
val L4 = g(x+Lx,xp+h*K3,y+Ly,yp+h*L3)
val M4 = z(t+h,xna+Lxna,xnap+h*M3)
x += h*xp + h*h*(K1+K2+K3)/6.0
xp += h*(K1+2.0*K2+2.0*K3+K4)/6.0
y += h*yp + h*h*(L1+L2+L3)/6.0
yp += h*(L1+2.0*L2+2.0*L3+L4)/6.0
xna += h*xnap + h*h*(M1+M2+M3)/6.0
xnap += h*(M1+2.0*M2+2.0*M3+M4)/6.0
t += h
tv.add(t)
xv.add(x)
xnav.add(xna)
yv.add(y)
}
}
val F = intent.getStringExtra("F").toDouble()
val m1 = intent.getStringExtra("m1").toDouble()
val m2 = intent.getStringExtra("m2").toDouble()
val k1 = intent.getStringExtra("k1").toDouble()
val k3 = intent.getStringExtra("k3").toDouble()
val c1 = intent.getStringExtra("c1").toDouble()
val c2 = intent.getStringExtra("c2").toDouble()
val f = fun(t: Double, x: Double, xp: Double, y: Double, yp: Double) = (1.0/m1)*(F * cos(sqrt(k1/m1)*t) -k1*x-k3*(x-y)-c1*xp-c2*(xp-yp))
val g = fun(x: Double, xp: Double, y: Double, yp: Double) = (1.0/m2)*(k3*(x-y)+c2*(xp-yp))
val z = fun(t: Double, xna: Double, xnap: Double) = (1.0/m1)*(F* cos(sqrt(k1/m1) *t) -k1*xna-c1*xnap)
rungeKutta4(0.0, 10.0, 0.1, f, g, z)
}
**I want to extract the variables tv, xv, xnav, yv (in bold in the code) from the function rungeKutta4 in order to use them in another function, how can I proceed ? **
Thanks for your help
Samet