CoCalc Shared Files7 / 7_1.sagews
Author: Georgy Lazarev
Views : 7
var('t y')
f(t,y)=y/(t+1)+(t+1)*exp(t)
y_0=1
t0=0
T=1
h=0.1
n=(T-t0)/h
n
def eyler(h):
n=abs(T-t0)/h
y=y_0
e=[]
for i in range(n):
y=(y+h*f(t0+i*h,y)).n()
e.append(y)
return e
eyler(0.1)
def rkfixed(h):
n=(T-t0)/h
y=y_0
t=t0
a=[]
for i in range(n):
k1=f(t,y)*h
k2=f(t+h/2,y+k1/2)*h
k3=f(t+h/2,y+k2/2)*h
k4=f(t+h,y+k3)*h
y=(y+(k1+2*k2+2*k3+k4)/6).n()
a.append(y)
t=t+h
return a
t=t0
th=[]
th2=[]
for i in range(n):
t=t+0.1
th.append(exp(t)*(t+1))
t=t0
for i in range(n):
t=t+0.2
th2.append(exp(t)*(t+1))
#plot(exp(t)*(t+1),(t,0,1))

e=copy(eyler(0.1))
e2=copy(eyler(0.2))
r=copy(rkfixed(0.1))
r2=copy(rkfixed(0.2))
th
th2
e
e2

print("Погрешность метода Элера, первый способ")
c=[]
for i in range(len(e)):
c.append(abs(e[i]-th[i]))
max(c)

print("Погрешность метода Рунге-Кутты, первый способ")
d=[]

for i in range(n):
d.append(abs(r[i]-th[i]))
max(d)

print("Погрешность метода Эйлера по правилу Рунге")
p=1
f=[]
n/2
for i in range(n/2):
f.append(abs((e[2*i]-e2[i])/(2^p-1)))
f
eyler(0.1)
print("Погрешность метода Рунге_Кутты по правилу Рунге")
g=[]
p=4
for i in range(n/2):
g.append(abs(r[2*i]-r2[i])/(2^p-1))
g
def theor(h):
th=[]
n=(abs(T-t0))/h
for i in range(n):
t=t+h
th.append(exp(t)*(t+1))
return th
def eyler_err(h):
e=copy(eyler(h))
c=[]
th=copy(theor(h))
for i in range(len(e)):
c.append(abs(e[i]-th[i]))
return max(c)
eyler(0.1)
h=0.1
k=0

#while (k<10):
#k+=1
#k
#eyler_err(h)
#a
#if abs(a)<rker:
#break
#else:
#h=h/2

(t, y) 10.0000000000000 [1.20000000000000, 1.43065971007923, 1.69644968356505, 2.00242745805494, 2.35431344844293, 2.75857586894414, 3.22252586881563, 3.75442417431006, 4.36360066223371, 5.06058844610267] [1.21568800988321, 1.46568330979220, 1.75481644984880, 2.08855457669778, 2.47308190605019, 2.91539008062481, 3.42337960269981, 4.00597367128644, 4.67324591119820, 5.43656365691809] [1.46568330979220, 2.08855457669778, 2.91539008062481, 4.00597367128644, 5.43656365691809, 7.30425723002040, 9.73247992042722, 12.8778843034273, 16.9390129003562, 22.1671682967919] [1.20000000000000, 1.43065971007923, 1.69644968356505, 2.00242745805494, 2.35431344844293, 2.75857586894414, 3.22252586881563, 3.75442417431006, 4.36360066223371, 5.06058844610267] [1.40000000000000, 1.92646999529177, 2.61939090995873, 3.52989278982853, 4.72329783406677] Погрешность метода Элера, первый способ 0.375975210815418 Погрешность метода Рунге-Кутты, первый способ 2.82251300287584e-6 Погрешность метода Эйлера по правилу Рунге 5.00000000000000 [0.200000000000000, 0.230020311726721, 0.265077461515800, 0.307366921012899, 0.359697171833058]
Error in lines 68-68 Traceback (most recent call last): File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "", line 6, in eyler TypeError: 'list' object is not callable