Handling constant names for ODE solutions

So, I try to solve symbolycally a variational problen of finding minima for a given functional. But I can't handle names of constants while solving Euler's equation. when changing function F, constants enumerate differently and i don't know how to keep them in place/
clc
syms x y Dy D2y
F= x + y^2 + Dy^2;
x1=0;
y1=1;
x2=1;
y2=3;
phi= -x^2 + 4;
fprintf('F(x,y,y'')=%s\n',char(F))
fprintf('Boundary cond. on left side: y(%d)=%d\n',x1,y1)
fprintf('Boundary cond. on right side: y(%d)=%d\n',x2,y2)
fprintf('Right side restrictions: y=%s\n',char(phi))
dFdy=diff(F,y);
dFdy1=diff(F,Dy);
d_dFdy1_dx=diff(dFdy1,x);
d_dFdy1_dy=diff(dFdy1,y);
d_dFdy1_dy1=diff(dFdy1,Dy);
dFy1dx=d_dFdy1_dx+d_dFdy1_dy*Dy+d_dFdy1_dy1*D2y;
Euler=simplify(dFdy-dFy1dx);
deqEuler=[char(Euler) '=0'];
Sol=dsolve(deqEuler,'x');
if length(Sol)~=1
error('No solutions (or more then 1)!');
end
SolLeft=subs(Sol,x,x1);
SolRight=subs(Sol,x,x2);
EqLeft=[SolLeft == sym(y1)] % =y1
EqRight=[SolRight == sym(y2)]; % =y2
syms C1 C2
Con=solve([EqLeft EqRight],[C1 C2]);
So, plugging for example F = -y^2 + Dy^2 gives me system with C6 and C7 that pop out of nowhere instead of C1 and C2. Why is this so?

 Accepted Answer

MATLAB tends to use the "next available" constant of integration -- but "next available" might take into account that the symbolic engine still has internal references to the C* that was used earlier.
You need to use symvars() or similar to detect which names are there, possibly with a setdiff() against the known names of symbols.
When there are multiple constants in the same expression, then sometimes you need to do a bit of analysis to figure out which constant is acting in which role. Sometimes it is easiest to substitute known values such as 0 or 1 for variables and see which constant surives. Sometimes, though, you need to use something like findSymType() to match a particular subexpression... it can get to be a bit of a nuisance.

More Answers (0)

Categories

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!