Is it possible to create a stairstep function?

조회 수: 8 (최근 30일)
Tyler Bikaun
Tyler Bikaun 2017년 2월 13일
편집: Walter Roberson 2017년 2월 14일
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
John Chilleri
John Chilleri 2017년 2월 14일
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].
Tyler Bikaun
Tyler Bikaun 2017년 2월 14일
I am using ODE45 to evaluate some equations, x being one of the variables within ODE45 will be used to evaluate a constant that is multiplied onto one of the state space equations within ODE45.
For example something along these lines
function xdot = ode(t,x)
xdot(1) = x(2)
xdot(2) = y_function*x(1);
The y_function is of the shape of a stairstep as shown above where the actual range I'll be using for x is approximately 0:0.001:100 (I don't need the stairstep explicitly, just need a way to call data from a function that is of the same nature).
I've done this using Simulinks 1D lookup table, but I don't want to use Simulink for this, however, I cannot find a way to do it using ODE45 and MATLAB for some reason.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 2월 14일
편집: Walter Roberson 2017년 2월 14일
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개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by