Trouble with MuPad when evaluating symfun at a symbolic variable instead of a number in MATLAB
2 views (last 30 days)
Show older comments
Nathaniel H Werner
on 18 Jan 2023
Commented: Nathaniel H Werner
on 25 Jan 2023
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?
0 Comments
Accepted Answer
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
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))
% differentiate the solution
dsol(x) = diff(sol,x)
% solve for C1
syms C1
C1sol = solve(dsol(L)==0,C1)
% sub C1 back into the solution
sol(x) = subs(sol,C1,C1sol)
For this problem sol(x) = 0 satisfies the differential equation and both boundary conditions.
3 Comments
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
dphi = diff(phi,x);
[newEqs,newVars] = reduceDifferentialOrder(eqn_x,phi)
syms Dphit(x)
sol = dsolve(newEqs,[phi(0)==0, Dphit(L) == .5])
Verify phi satisifes the differential equation
diff(sol.phi,x,2) + lambda*sol.phi
Verify Dphit is the derivative of phi
diff(sol.phi,x) - sol.Dphit
Verify boundary conditions
subs(sol.phi,x,0)
subs(sol.Dphit,x,L)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!