2021-06-24 MATLAB学习随记——方程求解

方程求解

  • 一、代数方程求解
    • 1. solve
      • 1.1方程求解
      • 1.2方程组求解
        • 1.2.1 S=solve(eqns)
        • 1.2.2 [y1,...,yN]=solve(eqns)
        • 1.2.3 S=solve(eqns,vars,Name,Value)
    • 2. linsolve
    • 3. vpasolve
      • 3.1 S = vpasolve(eqn,var,init_guess)
      • 3.2 Y = vpasolve(eqns,vars,init_guess)
  • 二、微分方程求解(dsolve)
    • 1. 求解常微分方程
    • 2. 求解常微分方程初值问题
    • 3.求解常微分方程组

一、代数方程求解

solve 普通方程求解
linsolve 给定形式矩阵方程求解
vpasolve 精度数值方程求解

1. solve

调用格式

S = solve(eqn) eqn:待求解方程
S=solve(eqn,var,Name,Value) var:待求解变量;NameValue:参数名和参数值
S=solve(eqns) eqns:待求解方程组
S=solve(eqns,vars,Name,Value) vars:待求解变量组;NameValue:参数名和参数值
[y1,...,yN]=solve(eqns)
[y1,...,yN]=solve(eqns,vars,Name,Value)

1.1方程求解

命令行:

clear;
display('单方程求解:a*x^2 + b*x + c ==0');
syms a b c x
xs1 = solve(a*x^2 + b*x + c)
xs2 = solve(a*x^2 + b*x + c == 0)
as = solve(a*x^2 + b*x + c,a)       %以a为自变量求解,b、c同理

输出:

>> Test
单方程求解:a*x^2 + b*x + c ==0 
xs1 =
 -(b + (b^2 - 4*a*c)^(1/2))/(2*a)
 -(b - (b^2 - 4*a*c)^(1/2))/(2*a)
 
xs2 =
 -(b + (b^2 - 4*a*c)^(1/2))/(2*a)
 -(b - (b^2 - 4*a*c)^(1/2))/(2*a)
 
as = -(c + b*x)/x^2

1.2方程组求解

1.2.1 S=solve(eqns)

命令行:

clear;
display('方程组求解:x + y == 1,x - 11*y == 5');
syms x y
S = solve(x + y ==1,x - 11*y ==5);
Ss = [S.x S.y]       %用于显示简化矩阵

输出:

>> Test
方程组求解:x + y == 1,x - 11*y == 5
Ss = [ 4/3, -1/3]

1.2.2 [y1,…,yN]=solve(eqns)

命令行:

clear;
display('方程组求解:a*u^2 + v^2 == 0,u - v == 1,a^2 + 6 == 5*a')
syms a u v
[a,u,v] = solve(a*u^2 + v^2 == 0,u - v == 1,a^2 + 6 == 5*a);
solutions = [a u v]		%得到的解矩阵输出

输出:

>> Test
方程组求解:a*u^2 + v^2 == 0,u - v == 1,a^2 + 6 == 5*a
solutions =
[ 2, 1/3 - (2^(1/2)*1i)/3, - (2^(1/2)*1i)/3 - 2/3]
[ 2, (2^(1/2)*1i)/3 + 1/3,   (2^(1/2)*1i)/3 - 2/3]
[ 3, 1/4 - (3^(1/2)*1i)/4, - (3^(1/2)*1i)/4 - 3/4]
[ 3, (3^(1/2)*1i)/4 + 1/4,   (3^(1/2)*1i)/4 - 3/4]

1.2.3 S=solve(eqns,vars,Name,Value)

命令行:

clear;
display('求解方程x^5 == 3125的根和正实根');
syms x
xall = solve(x^5 == 3125)
xreal = solve(x^5 == 3125,x,'Real',true)

输出:

>> Test
求解方程x^5 == 3125的根和正实根
 
