I believe I am doing something fundamentally wrong when trying to import and test a transfer function in Simulink which was created within the System Identification Toolbox (SIT).
To give a simple example of what I am doing.
I have an input which is an offset sinusoidal wave from 12 seconds to 25 seconds with an amplitude of 1 and a frequency of 1.5rad/s which gives a measured output.
I have used SIT to create a simple 2 pole 1 zero transfer function which gives the following agreement:
I have then tried to import this transfer function into Simulink for investigation in the following configuration which has a sinusoidal input of frequency 1.5rad/s and a starting t=12. The LTI system block refers to the transfer function variable within the workspace:
When I run this simulation for 13 seconds the input to the block is as expected but the post transfer function signal shows little agreement with what would be expected and is an order of magnitude out.
pre:
post:
Could someone give any insight into where I am going wrong and why the output from the tf in simulink shows little resemblance to the model output displayed in the SIT. I have a basic grasp of control theory but I am struggling to make sense of this.
This could be due to different initial conditions used in SimuLink and the SI Toolbox, the latter should estimate initial conditions with the model, while Simulink does nothing special with initial conditions unless you specify them yourself.
To me it seems that your original signals are in periodic regime, since your output looks almost like a sine wave as well. In periodic regime, initial conditions have little effect. You can verify my assumption by simulating your model for a longer amount of time: if at the end, your signal reaches the right amplitude and phase lag as in your data, you will know that the initial conditions were wrong.
In any case, you can get the estimated initial state from the toolbox, I think using the InitialState property of the resulting object.
Another thing that might go wrong, is the time discretization that you use in Simulink in case you estimated a continuous time model (one in the Laplace variable s, not in z or q).
edit: In that case I would recommend you check what Simulink uses to discretize your CT model, by using c2d in MATLAB and a setup like the one below in Simulink. In MATLAB you can also "simulate" the response to a CT model using lsim, where you have to specify a discretization method.
This set-up allows you to load in a CT model and a discretized variant (in this case a state-space representation). By comparing the signals, you can see whether the discretization method you use is the same one that SimuLink uses (this depends on the integration method you set in the settings).
Related
For example, I have a controller consisting of integrator and sine web signal combined in the input. My question may be very naive to you, please help me to understand.
In the picture I have a frequency which is the input of an integrator then two cos and sine block. How to make each Simulink block discrete using ZOH and rate transition block in Simulink.
Two potential solutions
Change the sample time of the sinewave to something that is non-zero. This will make it a discrete signal
Use a Zero Order Hold (as you've mentioned) to convert the signal to discrete, you will need to specify a sample time for this block to perform the conversion. Setting the time to 0.5 will sample the signal every half second for example.
I'm running a simulink model from simulink using matlab. My system is mainly in matlab, but I run the slx file and export the outputs to be used in matlab. The simulation is run for 48 seconds (1 second representing an hour). When I get the outputs, I'm expecting it to be the same quality as when I view it in simulink, but it's not. Here is an example of what my data looks like in simulink:
Here is how it looks like when I plot it in matlab (the number of samples becomes 307 when exported)
I tried to change the step size in simulink or change the solver, but this distorted my simulink output as the following.
My solver is ode45, how do I control the sampling frequency of my data so that I don't get different resolution after exporting it to matlab.
P.S Once I export it, I will interpolate the data so that I get samples in between the hours (a sample every minute instead of every hours). If I can do it at once by changing the step size then that will be perfect.
following your advice, I got this plot when I plot it vs time instead of samples
Thank you
You are using a variable-step solver (ODE45) and thus there is a very high chance you won't get a consistent sampling frequency.
The only way to ensure/control the sampling frequency is to use a fixed-step solver (ode4 for instance).
However, as to why the data looks different between the Simulink scope and the plotted data, for variable timestep solvers there is refine factor (configuration parameters -> Data Import/Export -> Additional Parameters). This is by default set to 1. Set this to 100 and you should get a more consistent-looking sample density.
What should be known about the refine factor?
To get smoother output and have a better time resolution, it is much faster to change the refine factor instead of reducing the step size.
When the refine factor is changed, the solvers generate additional points by evaluating a continuous extension formula at those points.
The refine factor applies to variable-step solvers and is most useful when you are using ode45.
Usually a value of 4 produces much smoother results.
https://blogs.mathworks.com/simulink/2009/07/14/refining-the-output-of-a-simulation/
https://uk.mathworks.com/help/simulink/gui/refine-factor.html
I am trying to find a transfer function of bldc motor speed over duty cycle percent. I made two measurements for different duty cycle percentages in order to both estimate transfer function and its validation.
For the first one (%65 duty cycle step input) I got below measurement and its transfer function estimation.
For the second one (%70 duty cycle step input) I got below measurement and it transfer function estimation.
The problem is that my transfer functions are not validating each other as shown below. They do not give the same response for the same input. Can anyone explain the reason?
It looks like the two measurements are very different. One has a maximum of 220, the other has a maximum of 350. This means the data acquisition is at fault, or the motor is itself variable.
Why don't you try measuring 20 times, and see if the raw data look similar?
Otherwise would need more info about your recording setup and the protocol for testing the duty cycles. It doesn't sound like a matlab or programming problem.
-- edit
Transfer functions are usually the output as a function of the input. Not functions of time.
Transfer function estimation assumes that the system is linear and time-invariant.
Most likely the system exhibits a nonlinear response characteristic which causes a very large change in output amplitude when input is increased from 65% to 70%, so a transfer function obtained at one operating point is not valid for the other.
Hopefully, I will be able to explain my question well.
I am working on Nonlinear model predictive control implementation.
I have got 3 files:
1). a simulink slx file which is basically a nonlinear pendulum model.
2). A function file, to get the cost function from the simulink model.
3). MPC code.
code snippet of cost function
**simOut=sim('NonlinearPendulum','StopTime', num2str(Np*Ts));**
%Linearly interpolates X to obtain sampled output states at time instants.
T=simOut.get('Tsim');
X=simOut.get('xsim');
xt=interp1(T,X,linspace(0,Np*Ts,Np+1))';
U=U(1:Nu);
%Quadratic cost function
R=0.01;
J=sum(sum((xt-repmat(r,[1 Np+1])).*(xt-repmat(r,[1 Np+1]))))+R*(U-ur)*...
(U-ur)';
Now I take this cost function and optimize it using fmincon to generate a sequence of inputs to be applied to the model, using my MPC code.
A code snippet of my MPC code.
%Constraints -1<=u(t)<=1;
Acons=[eye(Nu,Nu);-eye(Nu,Nu)];
Bcons=[ones(Nu,1);ones(Nu,1)];
options = optimoptions(#fmincon,'Algorithm','active-set','MaxIter',100);
warning off
for a1=1:nf
X=[]; %Prediction output
T=[]; %Prediction time
Xsam=[];
Tsam=[];
%Nonlinear MPC controller
Ubreak=linspace(0,(Np-1)*Ts,Np); %Break points for 1D lookup, used to avoid
% several calls/compilations of simulink model in fmincon.
**J=#(v) pendulumCostFunction(v,x0,ur,r(:,a1),Np,Nu,Ts);**
U=fmincon(J,U0,Acons,Bcons,[],[],[],[],[],options);
%U=fmincon(J,U0,Acons,Bcons);
U0=U;
UUsam=[UUsam;U(1)];%Apply only the first selected input
%Apply the selected input to plant.
Ubreak=[0 Ts]; %Break points for 1D lookup
U=[UUsam(end) UUsam(end)];
**simOut=sim('NonlinearPendulum','StopTime', num2str(Ts));**
In both the codes, I have marked the times we call our simulink model. Now, issue is that to run this whole simulation for just 5 seconds it takes around 7-8 minutes on my windows machine, MATLAB R2014B.
Is there a way to optimize this? As, I am planning to extend this algorithm to 9th order system unlike 2nd order pendulum model.
If, anyone has suggestion on using simulink coder to generate C code:
I have tried that, and the problem I face is that I don't know what to do with the several files generated. Please be as detailed as possible.
From the code snippets, it appears that you are solving a linear time invariant model with a quadratic objective. Here is some MATLAB (and Python) code for an overhead crane pendulum and inverted pendulum, both with state space linear models and quadratic objectives.
One of the ways to make it run faster is to avoid a Simulink interface and a shooting method for solving the MPC. A simultaneous method with orthogonal collocation on finite elements is faster and also enables higher index DAE model forms if you'd like to use a nonlinear model.
I am trying to run my simulink file which have pid controller connected to s-function block.
When i set three values parameters of pid which are proportional, integral and derivatives it takes too long to run the whole process. Why this is happened?
In the dialog box of my pid diagram, for porportional value, its equal to the value which correspond to the constant amplitude oscillation.
Then for integral, its equal to Kcu/Ti. Ti is the ultimate period(Pu)/2 and
lastly for derivatives, its equal tu Kcu*Td and Td is Pu /8. This is refer to ziegler nichols method. and again my question is why it takes too long to running this file?
MATLAB S-functions are slow because they run in the MATLAB interpreter. Consider implementing it using Simulink blocks or using a "Embedded MATLAB Function" (pre-R2011a) or "MATLAB Function" (R2011a+) block.
Read Guy and Seth's thoughts on Simulation performance.
#Nzbuu is right about the Matlab S-functions.
But I think the problem here could be somewhere else: #Syarina are you saying that the Simulink simulation gets slower after you set the proportional coefficient for the controller? If you simulate the plant alone, in this case the S-function, do you notice a significant difference in the execution speed? If it is really so, I suppose the PID controller makes the ODE system stiff. This means that the different states of the ODE system have really different dynamics - some are very fast, some are very slow. Using an ode-solver that is not suited for stiff equations you will find the simulation much slower (actually you would have luck if it converges at all).
My suggestion is try to change the solver - for example ode15s.