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
Best Answer