xall =
                                                           5
 - (2^(1/2)*(5 - 5^(1/2))^(1/2)*5i)/4 - (5*5^(1/2))/4 - 5/4
   (2^(1/2)*(5 - 5^(1/2))^(1/2)*5i)/4 - (5*5^(1/2))/4 - 5/4
   (5*5^(1/2))/4 - (2^(1/2)*(5^(1/2) + 5)^(1/2)*5i)/4 - 5/4
   (5*5^(1/2))/4 + (2^(1/2)*(5^(1/2) + 5)^(1/2)*5i)/4 - 5/4
 
xreal = 5

注意变量名不能重复,否则报错

2. linsolve

用于求解矩阵

X = linsolve(A,B) A:输入方程左项矩阵;B:右项列向量或矩阵
[X,R] = linsolve(A,B) X:解,满足AX = B;R:为A的条件数的倒数

命令行:

clear;
syms a x y z;
A = [a 0 0; 0 a 0; 0 0 1];
B = [x;y;z];
[X,R] = linsolve(A,B)

输出:

>> Test
X =
 x/a
 y/a
   z
 
R = 1/(max(abs(a), 1)*max(1/abs(a), 1))

3. vpasolve

用于求解数值方程

S = vpasolve(eqn,var,init_guess) eqn:待求解方程;var:变量
Y = vpasolve(eqns,vars,init_guess) eqns:待求解方程组
[y1,...yN] = vpasolve(eqns,vars,init_guess) init_guess:初始解或求解范围

3.1 S = vpasolve(eqn,var,init_guess)

命令行:

clear;
display('求解方程:sin(x)==x^2 -1');
syms x
nroot = solve(sin(x) == x^2 - 1)
proot = vpasolve(sin(x) == x^2 - 1,x,1.4)

输出:

>> Test
求解方程:sin(x)==x^2 -1
nroot = -0.63673
 
proot = 1.4096

3.2 Y = vpasolve(eqns,vars,init_guess)

命令行:

clear;
display('求解方程组:x^3 + 2*x == y,y^2 == x')
syms x y
digits(5)
S = vpasolve([x^3 + 2*x == y,y^2==x],[x,y])
S = [S.x S.y]		%将结果矩阵输出

输出:

>> Test
求解方程组:x^3 + 2*x == y,y^2 == x

S = 
    x: [6x1 sym]
    y: [6x1 sym]
 
S =
[             0.23657,              0.48639]
[                   0,                    0]
[ - 0.28124 + 1.2349i,    0.70187 + 0.8797i]
[ - 0.28124 - 1.2349i,    0.70187 - 0.8797i]
[   0.16295 - 1.6152i, - 0.94507 + 0.85452i]
[   0.16295 + 1.6152i, - 0.94507 - 0.85452i]

二、微分方程求解(dsolve)

dsolve函数用于常微分方程求解

S = dsolve(eqn) eqn:待求解方程
S = dsolve(eqn,cond) cond:初值条件
Y = dsolve(eqns) eqns:待求解方程组
Y = dsolve(eqns,conds) conds:初值条件
[y1,...yN] = dsolve(eqns)
[y1,...yN] = dsolve(eqns,conds)

1. 求解常微分方程

命令行:

clear;
display('求解常微分方程')
syms a x(t);
dsolve(diff(x) == -a*x)

输出:

>> Test
求解常微分方程
ans = C4*exp(-a*t)

2. 求解常微分方程初值问题

命令行:

clear;
display('求解常微分方程初值问题')
syms a y(t)
Dy = diff(y);
dsolve(diff(y,2) == -a^2*y ,y(0) == 1,Dy(pi/a) == 0)

输出:

>> Test
求解常微分方程初值问题
ans = exp(-a*t*1i)/2 + exp(a*t*1i)/2

这一部分没看懂!!

3.求解常微分方程组

命令行:

clear;
display('求解常微分方程组')
syms x(t) y(t)
z = dsolve(diff(x) == y,diff(y == -x));
s = [z.x z.y]

输出:

>> Test
求解常微分方程组
s = [ C8 - C7*exp(-t), C7*exp(-t)]

你可能感兴趣的:(matlab学习笔记,matlab)