Finding extrema of a function

In [4]:
x, y = var('x y')
f = y*(x-1)*exp(-x^2-y^2)
f.show()
P = plot3d(f,(x,-2,2),(y,-2,2))
P.show()
In [10]:
g=f.gradient()
g[0].full_simplify().show()
g[1].show()
plot_vector_field(f.gradient(), (x,-2,2), (y,-2,2))
Out[10]:
In [ ]:
 
In [11]:
(f.hessian()[0,0]).show()
(f.hessian()[1,0]).show()
(f.hessian()[0,1]).show()
(f.hessian()[1,1]).show()
In [14]:
f.hessian()(x=1,y=0)
Out[14]:
[     0 e^(-1)]
[e^(-1)      0]
In [29]:
(f.hessian())(x=1/2-sqrt(3/2),y=-sqrt(1/2)).n(digits=2) 
Out[29]:
[-0.69  0.00]
[ 0.00  -1.8]
In [30]:
(f.hessian())(x=1/2-sqrt(3/2),y=+sqrt(1/2)).n(digits=2) 
Out[30]:
[0.69 0.00]
[0.00  1.8]
In [31]:
(f.hessian())(x=1/2+sqrt(3/2),y=-sqrt(1/2)).n(digits=2) 
Out[31]:
[-0.0056    0.00]
[   0.00   0.064]
In [32]:
(f.hessian())(x=1/2+sqrt(3/2),y=+sqrt(1/2)).n(digits=2) 
Out[32]:
[0.0056   0.00]
[  0.00 -0.064]

Banach Fixpoint iteration

In [1]:
f = sqrt(2+sqrt(x))
d=diff(f)
d.show()
In [2]:
f(1), f(2)
Out[2]:
(sqrt(3), sqrt(sqrt(2) + 2))
In [3]:
diff(d).show()
In [4]:
d(1)
Out[4]:
1/12*sqrt(3)
In [10]:
v=[(0, sqrt(2))]
for i in range(10):
    i, xx=v[-1]
    v.append( (i+1,f(xx)) )
show(points(v, pointsize=30))

Picard Iteration

In [19]:
from sage.symbolic.integration.integral import indefinite_integral
t,x = var ('t x')
F(t,x) = x^2
v = 1
f_0(t) = 0
f_1(t) = v+indefinite_integral(F(t, f_0(t)), t)
In [20]:
f_2= v+indefinite_integral(F(t, f_1(t)), t)
f_2
Out[20]:
t + 1
In [21]:
f_3= v+indefinite_integral(F(t, f_2(t)), t)
f_3
Out[21]:
1/3*t^3 + t^2 + t + 1
In [22]:
f_4= v+indefinite_integral(F(t, f_3(t)), t)
f_4
Out[22]:
1/63*t^7 + 1/9*t^6 + 1/3*t^5 + 2/3*t^4 + t^3 + t^2 + t + 1
In [23]:
f_5= v+indefinite_integral(F(t, f_4(t)), t)
f_5
Out[23]:
1/59535*t^15 + 1/3969*t^14 + 1/567*t^13 + 1/126*t^12 + 5/189*t^11 + 22/315*t^10 + 86/567*t^9 + 71/252*t^8 + 29/63*t^7 + 2/3*t^6 + 13/15*t^5 + t^4 + t^3 + t^2 + t + 1
In [12]:
iteration = [plot(fff, (-1,1),  ymin=-10, ymax=10) for fff in [f_0,f_0, f_1,f_1, f_2,f_2,f_3,f_3,f_4,f_4,f_5,f_5]]
a = animate(iteration)
a
Out[12]:

1-dimensional ODE plots with vector fields

In [124]:
#cooler with: maxima('plotdf([1,y^2],[x,y],[x,-2,2],[y,-2,2])')
var('x,y')
ode_rhs = y^2
p = plot_slope_field(ode_rhs, (x,-4,4), (y,-5,5), headaxislength=3, headlength=3, color='darkred')
for i in range(-3,3):
    for j in range(-3, 3):
        p=p+desolve_rk4(ode_rhs, y, ivar=x, ics=[i,j],end_points=[-4,4] , output='plot',xmin=-4, xmax=4, ymin=-5, ymax=5, color='blue')
p
Out[124]:
In [123]:
var('x,y')
ode_rhs = x^2
p = plot_slope_field(ode_rhs, (x,-4,4), (y,-5,5), headaxislength=3, headlength=3, color='darkred')
for i in range(-3,3):
    for j in range(-3, 3):
        p=p+desolve_rk4(ode_rhs, y, ivar=x, ics=[i,j],end_points=[-4,4] , output='plot',xmin=-4, xmax=4, ymin=-5, ymax=5, color='blue')
p
Out[123]:
In [135]:
var('x,y')