Simulink --- Peacewise Defined Function

3 views (last 30 days)
Mario Bruza
Mario Bruza on 16 Jan 2018
Edited: Mario Bruza on 17 Jan 2018
Hi,
I´m using "S-function" block in simulink to create a peacewise defined function.
if true
% code
void test_Outputs_wrapper(const real_T *x,
real_T *y)
{
// if(*x==0){ //x=0 Undefined
// y[0]=-0.00001;
// }
// else if(*x==1) //x=1 Undefined
// {
// y[0]=1.00001;
// }
// else
// y[0]=1/((x[0]*x[0])-x[0]); //y = 1/(x^2 -x)
y[0]=1/((x[0]*x[0])-x[0]);
} end
The function above (inside the TAG CODE is: y = 1/(x^2 -x) ) I´m using a Signal Builder (block) where I set a ramp from 2 to 10 (y values).
Setting the domain out of a Undefined values for x a peace of a function run right.
When I use a code below, trying to define x values where a function is Undefined it goes wrong.
if true
% code
void test_Outputs_wrapper(const real_T *x,
real_T *y)
{
if(*x==0){
y[0]=-0.00001;
}
else if(*x==1)
{
y[0]=1.00001;
}
else
y[0]=1/((x[0]*x[0])-x[0]);
} end
Range between 0 and 10 (Signal Builder)
Then a got the graph below:
How can I define it using S-function?
If there is a best way to do that I will be apreciated to know.
Thanks in advance.

Answers (2)

Birdman
Birdman on 16 Jan 2018
Hi Mario,
Firstly, I want to say that you can easily implement what you want by not using S-function. It will be easier. I suggest you to implement it in a MATLAB Function block where you basically write a simple function which consists of if-elseif. The problem you encounter has something to do with your solver type. Probably it is set to variable-step solver and it automatically adjusts the step size necessary to solve your equation and at every step, it narrows down the size since it can provide a meaningful solution(until 1e+29). This can be solved by using a fixed-step solver and setting its step size as 0.01. Then you will have reasonable spikes at the points 0 and 1 respectively. Let me know the results.

Mario Bruza
Mario Bruza on 17 Jan 2018
Edited: Mario Bruza on 17 Jan 2018
Hi Birdman,
I got too many bugs with Scope block and Signal Builder block and when I update a new configuration data to check new simulations more bugs cames on. Sometimes it was needed to close and re-open MATLAB to check new datas configurations and Slope(block) behavior.
Is it normal in Simulink? For exemple.: The Slope(Sink block) just showed me a complete graph between my intervals when a did zoom in and zoom out...(strange behavior).
After a changed the solver parameters ( fixed-step solver) I got the graph correctly, but I used a RAMP BLOCK Signal (I had many problems in terms of behaviors with Signal Builder as above I mentioned). I used a function ( y= 1/((x^2)-x) ) without needed to set intervals where the function was Undefined. Does it is made by the Solver Parameter ( fixed-step solver)? Below are my parameters config:
if true
% code
void test_Outputs_wrapper(const real_T *u,
real_T *y)
{
y[0] = 1/(u[0]*u[0]-u[0]);
}
end
The solver determined by itself the undefined intervals when I changed the solver. Is it how the solver works with piecewise functions?
I tried use MATLAB_Function and I got many strange behaviors. Don´t know why.

Community Treasure Hunt

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

Start Hunting!