python - Convert Value History to Plot in Swift 3 -
i have pretty simple coupled bvp system solve , plot. made simple, powerful python code. however, swift doesn't so hell trying integrate swift , gave (if has proven solution such integration, ears). c++ easier integrate has ode solvers in odeint. decided own little algorithm in swift, isn't bad in terms of calculations. problem converting value history plot. after have tried searching, nothing useful seems show up. @ moment, have in playground , part under "// display final solution in history" shows plots when click value histories of x , v there.
i need produce graph python did me.
import uikit typealias solver = (double, double, double) -> void struct { var th1 = 0.0 var tc2 = 0.0 var deltat = 0.0 init(th1: double, tc2: double, deltat: double) { self.th1 = th1 self.tc2 = tc2 self.deltat = deltat } func solveusingsolver(solver: solver) { solver(th1, tc2, deltat) } } func solveeuler(th1: double, tc2: double, t : double) { var x = th1 var = 0.0 var upper = th1 var lower = tc2 var vold = (lower + upper) / 2 var v = vold var xold = x let epsilon = 0.05 var err = 1.0 var xarray: [double] = [0,1,2,3,4,5,6,7,8,9,10] var varray: [double] = [0,1,2,3,4,5,6,7,8,9,10] var j=0 while (err > epsilon) { while < 1 { v -= 4*t*i*(xold-vold) x -= t*i*(xold-vold) xold = x vold = v i+=t xarray[j]=x varray[j]=v j += 1 } err = abs(varray[10] - tc2) if varray[10] > tc2 { upper = varray[0] } else { lower = varray[0] } x = th1 xold = x vold = (lower + upper) / 2 v = vold i=0.0 j=0 } print(xarray) print(varray) // display final solution in history while < 1 { v -= 4*t*i*(xold-vold) x -= t*i*(xold-vold) xold = x vold = v i+=t xarray[j]=x varray[j]=v j += 1 } } let prob = he(th1: 100.0, tc2: 25.0, deltat: 0.1) prob.solveusingsolver(solver:solveeuler)
if you're curious, it's solving t1 = 100, t2 = 25, h1 = zeta, h2 = 4*zeta
Comments
Post a Comment