我正在尝试解决方程系统,其中包含代数和微分方程。为了象征性地这样做,我需要结合dsolve和solve(是吗?)。
考虑以下示例: 我们有三个基本方程式
a == b + c; % algebraic equation
diff(b,1) == 1/C1*y(t); % differential equation 1
diff(c,1) == 1/C2*y(t); % differential equation 2
求解两个微分方程,消除int(y,0..t)然后求解c = f(C1,C2,a)得到
C1*b == C2*c or C1*(a-c) == C2*c
c = C1/(C1+C2) * a
我如何说服Matlab给我这个结果?这是我试过的:
syms a b c y C1 C2;
Eq1 = a == b + c; % algebraic equation
dEq1 = 'Db == 1/C1*y(t)'; % differential equation 1
dEq2 = 'Dc == 1/C2*y(t)'; % differential equation 2
[sol_dEq1, sol_dEq2]=dsolve(dEq1,dEq2,'b(0)==0','c(0)==0'); % this works, but no inclusion of algebraic equation
%[sol_dEq1, sol_dEq2]=dsolve(dEq1,dEq2,Eq1,'c'); % does not work
%solve(Eq1,dEq1,dEq2,'c') % does not work
%solve(Eq1,sol_dEq_C1,sol_dEq_C2,'c') % does not work
我尝试过的方程式或解决方案中没有求解和/或解析的组合给了我一个有用的结果。有什么想法吗?
答案 0 :(得分:0)
现在我假设您希望代码相当通用,所以我使它能够处理任何给定数量的方程式和任何给定数量的变量,并且我没有手动计算。
请注意,符号工具箱的工作方式每年都会发生巨大变化,但希望这对您有用。现在可以将等式Eq1
添加到dSolve
的输入列表中,但有两个问题:一个是dSolve
似乎更喜欢字符输入,第二个是dSolve
1}}似乎没有意识到有3个独立变量a
,b
和c
(它只看到2个变量,b
和{{1} })。
为了解决第二个问题,我区分了原始方程以得到一个新的微分方程,存在三个问题:第一个是Matlab评估c
相对于a
的导数为t
,因此我必须将0
替换为a
,例如a(t)
和b
(我称c
a(t)
的长版{ {1}})。第二个问题是Matlab使用了不一致的表示法,而不是将a
的导数表示为a
,它将其表示为Da
,因此我不得不用前者替换后者,例如diff(a(t), t)
和b
;这给了我c
。最后一个问题是系统现在已经确定了,所以我必须得到初始值,这里我可以解决Da = Db + Dc
,但Matlab似乎对使用a(0)
感到满意。
现在回到最初的第一个问题,解决我必须将每个sym转换回char。
这是代码
a(0) = b(0) + c(0)
一些小注释,我将function SolveExample
syms a b c y C1 C2 t;
Eq1 = sym('a = b + c');
dEq1 = 'Db = 1/C1*y(t)';
dEq2 = 'Dc = 1/C2*y(t)';
[dEq3, initEq3] = ...
TurnEqIntoDEq(Eq1, [a b c], t, 0);
% In the most general case Eq1 will be an array
% and thus DEq3 will be one too
dEq3_char = SymArray2CharCell(dEq3);
initEq3_char = SymArray2CharCell(initEq3);
% Below is the same as
% dsolve(dEq1, dEq2, 'Da = Db + Dc', ...
% 'b(0)=0','c(0)=0', 'a(0) = b(0) + c(0)', 't');
[sol_dEq1, sol_dEq2, sol_dEq3] = dsolve(...
dEq1, dEq2, dEq3_char{:}, ...
'b(0)=0','c(0)=0', initEq3_char{:}, 't')
end
function [D_Eq, initEq] = ...
TurnEqIntoDEq(eq, depVars, indepVar, initialVal)
% Note that eq and depVars
% may all be vectors or scalars
% and they need not be the same size.
% eq = equations
% depVars = dependent variables
% indepVar = independent variable
% initialVal = initial value of indepVar
depVarsLong = sym(zeros(size(depVars)));
for k = 1:numel(depVars)
% Make the variables functions
% eg. a becomes a(t)
% This is so that diff(a, t) does not become 0
depVarsLong(k) = sym([char(depVars(k)) '(' ...
char(indepVar) ')']);
end
% Next make the equation in terms of these functions
eqLong = subs(eq, depVars, depVarsLong);
% Now find the ODE corresponding to the equation
D_EqLong = diff(eqLong, indepVar);
% Now replace all the long terms like 'diff(a(t), t)'
% with short terms like 'Da'
% otherwise dSolve will not work.
% First make the short variables 'Da'
D_depVarsShort = sym(zeros(size(depVars)));
for k = 1:numel(depVars)
D_depVarsShort(k) = sym(['D' char(depVars(k))]);
end
% Next make the long names like 'diff(a(t), t)'
D_depVarsLong = diff(depVarsLong, indepVar);
% Finally replace
D_Eq = subs(D_EqLong, D_depVarsLong, D_depVarsShort);
% Finally determine the equation
% governing the initial values
initEq = subs(eqLong, indepVar, initialVal);
end
function cc = SymArray2CharCell(sa)
cc = cell(size(sa));
for k = 1:numel(sa)
cc{k} = char(sa(k));
end
end
更改为==
,因为这似乎是我们的Matlab版本之间的差异。我还在=
中添加了t
作为自变量。我还假设你知道细胞,数字,线性指数等等。