matlab - Combining solve and dsolve to solve equation systems with differential and algebraic equations -


i trying solve equation systems, contain algebraic differential equations. symbolically need combine dsolve , solve (do i?).

consider following example: have 3 base equations

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 

solving both differential equations, eliminating int(y,0..t) , solving c=f(c1,c2,a) yields

c1*b == c2*c   or   c1*(a-c) == c2*c c = c1/(c1+c2) * 

how can convince matlab give me result? here tried:

syms b c y c1 c2; eq1 = == 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'); % works, no inclusion of algebraic equation %[sol_deq1, sol_deq2]=dsolve(deq1,deq2,eq1,'c'); % not work %solve(eq1,deq1,deq2,'c') % not work %solve(eq1,sol_deq_c1,sol_deq_c2,'c') % not work 

no combination of solve and/or dsolve equations or solutions tried gives me useful result. ideas?

now assumed wanted code rather general, made able work given number of equations , given number of variables, , did no calculation hand.

note way symbolic toolbox works changes drastically year year, work you. 1 can add equation eq1 list of inputs of dsolve there 2 problems that: 1 dsolve seems prefer character inputs , second dsolve doesn't seem realize there 3 independent variables a, b, , c (it sees 2 variables, b , c).

to solve second problem, differentiated original equation new differential equation, there 3 problems that: first matlab evaluated derivative of a respect t 0, had replace a a(t) , such b , c (i called a(t) long version of a). second problem matlab used inconsistent notation, instead of representing derivative of a da, represented diff(a(t), t) had replace latter former , such b , c; gave me da = db + dc. final problem system under determined, had initial values, here have solved a(0) matlab seemed happy using a(0) = b(0) + c(0).

now original first problem, solve had convert every sym char.

here code

function solveexample syms 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 general case eq1 array % , deq3 1 deq3_char = symarray2charcell(deq3); initeq3_char = symarray2charcell(initeq3);  % below same  % 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 eq , depvars  % may vectors or scalars % , need not same size. % eq = equations % depvars = dependent variables % indepvar = independent variable % initialval = initial value of indepvar  depvarslong = sym(zeros(size(depvars))); k = 1:numel(depvars)   % make variables functions   % eg. becomes a(t)   % diff(a, t) not become 0   depvarslong(k) = sym([char(depvars(k)) '(' ...     char(indepvar) ')']); end  % next make equation in terms of these functions eqlong = subs(eq, depvars, depvarslong);  % find ode corresponding equation d_eqlong = diff(eqlong, indepvar);  % replace long terms 'diff(a(t), t)' % short terms 'da' % otherwise dsolve not work. % first make short variables 'da' d_depvarsshort = sym(zeros(size(depvars))); k = 1:numel(depvars)   d_depvarsshort(k) = sym(['d' char(depvars(k))]); end % next make long names 'diff(a(t), t)' d_depvarslong = diff(depvarslong, indepvar); % replace d_eq = subs(d_eqlong, d_depvarslong, d_depvarsshort);  % determine equation % governing initial values initeq = subs(eqlong, indepvar, initialval); end  function cc = symarray2charcell(sa) cc = cell(size(sa)); k = 1:numel(sa)   cc{k} = char(sa(k)); end  end 

some minor notes, changed == = seems difference between our versions of matlab. added t independent variable in dsolve. assumed know cells, numel, linear indexes, ect.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -