Is it posible to solve system of differential equations with piecewise function inside?
23 views (last 30 days)
Show older comments
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)
0 Comments
Answers (2)
Walter Roberson
on 16 Mar 2022
Well, let's try something very simple:
syms x(t)
A = piecewise(t >= 0, -t, t)
dx = diff(x, t)
eqn = dx == A
dsolve(eqn)
I would say that's a NO.
5 Comments
Walter Roberson
on 20 Mar 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
expr2 = expr * heaviside(expr)
df = diff(f)
eqn = df == expr2
sol = dsolve(eqn)
John D'Errico
on 20 Mar 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))
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))
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.
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!