ode

201 days ago by thomaskuenzer

def f(t,x): return x 
       
def Euler(f,t0,x0,h,T): xxx=[x0] t=t0 for i in range(0,floor((T-t0)/h)): xxx.append(xxx[i]+f(t,xxx[i])*h) t+=h return xxx def RungeKutta(f,t0,x0,h,T): xxx=[x0] t=t0 for i in range(0,floor((T-t0)/h)): k1=f(t,xxx[i]) k2=f(t+h/2,xxx[i]+h/2*k1) k3=f(t+h/2,xxx[i]+h/2*k2) k4=f(t+h,xxx[i]+h*k3) xxx.append(xxx[i]+h/6*(k1+2*k2+2*k3+k4)) t+=h return xxx def Funktion(xh,h,t0,t): u=floor((t-t0)/h) return xh[u] 
       
approx=RungeKutta(f,0,1,10^-1,12) 
       
A=plot(Funktion(approx,10^-1,0,t),t,0,10) A.show() 
       
Traceback (click to the left of this block for traceback)
...
TypeError: unable to convert x (=floor(10*t)) to an integer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_32.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("QT1wbG90KEZ1bmt0aW9uKGFwcHJveCwxMF4tMSwwLHQpLHQsMCwxMCkKQS5zaG93KCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpGft2cy/___code___.py", line 3, in <module>
    A=plot(Funktion(approx,_sage_const_10 **-_sage_const_1 ,_sage_const_0 ,t),t,_sage_const_0 ,_sage_const_10 )
  File "/tmp/tmpC1SZr9/___code___.py", line 26, in Funktion
    return xh[u]
  File "expression.pyx", line 3905, in sage.symbolic.expression.Expression.__index__ (sage/symbolic/expression.cpp:17306)
  File "expression.pyx", line 719, in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:4400)
TypeError: unable to convert x (=floor(10*t)) to an integer
Funktion(approx,10^-1,0,3).n() 
       
20.0854907196649
20.0854907196649