Solving exact ODE and fplotting a solution curve.

I use MATLAB 2023a with the symbolic math toolbox.
I am trying to solve the following exact ODE with an initial condition and plot a solution curve.
(eq 1)
Since I do not know how to implement dx or dy in MATLAB code, I changed the above equation as follows:
(eq 2)
Then, I wrote the following code:
syms y(x);
ode = (exp(x + y) + y*exp(y)) + (x*exp(y) - 1)*diff(y, x, 1) == 0
ode(x) = 
cond = y(0) == -1;
sol = dsolve(ode, cond, 'Implicit', true)
sol = 
%% In the sol, sol(1) is a trivial solution.
%% Plotting a solution curve.
fplot(sol)
Error using fplot
Input must be a function handle or symbolic function.
%% Plotting a solution curve with the non-trivial solution.
fplot(sol(2))
%% The above command also produces the following ERROR messsage.
% Error using fplot
% Input must be a function handle or symbolic function.
My question is as follows:
First, why does dsolve() produce a trivial solution, i.e., sol(1)? Also if a way of excluding any trivial solution exists, please let me know.
Second, is there a way to implement dx and dy in MATLAB as originally shown in equation (eq 1)? If not, is there a way of implementing the original ODE problem in an easy way?
Third, how to plot a solution curve of the (non-trivial) particular solution, i.e. sol(2)?
Thank you for your time.

 Accepted Answer

Why do you think sol(1) in your code above gives a trivial solution ? Is y(x) = -exp(x) trivial ?
syms y(x)
ode = (exp(x + y) + y*exp(y)) + (x*exp(y) - 1)*diff(y, x, 1) == 0
ode(x) = 
cond = y(0) == -1;
sol = dsolve(ode, cond, 'Implicit', true)
sol = 
syms z
sol = subs(sol,y(x),z);
sol1 = solve(sol(1),z)
sol1 = 
sol2 = solve(sol(2),z)
sol2 = 
hold on
fplot(sol1)
fplot(sol2)
hold off
xlim([0.01 5])

2 Comments

Hi Torsen,
Thank you for your reply.
Due to your reply, my third question is definitely resolve.
The term 'trivial', which I wrote in the original question, is wrongly used.
My intention was 'incorrect'. Sorry for this confusion.
The 'trivial' solution is y = 1 :)
The sol(1) is incorrect solution, I think.
The reason is as follows:
The original problem can be converted as follows:
The above equation is exact ODE.
Hence, the general solution can be found as follows:
The initial condition gives us .
Hence, the correction solution is , as in sol(2).
The following code shows the correct solution.
But this code is written by multiplying the integrating factor to equation (eq 2), which should be found before wring this code.
syms y(x)
ode = (exp(x) + y) + (x - exp(-y))*diff(y, x, 1) == 0
cond = y(0) == -1
sol = dsolve(ode, cond, 'Implicit', true)
My question is as follows:
  1. Why the original code, which I wrote in the original question, yields the incorrect solution, i.e., sol(1)?
  2. How do we write MATLAB code that interprets dx and dy as written in equation (eq1) in the original question?
Thank you in advance.
Why the original code, which I wrote in the original question, yields the incorrect solution, i.e., sol(1)?
This seems to be a bug in the toolbox. You should report it to MATLAB's support team.
How do we write MATLAB code that interprets dx and dy as written in equation (eq1) in the original question?
You can't.

Sign in to comment.

More Answers (0)

Asked:

DH
on 12 May 2023

Commented:

on 15 May 2023

Community Treasure Hunt

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

Start Hunting!