I have simple Simulink model and I would like to change the initial condition of integrator based on some signal. This signal can take values 1 or 0 and initial conditions of integrator should be equal to 1.16 or 0.65 respectively.
I tried to set a parameter x_init in Model Workspace (and then use it in Integrator block), but I couldn't access it via function. Then I tried to run MATLAB function inside simulink model with set_param(...), but I got error:
Function 'set_param' is not supported for code generation. Consider adding coder.extrinsic('set_param') at the top of the function to bypass code generation.
This is how the structure of model looks like in Model Explorer. I would like to change the x initial condition.
Using a workspace variable as you are doing is the wrong approach.
Change the Initial Condition Source property of the integrator to external. This will give the block an additional in port. The value of the signal fed into this port when the integrator is reset is taken as the initial condition.
Related
I have the following function in a simulink block. It takes as an input the value of xi and of xihat, and assess whether xihat should be updated or not. I also want to output whether there was an update, therefore the variable update takes the value 1 if xihat is updated and -1 if it is not. When I run the Simulink programme, I get the expected values for xihat (when plotted, the data is similar to xi but with a stairs kind of appearance). However the value of up is -1 for all of the simulation time. This is not coherent with the fact that the value of xihat is updated. I have even included a disp('yes') to show when the if statement is executed. In short, when the condition is positive definite, xihat should be updated and up should be assigned 1 and only the former is executed. How is this possible?
I have tried to run this exact same function with the same data in a matlab script and it then runs perfectly and up is coherent with xihat. I have attached an image of the subsystem in which the block is located.
I also tried to change the variable name, change the order of the statements, output the conditions etc and nothing seems to help.
Simulink subsystem with MAtlab block.:
I'm trying to model the respective processes of an internal combustion engine. My current modelling approach is to have different sub functions which model the different processes.
Within each sub function is a Level 2 S-Function which solves the ODEs to give the in cylinder state (pressure, temperature, etc).
The problem that I'm having is that each sub function is enabled depending on the current crank angle which is computed from the current timestep in Simulink. The first process works fine as I manually set the initial values, but then I can't pass the latest in-cylinder state (the output from the first sub function) to the second sub function to use as the initial conditions (it insists on using the initial values I set at the beginning of the simulation).
Is there any way round this? Currently I'm going along a path of global data stores, but haven't had any joy so far.
There are a lot of different ways to solve this problem.
I'll show some of them as examples.
You can create additive output with Unit dalay block like this:
So you can get value of your crank angle from previous timestep and USE IT in formula for solving you equations.
Also you can use some code like this:
if (t == 0)
% equations with your initial values
sred = 0;
else
% equations with other values
y = uOld + myCoeef;
end
Another idea: sometimes I use persistent variables in Matlab function to save values of some variable from previous step. But I think it makes calculation slower.
One more idea - if you have Stateflow you can create chart with two states: first for initial moment with your coefficient and second to solve new equations.
If I understood you in wrong way you can show your code and we'll offer some new ideas!
P.S. Example of my using of S-Function:
My S-Function needs 2 values: Q is calculated in simulink at every step, ro is initial I took from big matrix I loaded from workspace in table and took necessary value depending of time.
So there is no any initial values in S-Function - all needed values I transmit into it from simulink!
In simulink I have a function block. Basically it contains
function y=fcn(u)
if u==1
a=[0,...,1];
b=[1,...,2];
end
y=[a',b'];
%y=struct('time',a,'value',b); %Second option
I want to use these arrays as a signal. As you see I have tried two options, making an output as an array and as a structure, non of them work for me.
In short, I'd like to be able to connect a scope to the output of the function and see the signal generated by (a,b). The reason I want to do it from a Simulink block is that I can just switch between many options of signals without having to build again the model. More over, I'd prefer that if the simulation time is bigger than whatever is specified in a then the signal keeps the last value of b.
p.s. I have tried this using a From Workspace block and it works fine.
I have a MATLAB function block in simulink and for each step simlulink does I want to input a counter with increment 1.
Ex:
1st Step -> Acc=1
2nd Step -> Acc=2
I tried using a Count up block + Pulse generator but the time step of simulink is not constant.
Any ideas?
A common way to do this is to use a sum and a memory block with an initial condition of 0. It should count steps in both fixed and variable step simulations. In fact I believe this would be build and perform very much like an s-function solution during simulation.
Why not just use an integrator block? You can choose with a discreet or continuous integrator block depending on your model type. You can specify the start conditions/value and reset conditions if needed. The image below shows an example of discreet and continuous blocks. Both are just using their default values. To do what you want (adding 1 to the output every step) just define the model sample time as an environment variable (eg sT=0.01) and set the integrator gain to be 1/sT.
I want to control from matlab a modelica model implemented in Dymola. At each x-seconds, matlab reads the states values and it calculates the new parameters values of the model and calls dymola to simulate the model with these new parameters values.
I try to initialize the states using the final values of the previous simulation using importInitial(dsName="dsfinal"); and then simulating.
This works if I give the command straight in the simulation log but it doesn't work when I call it from matlab, eventhought exactly the same command appear in the dymola simulation log.
Could any one help?
Thx!
The problem was due to the change of the parameters values.
Using following code solved it:
1) use simulateModel() with all simulation parameters you want for the first simulation
2) use importInitial('dsfinal.txt') to import the final state values
3) use modelName.parameterName = newValue to change parameter value
4) use simulate() to simulate further with the same settings as first simulation.
See also: http://www.claytex.com/how-to-restart-a-simulation/