象征性地解决ODE系统时出现“使用sym> convertChar的错误”

时间:2018-08-13 10:12:18

标签: matlab symbolic-math ode equation-solving dsolve

我正在尝试求解包含代数方程和微分方程的方程组。为此,我需要组合dsolvesolve。考虑以下示例:

我们有三个基本方程,

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

当我运行代码时:

function SolveExample

syms a b c y C1 C2 t
Eq1 = ('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);

% 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)
% eq = equations
% depVars = dependent variables
% indepVar = independent variable
% initialVal = initial value of indepVar

depVarsLong = sym(zeros(size(depVars)));
for k = 1:numel(depVars)
  % Making 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 making 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'

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

我收到此错误消息:

Error using sym>convertChar (line 1448)
Character vectors and strings in the first argument can only specify a variable or number. 
To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.

Error in sym>tomupad (line 1214)
        S = convertChar(x);

Error in sym (line 211)
                S.s = tomupad(x);

Error in SolveExample>TurnEqIntoDEq (line 43)
  depVarsLong(k) = sym([char(depVars(k)) '(' char(indepVar) ')']);

Error in SolveExample (line 21)
[dEq3, initEq3] = TurnEqIntoDEq(Eq1, [a b c], t, 0);

我该怎么办?

0 个答案:

没有答案