Using dsolve, replace arbitrary constants like C11 with one's own parameter name.

10 views (last 30 days)
when one solves a differential equation using dsolve, without specifying terminal conditions, matlab outputs the solution with an arbitrary parameter name. Here's an example.
syms c T lambda0 x(tau) x0 xT a tau
xDot = @(tau) a*x - exp(-a*tau)/lambda0;
diffEqSolve = dsolve(diff(x,1) == xDot(tau));
diffEqSolve is now:
C3*exp(a*tau) + exp(-a*tau)/(2*a*lambda0)
Since I known that the solution to this problem will have just one parameter, I'd like to pass my own symbol to dsolve. E.g., pass the symbolic variable 'c' to dsolve, and have it return
c*exp(a*tau) + exp(-a*tau)/(2*a*lambda0)
If this isn't possible (I suspect it isn't), could somebody please explain how matlab decides what number to append to C? I've had the same string of commands return C3 or C11, depending on what commands have be processed before dsolve is invoked, and have been unable to see any pattern. If I could compute the number beforehand, then I could do a subs command and get the result I want, e.g.,
diffEqSolve = subs(dsolve(diff(x,1) == xDot(tau)),'C3','c')
Thanks in advance for any suggestions.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Dec 2015
There is no pattern. Or rather, the symbolic engine uses the next one that it has not used before in the session, including using it internally during computations that are not shown to you. Calculations will not necessarily get done in any particular order, either.
You can use symvar() on the expressions before and after to find the names that have been introduced, to give you a list of constants of integration without context in the expression. You could pull the expression apart if you really wanted to, or you could just rename them without caring where they occur or what they mean.
If you want the new names to have meaning reflecting what they are for, you should probably be using initial conditions; it is completely allowed to use symbolic initial conditions such as x(0)==x0 and the computation engine will use that information. Caution, though: it is not uncommon that when you specify an initial condition, the computation engine ends up discarding a valid computation path and is not able to find a solution when one exists. It is also common for pretty strange computations to show up, like x being a function of tau involving a constant omega19 where omega19 is the root of a integral expression involving a function that itself involves the root of an integral expression...
  2 Comments
Leo Simon
Leo Simon on 6 Dec 2015
Edited: Leo Simon on 6 Dec 2015
Following your symvar suggestion, I tried:
xDot = @(tau) a*x - exp(-a*tau)/lambda0;
diffEqSolve = dsolve(diff(x,1) == xDot(tau));
matlabConst = char(setdiff(symvar(diffEqSolve),[a,tau,lambda0]));
tmpString = ['@(tau,c,lambda0,a)' strrep(char(diffEqSolve),matlabConst,'c')];
diffEqSoln = str2func(tmpString);
and it seems to be working fine. Don't know if this kind of thing is also subject to your caution warning. Obviously it will only work if I know in advance that there's only going to be one mysterious matlab constant.
Walter Roberson
Walter Roberson on 7 Dec 2015
You should not do that. char() of a symbolic expression is not necessarily going to give you something that uses MATLAB syntax. Symbolic expressions use a language that is like MATLAB but has some subtle differences.
matabConst = setdiff(symvar(diffEqSolve),[a,tau,lambda0]);
diffEqSoln = matlabFunction( subs(diffEqSolve, matlabConst, sym('c')) );

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!