2013-07-17 97 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作爲自變量。我還假設你知道細胞,數學,線性指數等。