Trouble with MuPad when evaluating symfun at a symbolic variable instead of a number in MATLAB

1 view (last 30 days)
I created a symbolic function in MATLAB R2021b using this script with the goal of solving an ODE.
syms phi(x) lambda L
assume(lambda>0)
eqn_x = diff(phi,x,2) == -lambda*phi;
dphi = diff(phi,x);
cond1 = phi(0)==0;
cond2 = dphi(1)==0;
cond = [cond1, cond2]; % this is the line where the problem starts
disp(cond)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Addendum:
I had presumed these conditions above would work with dsolve, apparently they do not as even in that case I get the same error described below. So it might not actually be a problem with using x=1 instead of x=L but a problem with how I am inputting my conditions. See the line I have commented out below.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
As you can see, this script gives valid conditions to solve the ODE. However, I want to set x = L instead of a number, because I want the ability change the value of L to a number after getting a solution.
I've tried the following, and consulted the MuPad tutorial, but still have not been able to get MATLAB to solve the ODE.
cond2 = dphi(L)==0;
cond = [cond1, cond2];
disp(cond)
% phi = dsolve(eqn_x,x,cond); this is commented to stop an error in MATLAB
% when running in the browser
cond2 = dphi(x==L)==0;
cond = [cond1, cond2];
disp(cond)
same error
cond2 = dphi | x == L; % based on the MuPad tutorial see link below (page 177)
cond = [cond1, cond2];
disp(cond)
and finally...
cond2 = subs(dphi, x = L);
cond = [cond1, cond2];
disp(cond)
same error
Obviously, the way MATLAB is interpreting my cond2 is not correct. Can someone recommend a fix or workaround?

Accepted Answer

Paul
Paul on 22 Jan 2023
Hi Nathaniel,
I too could not figure out how to specify cond2 the way you want.
A workaround would be to only specify cond1, apply dsolve, and then solve for the constant in the solution based on cond2.
syms phi(x) lambda L
assume(lambda>0)
eqn_x = diff(phi,x,2) == -lambda*phi
eqn_x(x) = 
dphi = diff(phi,x);
cond1 = phi(0)==0;
cond2 = dphi(0)==0;
cond = [cond1, cond2]; % this is the line where the problem starts
disp(cond)
% solve with just cond1
sol(x) = dsolve(eqn_x, cond(1))
sol(x) = 
% differentiate the solution
dsol(x) = diff(sol,x)
dsol(x) = 
% solve for C1
syms C1
C1sol = solve(dsol(L)==0,C1)
C1sol = 
0
% sub C1 back into the solution
sol(x) = subs(sol,C1,C1sol)
sol(x) = 
0
For this problem sol(x) = 0 satisfies the differential equation and both boundary conditions.
  3 Comments
Paul
Paul on 24 Jan 2023
You can always contact Tech Support and submit a bug report, if you think it is a bug, or an enhancement request otherwise.
Another option that works for this case is to convert the second order ODE to a system of first order ODEs and specify the boundary conditions there. I changed cond2 so that we get a non-zero solution.
syms phi(x) lambda L
assume(lambda>0)
eqn_x = diff(phi,x,2) == -lambda*phi
eqn_x(x) = 
dphi = diff(phi,x);
[newEqs,newVars] = reduceDifferentialOrder(eqn_x,phi)
newEqs = 
newVars = 
syms Dphit(x)
sol = dsolve(newEqs,[phi(0)==0, Dphit(L) == .5])
sol = struct with fields:
phi: sin(lambda^(1/2)*x)/(2*lambda^(1/2)*cos(L*lambda^(1/2))) Dphit: cos(lambda^(1/2)*x)/(2*cos(L*lambda^(1/2)))
Verify phi satisifes the differential equation
diff(sol.phi,x,2) + lambda*sol.phi
ans = 
0
Verify Dphit is the derivative of phi
diff(sol.phi,x) - sol.Dphit
ans = 
0
Verify boundary conditions
subs(sol.phi,x,0)
ans = 
0
subs(sol.Dphit,x,L)
ans = 
Nathaniel H Werner
Nathaniel H Werner on 25 Jan 2023
Thank you for the suggestion. It is an interesting idea, since I don't have use or time to explore further at the moment I'll leave it here for now.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!