Is it possible to create a stairstep function?
10 views (last 30 days)
Show older comments
Tyler Bikaun
on 13 Feb 2017
Edited: Walter Roberson
on 14 Feb 2017
Hi all,
I am trying to create a function that emulates what is seen below, I don't think it may be possible with stairstep but I am unsure. Does anyone know how this could be done? As I want an equation that I can have an input x value and then output a y value from the function.
% Stairstep function
x = [0:15];
y = [19,19,19,19,19,25,25,25,25,25,25,19,19,19,19,19];
figure
stairs(x,y)
ylim([0,30]);
Much appreciated
3 Comments
John Chilleri
on 14 Feb 2017
Not sure I understand, unless you're speaking of exactly the x and y you have above. In that case,
function y = foo(x)
vec = [ones(1,5)+18 ones(1,6)+24 ones(1,5)+18];
y = vec(x+1);
end
which accepts any x in [0,15].
Accepted Answer
Walter Roberson
on 14 Feb 2017
Edited: Walter Roberson
on 14 Feb 2017
Although it would be possible to program that, you should not use it in the way you propose. The ode*() routines require that the values have continuous derivatives, which you would not satisfy with a stair-steps function.
If your discontinuities are according to fixed time-points, then you need to break up your ode45 call at the timepoints, start at one end, ode45, use the result for the initial conditions of the next ode45 call and so on.
If your discontinuities are according to x rather than t, then you need to use event functions to tell it to terminate the integration at that point, and loop back and do the next ode45 call using the results from each as the boundary conditions for the next.
ode45 typically notices the discontinuity of a stairs function and considers it a singularity at which it is unable to meet integration tolerances so it gives up.
0 Comments
More Answers (0)
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!