0

What is the simplest way to generate piecewise level function like the following

enter image description here

Suppose, I know height (y-value) and length (horizontal) of each level in a matrix

>> C=[2,4,1,-3;2,1.5,0.7,2.8]

C =

    2.0000    4.0000    1.0000   -3.0000
    2.0000    1.5000    0.7000    2.8000

I.e. first row says level level, and the second -- it's horizontal length.

Also I would like to have some interpolation between levels.

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

2 Answers2

0

I use this tool. For example, given this set of piecewise functions:

 x < -5, y = 2 
-5 <= x < 0, y = sin(x) 
 0 <= x < 2, y = x.^2 
 2 <= x < 3, y = 6 
 3 <= x, y = inf 

So, as a function of any variable x, just call it like this:

y = piecewise_eval(x,[-5 0 2 3],{2,'sin(x)','x.^2',6,inf});
0

use

stairs([0 cumsum(C(2,:))], [C(1,:) C(1,end)]);

If you want to change the values in between or plot it differently you can get the coordinates by

[x, y] = stairs([0 cumsum(C(2,:))], [C(1,:) C(1,end)]);
Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52