# 基础代数与微积分¶

Sage可以执行与基本代数和微积分相关的各种计算：例如，求解方程、微分、积分和拉普拉斯变换。见 Sage Constructions 更多示例的文档。

sage: u = var('u')
sage: diff(sin(u), u)
cos(u)


## 解方程¶

### 精确解方程¶

sage: x = var('x')
sage: solve(x^2 + 3*x + 2, x)
[x == -2, x == -1]


sage: x, b, c = var('x b c')
sage: solve([x^2 + b*x + c == 0],x)
[x == -1/2*b - 1/2*sqrt(b^2 - 4*c), x == -1/2*b + 1/2*sqrt(b^2 - 4*c)]


sage: x, y = var('x, y')
sage: solve([x+y==6, x-y==4], x, y)
[[x == 5, y == 1]]


sage: var('x y p q')
(x, y, p, q)
sage: eq1 = p+q==9
sage: eq2 = q*y+p*x==-6
sage: eq3 = q*y^2+p*x^2==24
sage: solve([eq1,eq2,eq3,p==1],p,q,x,y)
[[p == 1, q == 8, x == -4/3*sqrt(10) - 2/3, y == 1/6*sqrt(10) - 2/3], [p == 1, q == 8, x == 4/3*sqrt(10) - 2/3, y == -1/6*sqrt(10) - 2/3]]


sage: solns = solve([eq1,eq2,eq3,p==1],p,q,x,y, solution_dict=True)
sage: [[s[p].n(30), s[q].n(30), s[x].n(30), s[y].n(30)] for s in solns]
[[1.0000000, 8.0000000, -4.8830369, -0.13962039],
[1.0000000, 8.0000000, 3.5497035, -1.1937129]]


（功能 n 打印数值近似值，参数是精度的位数。）

### 数值求解方程¶

sage: theta = var('theta')
sage: solve(cos(theta)==sin(theta), theta)
[sin(theta) == cos(theta)]


sage: phi = var('phi')
sage: find_root(cos(phi)==sin(phi),0,pi/2)
0.785398163397448...


## 分化、整合等。¶

Sage知道如何区分和整合许多功能。例如，区分 $$\sin(u)$$ 关于 $$u$$ ，请执行以下操作：

sage: u = var('u')
sage: diff(sin(u), u)
cos(u)


sage: diff(sin(x^2), x, 4)
16*x^4*sin(x^2) - 48*x^2*cos(x^2) - 12*sin(x^2)


sage: x, y = var('x,y')
sage: f = x^2 + 17*y^2
sage: f.diff(x)
2*x
sage: f.diff(y)
34*y


sage: integral(x*sin(x^2), x)
-1/2*cos(x^2)
sage: integral(x/(x^2+1), x, 0, 1)
1/2*log(2)


sage: f = 1/((1+x)*(x-1))
sage: f.partial_fraction(x)
-1/2/(x + 1) + 1/2/(x - 1)


## 解微分方程¶

sage: t = var('t')    # define a variable t
sage: x = function('x')(t)   # define x to be a function of that variable
sage: DE = diff(x, t) + x - 1
sage: desolve(DE, [x,t])
(_C + e^t)*e^(-t)


sage: s = var("s")
sage: t = var("t")
sage: f = t^2*exp(t) - sin(t)
sage: f.laplace(t,s)
-1/(s^2 + 1) + 2/(s - 1)^3


|------\/\/\/\/\---|mass1|----\/\/\/\/\/----|mass2|
spring1               spring2


$m_1 x_1''+（k_1+k_2）x 1-k_2 x 2=0 m_2 x 2''+k_2（x_2-x_1）=0，$

