what's wrong with matlab dsolve ?

I am using this matlab version
'24.2.0.2806996 (R2024b) Update
Example:
-------------------------------------------
syms u(t) a
sl = dsolve( diff(u,t) == sqrt( u^2+a^2) )
sl =
-a*1i
a*1i
exp(C1 + t)/2 - (a^2*exp(- C1 - t))/2
------------------------------------------
Apart from the constant solution,
the correct solution is
u(t) = a*sinh( t+ const.)
which is different than the matlab solution unless a^2 = 1
What's wrong with dsolve ?

回答(3 个)

You can use the rewrite function to get it in a particular form, however MATLAB doesn't appear to agree with your solution --
syms u(t) a
sl = dsolve( diff(u,t) == sqrt( u^2+a^2) )
sl = 
sl_rw = rewrite(sl, 'sinhcosh')
sl_rw = 
sl_rw = rewrite(sl, 'sinh')
sl_rw = 
sl_rw = simplify(sl_rw, 500)
sl_rw = 
.
If a > 0, insert C1 = const + log(a) in MATLAB's solution for the free constant C1, and you will recover your solution.
For a < 0, your solution won't work because you get
a*cosh( t+ const ) = abs(a)*cosh(t+ const)
from
diff(u,t) = sqrt(a^2+u^2)
for
u(t) = a*sinh(t + const)
which is only valid if a >= 0.
For a = 0, your solution is only u = 0 whereas MATLAB gives u = 0 and u = exp(C1 + t)/2 (or simpler u = const*exp(t) for a free constant const >= 0) which is correct.
So this will work:
syms a positive
syms u(t)
sol = dsolve( diff(u,t) == sqrt( u^2+a^2) )
sol = 
This is not a solution to your problem but a demo to show the comparison between the numerical solution and the analytical solution, given that the parameter a is a real number that can take on either positive or negative values. However, I haven't tested whether dsolve can achieve this level of simplification after applying the simplify() and rewrite() functions.
Differential equation:
Analytical solution (real):
a = -3; % parameter
ode = @(t, u) sqrt(u^2 + a^2);
tspan = [0, 3]; % duration
u0 = 2; % initial value
% ode45 solution
[t, u] = ode45(ode, tspan, u0);
% analytical solution
tsol = linspace(0, tspan(2), 21);
usol = abs(a)*sinh(tsol + asinh(u0/abs(a)));
% result
hold on
plot(t, u)
plot(tsol, usol, '.');
xlabel('t')
ylabel('u(t)')
legend('ode45 solution', 'analytical solution', 'location', 'northwest', 'fontsize', 14)
hold off

类别

产品

版本

R2024b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by