1、solve :解代数方程
syms a b c x
S = a*x^2 + b*x + c;
solve(S)
s = solve('cos(2*x)+sin(x)=1');
,
syms x y alpha;
[x,y] = solve('x^2*y^2, x-y/2-
alpha');
方法 1:
clear u v x y
syms u v x y
S = solve('x+2*y-u, 4*x+5*y-v');
sol = [S.x;S.y]
方法 2:
A = [1 2; 4 5];
b = [u; v];
z = A
2
0ax bx c
cos 2 sin 1x x
2 2
0x y
2
x
y
2
4 5
x y u
x y v
2、fsolve (@myfunc, ‘初始条件’, ‘自变量列表’)
非线性方程组求解。
解方程: F(x) = 0
x = fsolve(fun,x0)
x = fsolve(fun,x0,options)
x = fsolve(problem)
[x,fval] = fsolve(fun,x0)
[x,fval,exitflag] = fsolve(...)
[x,fval,exitflag,output] = fsolve(...)
[x,fval,exitflag,output,jacobian] = fsolve(...)
function F = myfun(x)
F = x*x*x-[1,2;3,4];
x0 = ones(2,2); % Make a starting guess at the solution
options = optimset('Display','off'); % Turn off Display
[x,Fval,exitflag] = fsolve(@myfun,x0,options)
x = -0.1291 0.8602
1.2903 1.1612
3、dsolve (‘eqn 1’, ‘eqn 2’, …… ,‘eqn n’, ‘初始条件’, ‘自变量列表’)
符号求解器,求解常微分方程
书写规则:
,,
默认自变量:t;
dsolve('Dy = a*x');
dsolve('Df = f + sin(t)');
dsolve('D2y = -a^2*y', 'y(0) = 1', 'Dy(pi/a) = 0');
dsolve('(Dy)^2 + y^2 = 1','y(0) = 0');
求解方程:
Matlab 方法:
dsolve('Du=1+u^2','t')
u = tan(t+C1)
1 2
3 4
x x x
Dy y t
2 2
2D y y t
3 3
3D y y t
求解方程组:
Matlab 方法:
[x,y,z]=dsolve('Dx=2*x-
3*y+3*z','Dy=4*x-
5*y+3*z','Dz=4*x-4*y+2*z', 't');
% 将 x 化简
x = simple(x);
y = simple(y);
z = simple(z);
x = C2/exp(t)+C3*exp(t)^2;
y = C2*exp(-t)+C3*exp(2*t)+exp(-2*t)*C1;
z = C3*exp(2*t)+exp(-2*t)*C1;
4、微分方程的数值计算
4.1 数值计算通式
a) 在解 n 个方程时,y0 和 y 均为 n 维向量,f.m 中的方程组用数组 y 定义;
b) 高阶微分方程转换为一阶微分方程进行计算。
自变量 函数值 替换为
ode45
ode23
ode113
ode15s
ode23s
f.m Ts=[t
1
,t
2
] y(t
1
)
设置:相对误差和绝对误差
省缺值: 10
-3
10
-6
设置新值:re abs
方法:options=odeset(‘relTol’,re,’absTol’,abs)
[t, y] = solver(’f’,ts,y0,options)
自变量 函数值 替换为
ode45
ode23
ode113
ode15s
ode23s
f.m Ts=[t
1
,t
2
] y(t
1
)
设置:相对误差和绝对误差
省缺值: 10
-3
10
-6
设置新值:re abs
方法:options=odeset(‘relTol’,re,’absTol’,abs)
自变量 函数值 替换为
ode45
ode23
ode113
ode15s
ode23s
f.m Ts=[t
1
,t
2
] y(t
1
)
设置:相对误差和绝对误差
省缺值: 10
-3
10
-6
设置新值:re abs
方法:options=odeset(‘relTol’,re,’absTol’,abs)
[t, y] = solver(’f’,ts,y0,options)
c) ode45: 首选的求解器,45 阶显式 Runge-Kutta 法,需要前一时刻的值作为初值;
d) ode23: 第二选择的求解器,23 阶显式 Runge-Kutta 法,需要前一时刻的值作为初值;
e) 如果 ode45 求解的问题比较费时的时候,可以选择其它算法,进行高效率计算
比如:(1)ode113 :需要前几个时刻的计算值作为初始值,属于多步求解器
例 1 单变量情况
令 y1=x,y2=y1’, 则此微分方程拆为一组一阶方程,即
函数 f.m 具体定义为
function dy=f(t,y)
dy=zeros(2,1);
dy(1)=y(2);
dy(2)=1000*(1-y(1)^2)*y(2)-y(1);
取 t1=0,t2=3000
输入如下命令:
[T,Y]=ode15s('f',[0 3000],[2 0]);
plot(T,Y(:,1),'-');
计算结果见左图。
例 2 解微分方程组,即两个自变量,为两个耦合微分方程。
1、 建 立
f.m:
function dy=f(t,y)
dy=zeros(3,1);
dy(1)=y(2)*y(3);
dy(2)=-y(1)*y(3);
dy(3)=-0.51*y(1)*y(2);
2、取 t0=0,tf=12,输入命令:
[T,Y]=ode45('f',[0 12],[0 1 1]);
plot(T,Y(:,1),'-',T,Y(:,2),'*',T,Y(:,3),'+')