Question

Hellp,

I'm a little new to the Simulink environment in Matlab. So, I was trying to implement a piece-wise function in it, but I could't figure out how to do so. The function that I want to implement is the following one:

enter image description here

In those equations the only thing that changes with time is x. So could someone help me ?

Thanks in advance.

Edit: Giving more information about my approach I'd like to add that my "first solution" was try to implement it using the Matlab Function bloc as suggested by am304. Bu,t I run into problems when I tried to compile it. So I moved one and developed the following solution, that works, but I find it a little to be a little rough. enter image description here

Switch 1: uses Vref as threshold, Switch 2: uses -Vref as threshold, Switch 3: uses -Vref as threshold, Switch 4: uses Vref as threshold.

Was it helpful?

Solution

There are various ways in which this can be done: native Simulink blocks, Stateflow charts, MATLAB Function, etc...

Here's how I would do it with a MATLAB Function block:

function vcons = piecewise_fcn(x)

% Define Vref, kx, and x_stop

if kx*(x_stop - x) >= Vref
   vcons = Vref;
elseif kx*(x_stop - x) <= -Vref
   vcons = -Vref;
else
   vcons = kx*(x_stop - x);
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top