Filter löschen
Filter löschen

Piece-wise Functions in 3 Dimensional Differential Equation Solver

2 Ansichten (letzte 30 Tage)
Hello,
I want to implement a piecewise function as a part of a differential equation. I was looking at previous posts on this issue, specifically this one, however what I don't understand is the way the function file determines the correct part of the piecewise to use. In Mischa's example (and others that I have seen) his if statements depend on the value of t which to my understanding, is the time span vector that is accepted as the first input argument of the ode solver. If this is the case and the piecewise I wish to implement depends on position, not time, how can I make it so that the if statements take in the current position element from the "S" matrix (as defined in my code)? Or if the solution is just to use "t", how would the if statements know that the conditions that need to be met only depend on z? The primary difference between my odes and the examples I have seen so far is that the others are one dimensional while mine are 3, so it seems the method of using "t" could only work for one dimensional odes. The method I wish to use (posted below) does not work either.
v0 = [2 0 0];
p0 = [0 10 0];
s0 = [p0 v0];
tspan = [0 10];
[t,S] = ode15s(@newsyntaxtestdiffiq,tspan,s0);
function refsolve = newsyntaxtestdiffiq(t,s)
Bx = 0;
By = 0;
Bz = 2;
%Matrix s is defined as s = [p0 v0] = [x y z x' y' z']
ode1 = 0;
ode2 = 0;
ode3 = newsyntaxtestefield(s(1,3));
ode4 = s(4);
ode5 = s(5);
ode6 = s(6);
refsolve = [ode4; ode5; ode6; ode1; ode2; ode3];
end
function [Ez] = newsyntaxtestefield(s(1,3))
if (s(1,3) > 0)
Ez = 2;
elseif (s(1,3) = 0)
Ez = 0;
else (s(1,3) < 0)
Ez = -2;
end
%ode3 = piecewise(z>0,-2,0,0,z<2,2);
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 31 Aug. 2019
Bearbeitet: Walter Roberson am 31 Aug. 2019
None of the numeric ode*() routines can handle discontinuities in the equations. They absolutely require that the estimated gradient be continuous, and you really should have a continuous hessian as well (but sometimes the routines will not notice if you do not.)
In order to deal with this, you need to stop the ode*() call at the discontinuity and then continue from the same boundary condition.
In situations where the discontinuity is based in time only, then you can do that by specifying tspan as ending at the discontinuity, and then continue from that time.
In situations where the discontinuity is based upon some of the other variable (or combination of variables) reaching particular values or relationships, then you need to use an event function to signal when the discontinuity occurs.
I suggest that you look at the ballode example.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by