sage: de1 = maxima("2*diff(x(t),t, 2) + 6*x(t) - 2*y(t)")
sage: lde1 = de1.laplace("t","s"); lde1
2*((-%at('diff(x(t),t,1),t=0))+s^2*'laplace(x(t),t,s)-x(0)*s)-2*'laplace(y(t),t,s)+6*'laplace(x(t),t,s)


$-2x'（0）+2s^2cdot X（s）-2sx（0）-2Y（s）+6X（s）=0$

（其中小写函数的拉普拉斯变换 $$x(t)$$ 是大写函数 $$X(s)$$ ). 取第二个方程的拉普拉斯变换：

sage: de2 = maxima("diff(y(t),t, 2) + 2*y(t) - 2*x(t)")
sage: lde2 = de2.laplace("t","s"); lde2
(-%at('diff(y(t),t,1),t=0))+s^2*'laplace(y(t),t,s)+2*'laplace(y(t),t,s)-2*'laplace(x(t),t,s)-y(0)*s


$-Y'（0）+s^2Y（s）+2Y（s）-2X（s）-sy（0）=0。$

sage: var('s X Y')
(s, X, Y)
sage: eqns = [(2*s^2+6)*X-2*Y == 6*s, -2*X +(s^2+2)*Y == 3*s]
sage: solve(eqns, X,Y)
[[X == 3*(s^3 + 3*s)/(s^4 + 5*s^2 + 4),
Y == 3*(s^3 + 5*s)/(s^4 + 5*s^2 + 4)]]


sage: var('s t')
(s, t)
sage: inverse_laplace((3*s^3 + 9*s)/(s^4 + 5*s^2 + 4),s,t)
cos(2*t) + 2*cos(t)
sage: inverse_laplace((3*s^3 + 15*s)/(s^4 + 5*s^2 + 4),s,t)
-cos(2*t) + 4*cos(t)


$x_1（t）=cos（2t）+2cos（t），quad x_2（t）=4cos（t）-cos（2t）。$

sage: t = var('t')
sage: P = parametric_plot((cos(2*t) + 2*cos(t), 4*cos(t) - cos(2*t) ),
....:     (t, 0, 2*pi), rgbcolor=hue(0.9))
sage: show(P)


sage: t = var('t')
sage: p1 = plot(cos(2*t) + 2*cos(t), (t,0, 2*pi), rgbcolor=hue(0.3))
sage: p2 = plot(4*cos(t) - cos(2*t), (t,0, 2*pi), rgbcolor=hue(0.6))
sage: show(p1 + p2)


## 微分方程组的欧拉方法¶

$y'=f（x，y），quad y（a）=c，$

$y'（x）近似分形{y（x+h）-y（x）}{h}，$

$y（x+h）近似y（x）+hcdot f（x，y（x））。$

$y{new}近似y{old}+hcdot f（x，y{old}）。$

$$x$$

$$y$$

$$h\cdot f(x,y)$$

$$a$$

$$c$$

$$h\cdot f(a,c)$$

$$a+h$$

$$c+h\cdot f(a,c)$$

$$a+2h$$

$$b=a+nh$$

？？？？

《诗经》的理念与此相似。

sage: t,x,y = PolynomialRing(RealField(10),3,"txy").gens()
sage: f = y; g = -x - y * t
sage: eulers_method_2x2(f,g, 0, 1, 0, 1/4, 1)
t                x            h*f(t,x,y)                y       h*g(t,x,y)
0                1                  0.00                0           -0.25
1/4              1.0                -0.062            -0.25           -0.23
1/2             0.94                 -0.12            -0.48           -0.17
3/4             0.82                 -0.16            -0.66          -0.081
1             0.65                 -0.18            -0.74           0.022


sage: f = lambda z: z[2]        # f(t,x,y) = y
sage: g = lambda z: -sin(z[1])  # g(t,x,y) = -sin(x)
sage: P = eulers_method_2x2_plot(f,g, 0.0, 0.75, 0.0, 0.1, 1.0)


sage: show(P[0] + P[1])


（有关打印的详细信息，请参见 作图

## 特殊功能¶

sage: x = polygen(QQ, 'x')
sage: chebyshev_U(2,x)
4*x^2 - 1
sage: bessel_I(1,1).n(250)
0.56515910399248502720769602760986330732889962162109200948029448947925564096
sage: bessel_I(1,1).n()
0.565159103992485
sage: bessel_I(2,1.1).n()
0.167089499251049


sage: maxima.eval("f:bessel_y(v, w)")
'bessel_y(v,w)'
sage: maxima.eval("diff(f,w)")
'(bessel_y(v-1,w)-bessel_y(v+1,w))/2'