Vorlesung 3

Transcription

Vorlesung 3
19.12.12
Vorlesung 3 -- Sage
admin Toggle Home Published Log Settings Help Report a Problem
Sign out
The Sage
Notebook
Version 5.3
Vorlesung 3
Save
Save & quit
Discard & quit
last edited Dec 19, 2012 11:26:21 AM by admin
File...
Action...
Data...
sage
Typeset
Print Worksheet Edit Text Revisions Share Publish
# Ableitungen
x = var('x')
diff(sin(x^2),x)
2*x*cos(x^2)
y= var('y')
f=x^2+17*y^2
f
x^2 + 17*y^2
f.diff(x)
2*x
f
x^2 + 17*y^2
diff(f,y,2)
34
g=f.diff(y,3)
g
0
g(2)
68
diff(sin(x^2),x,4)
16*x^4*sin(x^2) - 48*x^2*cos(x^2) - 12*sin(x^2)
f=x*sin(x^2)
q=integral(f,x, 0, pi/2)
q.n()
0.688379183887200
localhost:8080/home/admin/23/
1/7
19.12.12
Vorlesung 3 -- Sage
integral?
f=1/((1+x)*(x-1))
g=f.partial_fraction(x)
g
1/2/(x - 1) - 1/2/(x + 1)
g(2)
1/3
def hanoi(n,start,ziel,hilf):
if (n>0):
hanoi(n-1,start,hilf,ziel) # Turm(n-1) von start auf hilf verschieben
print n, 'von', start, 'nach', ziel
hanoi(n-1, hilf, ziel, start)
hanoi(3, 'links', 'rechts', 'mitte')
1 von links nach rechts
2 von links nach mitte
1 von rechts nach mitte
3 von links nach rechts
1 von mitte nach links
2 von mitte nach rechts
1 von links nach rechts
def monte_carlo(n):
print "Monte-Carlo Methode zur"
print "zur Näherung von pi"
v=0 # Zähler
for i in range(1,n+1):
x=random() # (0,1)verteilte ZV
y=random()
if (x^2+y^2 <=1):
v=v+1
pi_naeh=(4* (v/n)).n()
print "Die Näherung beträgt: "
print pi_naeh
localhost:8080/home/admin/23/
2/7
19.12.12
Vorlesung 3 -- Sage
pi.n()
3.14159265358979
monte_carlo(1000000)
Monte-Carlo Methode zur
zur Näherung von pi
Die Näherung beträgt:
3.14351600000000
t=var('t')
x=function('x',t)
DE = diff(x,t) +x
DE
x(t) + D[0](x)(t)
desolve(DE,[x,t])
c*e^(-t)
DE=diff(x,t) - x -1
DE
-x(t) + D[0](x)(t) - 1
desolve(DE,[x,t])
(c - e^(-t))*e^t
sol=desolve(DE, [x,t], ics=[0,1])
plot(sol)
localhost:8080/home/admin/23/
3/7
19.12.12
Vorlesung 3 -- Sage
def euler(f,a,b, n, x0):
x=x0;
t=a #Zeit, erster betrachter Wert im Intervall [a,b]
h=(b-a)/n
print ' t
x(t) '
print '____________________'
for i in range(1,n+1):
print "%10f %10f" % (t,x)
x=x+h*f(t,x)
t=t+h
print "%10f %10f" % (t,x)
f(t,x)=x+1
euler(f,-1,1,20,-0.1)
t
x(t)
____________________
-1.000000 -0.100000
-0.900000 -0.010000
-0.800000 0.089000
-0.700000 0.197900
-0.600000 0.317690
-0.500000 0.449459
-0.400000 0.594405
-0.300000 0.753845
-0.200000 0.929230
-0.100000 1.122153
0.000000 1.334368
0.100000 1.567805
0.200000 1.824586
0.300000 2.107044
0.400000 2.417749
0.500000 2.759523
localhost:8080/home/admin/23/
4/7
19.12.12
Vorlesung 3 -- Sage
0.600000
0.700000
0.800000
0.900000
1.000000
3.135476
3.549023
4.003926
4.504318
5.054750
def euler_liste(f,a,b, n, x0):
x=x0;
t=a #Zeit, erster betrachter Wert im Intervall [a,b]
h=(b-a)/n
L=[(t,x)]
for i in range(1,n+1):
x=(x+h*f(t,x)).n()
t=(t+h).n()
L=L+[(t,x)]
return L
list=euler_liste(f,-1,1,20,-0.1)
list
[(-1, -0.100000000000000), (-0.900000000000000, -0.0100000000000000),
(-0.800000000000000, 0.0890000000000000), (-0.700000000000000,
0.197900000000000), (-0.600000000000000, 0.317690000000000),
(-0.500000000000000, 0.449459000000000), (-0.400000000000000,
0.594404900000000), (-0.300000000000000, 0.753845390000000),
(-0.200000000000000, 0.929229929000000), (-0.100000000000000,
1.12215292190000), (-1.38777878078145e-16, 1.33436821409000),
(0.0999999999999999, 1.56780503549900), (0.200000000000000,
1.82458553904890), (0.300000000000000, 2.10704409295379),
(0.400000000000000, 2.41774850224917), (0.500000000000000,
2.75952335247409), (0.600000000000000, 3.13547568772150),
(0.700000000000000, 3.54902325649365), (0.800000000000000,
4.00392558214301), (0.900000000000000, 4.50431814035731),
(1.00000000000000, 5.05474995439304)]
list[0]
(-1, -0.100000000000000)
list[1]
(-0.900000000000000, -0.0100000000000000)
list[len(list)-1]
(1.00000000000000, 5.05474995439304)
#Weg plotten
graph=line([list[0],list[1]])
for i in range(1,len(list)-1):
graph=graph+line([list[i],list[i+1]])
localhost:8080/home/admin/23/
5/7
19.12.12
Vorlesung 3 -- Sage
graph
localhost:8080/home/admin/23/
6/7
19.12.12
localhost:8080/home/admin/23/
Vorlesung 3 -- Sage
7/7

Documents pareils