方程式求根

Symbolic Root Finding Approach

  • Performing mathematics on symbols, NOT numbers
  • The symbols math are performed using “symbolic variables”
  • use sym or syms to create symbolic variables

就是利用sym来创建一个未知数x

1
2
3
syms x
x + x + x %就是3x
(x + x + x)/4 %就是(3/4)x

或者

1
2
3
x = sym('x');
x + x + x
(x + x + x)/4

同时用未知数去定义一个量,那个量也变为未知数

例如:

1
y = x^2 - 2*x -8

Symbolic Root Finding: solve()

  • Function ‘solve’ finds roots for equations

对于:y = x*sin(x) - x = 0

1
2
syms x
solve('x*sin(x)-x', x)
1
2
3
syms x
y = x*sin(x)-x;
solve(y, x)

Solving Multiple Equations

  • solve this equation using symbolic approach:

{

x - 2y = 5

x + y = 6

}

1
2
3
4
5
6
syms x y
eq1 = x - 2*y - 5;
eq2 = x + y - 6;
A = solve(eq1, eq2, x, y);
A.x
A.y

Solving Equations Expressed in Symbols

  • what if we are given a function expressed in symbols?

a*x^2 - b = 0

1
2
syms x a b
solve('a*x^2-b')
  • x is always the first choice to be solved
  • what if one wants to express b in terms of a and x?
1
2
syms x a b
solve('a*x^2-b','b')

Symbolic Differentiation微分: diff()

  • Calculate the derivative of a symbolic function:

y = 4*x^5

1
2
3
syms x 
y = 4*x^5
yprime = diff(y)

Symbolic Integration积分: int()

1

1
2
3
4
syms x;
y = x^2*exp(x);
z = int(y);
z = z-subs(z, x, 0)

fsolve()

  • A numeric root solver
  • For example, solve this equation:

f(x) = 1.2x + 0.3 + x*sin(x)

1
2
f2 = @(x)  (1.2*x+0.3+x*sin(x));
fsolve(f2, 0)

其中fsolve的第二个参数是对方程成立的x的大概范围猜测,上面例子猜测为0

fzero()

  • Another numeric root solver
  • Find the zero if and only if the function crosses the x-axis
1
2
f = @(x) x.^2;
fzero(f, 0.1) % fzero解不出来 因为x^2 没有穿过x轴 但是fsolve(f, 0)能解出来
1
2
f = @(x) x;
fzero(f, 0.1) %能解出来

fzero第二个参数也类似fsolve

Finding Roots of Polynomials: roots()

  • find the roots of the polynomial
  • roots only works for polynomial
1
roots([1 -6 -12 81])  % f(x) = x^3-6*x^2-12*x+81

求解root 两种原理:

Bisection 二分法

Newton-Raphson Method(Open) 牛顿法

Assumption:

  • f(x) continusous
  • f`(x) known

1

1

Donate? comment?