[Tutor] hi
Oscar Benjamin
oscar.j.benjamin at gmail.com
Fri Aug 9 20:05:01 CEST 2013
Can you respond inline and trim the part you're not quoting instead of
top-posting please?
On 8 August 2013 18:22, Vick <vick1975 at orange.mu> wrote:
> Hi,
>
> Thanks !
>
> What is the best numerical ODE that you have coded then?
What do you mean by best? There are different ODE solvers for
different problems.
> Can you solve for
> the following equation and tell me the error it gives:
>
> N(ODE) = (4 * (exp(1) ** (0.8 * t))) - (0.5 * x) for f(t , x)
>
> The true value should be : TF = (4/1.3)*EXP(0.8*t)-(1.4/1.3)*EXP(-0.5*t)
You haven't given the initial condition but I guess it should be that
x=2 at t=0 from the solution.
> The error after your numerical ODE should be Er = ABS((TF-N(ODE))/TF)*100
Why are you multiplying by 100? That doesn't make any sense.
I don't see what's so special about this ODE that you wouldn't just
use scipy's odeint:
#!/usr/bin/env python
# solve.py
from math import exp
from scipy.integrate import odeint
# System derivative
def f(x, t):
return 4 * exp(0.8 * t) - 0.5 * x
# Analytic solution
def analytic(t):
return (40/13.)*exp(0.8*t) - (14/13.)*exp(-0.5*t)
# Initial condition
x0 = [2]
# Time through which to integrate (from t=0 to t=T)
T = 4
xtrue = analytic(T)
for tol in 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15:
xest = odeint(f, x0, [0, T], rtol=tol, atol=tol)[-1]
relerror = abs((xest - xtrue)/xtrue)
print('Tolerance: %s Error: %.3e' % (tol, relerror))
Output:
$ ./solve.py
Tolerance: 1e-08 Error: 2.066e-09
Tolerance: 1e-09 Error: 1.623e-10
Tolerance: 1e-10 Error: 2.707e-11
Tolerance: 1e-11 Error: 1.818e-12
Tolerance: 1e-12 Error: 7.281e-13
Tolerance: 1e-13 Error: 7.620e-14
lsoda-- at start of problem, too much accuracy
requested for precision of machine.. see tolsf (=r1)
In above message, R1 = 0.2960594732334E+01
Illegal input detected (internal error).
Run with full_output = 1 to get quantitative information.
Tolerance: 1e-14 Error: 9.735e-01
lsoda-- at start of problem, too much accuracy
requested for precision of machine.. see tolsf (=r1)
In above message, R1 = 0.2960594732334E+02
Illegal input detected (internal error).
Run with full_output = 1 to get quantitative information.
Tolerance: 1e-15 Error: 9.735e-01
So you can get a relative error of 1e-14 with this. Is that good enough?
Oscar
More information about the Tutor
mailing list