2013-07-17 64 views
4

我正在尝试求解包含代数和微分方程的方程系统。要象征性地做到这一点,我需要结合dsolve和解决(我呢?)。将求解和dsolve结合起来求解具有微分和代数方程的方程系统

考虑下面的例子: 我们有三个基方程

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,一个)收益率

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 

解决方案和/或dsolve与方程或他们的解决方案没有组合我尝试给了我一个有用的结果。有任何想法吗?

回答

0

现在我假设你想要的代码是相当一般的,所以我使它能够使用任何给定数量的方程和任何给定数量的变量,而且我没有手工计算。

请注意,符号工具箱的工作方式每年都会发生巨大变化,但希望这对您有用。现在可以将方程Eq1添加到dSolve的输入列表中,但存在两个问题:一个是dSolve似乎更喜欢字符输入,第二个是dSolve似乎没有认识到存在3个独立变量abc(它只能看到2个变量,bc)。

为了解决第二个问题,我分化原方程得到一个新的微分方程,有三个问题是:首先是Matlab的评价的a的衍生物相对于t作为0,所以我不得不更换aa(t)等等bc(我叫a(t)长版a)。第二个问题是Matlab使用不一致的符号,而不是代表a的衍生作为Da,它表示它为diff(a(t), t),因此我必须用前者代替后者,并且对于bc等等;这给了我Da = Db + Dc。最后一个问题是系统现在处于确定状态,所以我必须得到初始值,在这里我可以解决a(0),但是Matlab似乎对使用a(0) = b(0) + c(0)感到满意。

现在回到原来的第一个问题,解决我必须将每个sym转换回char。

下面是代码

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的版本之间的差异。我还在dsolve中增加了t作为自变量。我还假设你知道细胞,数学,线性指数等。