Using if function in MATLAB Simulink - matlab

I have created the following block diagram on Matlab Simulink. It should give OUT1=1 OUT2=0 when INPUT>=0 and OUT1=0 OUT2=1 when INPUT<0. But it does not give the expected results. What is the reason for that. Any advice to make it work correctly would be appreciated.

The behaviour you are seeing can be explained by noting the following:
The output from the If Subsystem acts like an enable signal.
By default, the Output Ports of an If Action Subsystem are set to hold their values when the (sub)system is disabled.
This can be changed by going to the parameters dialog of the Output Port (within the If Action Subsystems) and changing the Output when disabled property to reset. You'll also want to change the default value to zero.

In many cases an IF is translated to a switch. You calculate cases and select the one which is intended. Here you could connect a constant [1 0] and a constant [0 1] to the first and third input and the sign block from your solution to the second input.

This if function block thing didn't work. So I come up with a different approach to get the result I wanted using the sign block. Solution is posted below. Thank you for everyone for trying to help.

Related

OpenModelica "Change" block does not work properly. [Beginner]

I am really new on Modelica in general but I have a project I need to finish really soon. I apologize if something that I say is really basic. I have tried a few tweaks and searched over the internet but I cannot seem to figure out how to do this to work.
I have created a model. Inside there is a block with a boolean output, lets call this output boolean variable Y1.
Connected to that block I use the Modelica.Blocks.Logical.Change or the Modelica.Blocks.MathBoolean.ChangingEdge blocks. I have tried both since if I understand correctly they do the same thing. Y1 is the input of this block.
So basically the moment when Y1 changes from true to false or from false to true the output of the Change block (lets call it Y2) should be true just for that timestep.
This does not happen. Y2 is constantly on false. I have checked and Y1 is changing over time.
I have also tried to run the example Modelica.Blocks.Examples.BooleanNetwork1 but I see the same thing in there also. The desired output does not change to the example either.
For reference I use the OpenModelica 1.17 with Modelica Standard Library 3.2.3. These are the versions I am obligated to use and I cannot use newer or older ones.
Any tip would be highly appreciated! Thank you.
PS If this does not work I would like to use something different. Is there a way I can access all the previous values (history) of Y1 inside an another block and find these changes with an algorithm there? So lets say I am on timestep N and inside a block I want to access Y1 from 0 to N-1 and find the last timestep that Y1 has changed.
How did you check that the output of the change block doesn't fire at the events? You may not be able to see it on a plot since the impulse is 'infinitely' short (at least, that's my experience with Dymola).
You could latch the impulse from the change block by connecting it's output to an SR flipflop.

Matlab: stop terminal message in call to ga (genetic algorithm function)

In matalb I am implementing genetic algorithm using ga. In a typical call to ga like x = ga(nvars,#fitnessfunc) when returns gives message on terminal specifying the reason for the termination of the genetic algorithm. A typical message is:
Optimization terminated: average change in the fitness value less than options.TolFun.
I want to turn off this message. I read the documentation but found nothing.
Any ideas?
You could try the answer given in here.
Use the evalc and then you wont display the text plus you can actually read it and use it for your own purposes (like knowing why did it stop etc)
Found the answer here minutes after posting the question. To stop the display we have to set display parameter of options structure to 'off'. The proper code is below:
problem.solver = 'ga';
problem.options = gaoptimset('display','off','param2','value2'...);
problem.fitnessfnc = #fitness;
problem.nvars = nvars;
output = ga(problem);

Getting the solver type and step size (for fixed step solvers)

we are trying to integrate a simulation model into Simulink as a block. We have a custom continuous block which loads an m file that contains the functions Derivatives, Outputs etc.
My question is: is there a way to find out which solver is used currently and with which parameters? Our model won't be able to support variable time solvers and I would like to give a warning. Similarly, the model requires the fixed step time for initialization.
Thanks in advance.
You can get the current solver name using
get_param('modelName', 'SolverName');
Some of the other common solver parameters are
AbsTol
FixedStep
InitialStep
ZcThreshold
ExtrapolationOrder
MaxStep
MinStep
RelTol
SolverMode
You can find other parameters you may wish to query by opening the .mdl file in your favorite text editor and digging through it.
If I'm understanding your use case correctly, you are trying to determine the type of solver (and other solver params) for the top-level simulink system containing your block.
I think the following should give you what you want:
get_param(bdroot, 'SolverType'); % //Returns 'Variable-step' or 'Fixed-step'
get_param(bdroot, 'FixedStep'); % //Returns the fixed step size
Notice that for purposes of generality/reusability, this uses bdroot to identify the top-level system (rather than explicitly specifying the name of this system).
If you want to find out more about other model parameters that you can get/set, I would check out this doc.
Additionally, I'm interested to know why it is that your model doesn't support a variable-step solver?

Setting a Simulink model Inport value from an m file

I've been doing a lot of research over the last few hours and I can't seem to figure out how to get and set a value to an Inport box. I have a simple model that has one inport and one outport and they are connected to each other. I want to set the inport value to 2 and run my simulation and see if my outport got set correctly. I read that you can't use set_param to set that value but you have to use sim(), but I'm not having any luck with that. So if anyone know how to look at the data in the inport box and/or how to set it, I'd appreciate it. Thanks!
Lucas
Ports in and out in Simulink don't work as you think. They are needed when you create subsystem - your own Simulink block, than you'll have your in and out ports.
But when you just want a make some model, and pass some data in it, and get results to Matlab, then you need To workspace and From workspace blocks. Some variable-name is assigned in their options, so you can set input data from your .m file and get results in matlab variables.
Block From workspace takes matrix variable, but if you want to pass just a number, you can use block Const and fill it value with a variable name.
Here is a screenshot, an example of in, out, to workspace and const blocks:
Here in example, I have input parameter x (block const), subsystem Gain5 and output parameter y (block To workspace). Inside the subsystem I use in and out blocks to get and return values.

MATLAB set_param for From Wave Device

How to set the parameters of a from wave device block in a simulink model?
I need to set the "samples per frame" parameter. It must be something like ('Model Name/ From Wave Device','Samples Per Frame',1024) ... but it does not work like this.
Is it possible to set the parameters of this block?
Most Simulink blocks use the dialog prompt as the parameter name, but without the spaces. So in this case, you would use,
>> set_param('model/blockName','SamplesPerFrame','1024');
Also, since the block accepts a workspace variable as the value, you should set it as a string, so use '1024', not 1024. One other tip is to use Tab completion, so you could have typed,
>> set_param('model/blockName','S[tab]
and this would have shown you a list of possible parameters that start with S.