Is it posible to solve system of differential equations with piecewise function inside?

18 visualizaciones (últimos 30 días)
This is the function I want to define
clear
syms x y sf(x,y)
p00 = -6.602;
p10 = 0.06553;
p01 = -1.758;
p20 = -0.0001291;
p11 = 0.04463;
p02 = 1.955;
sf(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2
fsurf(sf)
xlim([0 230])
ylim([0 0.7])
zlim([0 6])
And this is how I defined it like Piecewise, where x=iv (number), y=dp, where,
dp=pkv-pvp, pkv, pvp are both variables dependent on "t". There are two diff. equations. Is it posible, that ode45 will calculate it?
Qvvp = piecewise((p00 + p10*iv + p01*dp + p20*iv^2 + p11*iv*dp + p02*dp^2)>=0,(p00 + p10*iv + p01*dp + p20*iv^2 + p11*iv*dp + p02*dp^2)/1e3/60,0)

Respuestas (2)

Walter Roberson
Walter Roberson el 16 de Mzo. de 2022
Well, let's try something very simple:
syms x(t)
A = piecewise(t >= 0, -t, t)
A = 
dx = diff(x, t)
dx(t) = 
eqn = dx == A
eqn(t) = 
dsolve(eqn)
Error using mupadengine/feval_internal
Invalid equations.

Error in dsolve>mupadDsolve (line 334)
T = feval_internal(symengine,'symobj::dsolve',sys,x,options);

Error in dsolve (line 203)
sol = mupadDsolve(args, options);
I would say that's a NO.
  5 comentarios
Michal Bárta
Michal Bárta el 20 de Mzo. de 2022
Ok thanks, To question: it is exactly like you wrote it. Or is there other solution to this?
Walter Roberson
Walter Roberson el 20 de Mzo. de 2022
If you rewrite the piecewise(expression >= 0, expression, 0) as heaviside(expression)*expression then you might potentially be able to get further, as in this example
sympref('HeavisideAtOrigin', 1);
syms f(t)
expr = t^2 - 5*t + 2
expr = 
expr2 = expr * heaviside(expr)
expr2 = 
df = diff(f)
df(t) = 
eqn = df == expr2
eqn(t) = 
sol = dsolve(eqn)
sol = 

Iniciar sesión para comentar.


John D'Errico
John D'Errico el 20 de Mzo. de 2022
Well, actually, yes you can solve the problem using a numerical ODE solver. That is trivial. For example, consider the simple problem posed by Walter in his answer. For example
du/dt = -t, when t < 0
du/dt = t for t >= 0
Can we solve the ODE using a numerical solver? CERTAINLY!
% choose a specific initial value. u(-5) == 2;
tspan = [-5,5];
odefun = @(t,y) abs(t);
[t,y] = ode45(odefun,tspan,2);
plot(t,y)
ODE45 had no problems with this. Is the solution correct?
plot(t,gradient(y,t))
hold on
fplot(@abs,[-5,5])
Not bad, but you need to recognize the singularity in the problem at t==0. That would be expected to cause ode45 some issues, but it will also cause the gradient function some problems too near t==0, because gradient uses finite differences. So I might have done a little better to use a stiff solver on the problem, but ODE45 handled it with no complaints, and it got the correct result.
So to answer your question, if an ODE solver can handle a piecewise problem, yes. In fact, similar things are often done when solving ODEs.
To ask if dsolve can handle the same problem, well, it has issues.
syms u(t)
dsolve(diff(u) == abs(t))
ans = 
So it looks like dsolve solved the wrong problem. It solved the problem diff(u)==t. I might have posed it in another form, perhaps as
dsolve(diff(u) == t*(heaviside(t)*2 - 1))
ans = 
This works fine, as long as t is always non-negative, but it too fails to recognize the singularity. Regardless, use of an ode solver is not that difficult for such a problem.
For more complex piecewise functions, you would need to write a function that evaluates them properly, but that too is not difficult. I even have a function called piecewise_eval, on the File Exchange, to efficiently evaluate a piecewise function.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by