How to implement a time dependent exponential function in Simulink - matlab

This is the equation I am trying to implement. I am using Simulink integrator block and simple algebraic blocks for it. How can I implement the last part (t-tk) because I don't know how to use the simulation time(t) in simulink.

Related

Convolution in Simulink

How to make block arrangements in Simulink to convolute 2 continuous input signal?
For some reason, I have to be able to reset the convolute integral accumulation. Hence, I can't use the conv block in Simulink. Nor can't I use a transfer function block.
This is what I have tried so far. But it only match with the conv block for the first 0.5 second. It then jumps up and down randomly. I know that the implementation I have tried is not continuous, but I don't know how to implement it continuously: whether it is possible to integrate with respect to some other variable other than time in Simulink.

Generating a sine wave in Simulink without any standard blocks

I'm trying to generate a sine wave without using any standard blocks available in Simulink. Right now I'm using constants for phase angle and frequency but eventually I want to vary this and hence not using the standard blocks. Here is the Simulink model
Here is the scope output
The output is not a sinusoidal wave. What am I screwing up here?
I'm using a fixed step auto solver.
As I'm currently not in the possession of Matlab/Simulink so I can't construct a working example. But I think the best solution for your problem is using a Simulink function block. In this block you can define a Matlab function of your own design and it will allow you to generate any signal you want.
Mathworks example

Optimization in NMPC of second order pendulum model

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.

For loop model Simulink

I want to model a for loop system in Simulink, how I can model the following MATLAB syntax into Simulink model?
N=3;
for i=0:1:N
sum(i+1)=factorial(i)/factorial(N);
end
I have tried for loop sub systems in Simulink and also Sum block for iteration loop but doesn't help me. factorial function can be calculated with FCN function.
Suggest me the ways to resolve this model with step time.
If you have the code already in matlab use a embedded matlab function to implement it i your simulink model. This is in general quite efficient since it will be compiled (compared to interpreted matlab function blocks)

How do I integrate an MPC, PID and System models together to simulate on Matlab

I have a system with a Model Predictive Controller and PID Controller.
Assuming I have models for each controller and can express them in discrete time, please how do I integrate them together to simulate properties of the system in matlab?
Thanks
... continuing from the comments.
This is what Simulink is made for. Of course there are ways to do it without Simulink, but often you still use Simulink tools and functions just without the graphical Interface.
I assume you have your transfer functions "on paper". So you need the tf function to define your system model.
G = tf(num,den)
num and den are the coefficient vectors of your transfer function of numerator and denumerator. In Simulink you use the Transfer Fcn block and you define it with
G.num{1} %Numerator coefficients
G.den{1} %Denumerator coefficients
Your PID-controller cannot be defined using this block, as Simulink requires a higher or equal order for the denumerator. Instead use the PID controller Block. You need to calculate the Proportional, Integral and Differential gain before.
Then read the documentation about the MPC toolbox - I'm not familiar with it and can't help you on that - it is explained how you can create an mpc object regarding all your constraints (see your other question).
Then you have various options to transform your mpc object into something Simulink can deal with. I'd recommend the ss - the state space model - which can be implemented using the state space block. There is also a MPC Controller block, I don't have the toolbox - but you'll be able to find out how it can be used.
Finally you find source blocks, like a step to generate a test signal. And there are Sinks, in the easiest case scope to display your results. You can also save them to workspace or whatever...