adding image/snapshot as a comment in Simulink Model - simulink

I made a Simulink model for a Polynomial curve and want to put the Polynomial equation in the model for the future reference.
Is there a method I can add snapshot of that equation as a comment in the Simulink Model
Thanks in advance

The best choice are annotations. You might insert an image but I prefer the Equations option. It allows you to insert LaTex or MathML, which means the Euqtion an later be modified or resized without issues.

Related

The process of solving DAE in Dymola

I am trying to understand the process of solving DAEs in Dymola. According to Chapter 13.8 of Dymola's help document, Dymola would first use an index reduction algorithm to transform the original DAE into ODE, then get a BLT form after partitioning and tearing, so Dymola could solve ODE with BDF method like DASSL.
Here is the screenshot of Dymola's help document.
I also found some material about how to solve DAE from Wolfram mathematica, as shown in the following screenshot, there could be different strategies in different phases, I marked the method I think adopted in Dymola. My question is:
Does Dymola just adopt the method as I marked?
Or Dymola also adopt the method as Wolfram Mathematica, which is using different methods in different phases? If so, how does Dymola determine using which method?
Dymola does something resembling that red line, and ignoring many of the choices in that flow-chart as they are not relevant and there many equally important choices hidden.
E.g.,
The index reduction algorithm will work even if the index is 1 or 0 - it will just not do anything.
The dummy derivatives method can be seen as a projection method. (I did my master's thesis on implementing it a long time ago.)
IDAS is one of the solvers in the sundials suite. Dymola can use sundials solvers for integration, as well as a number of other solvers; and both have the model as ODE or as index 1 DAE for suitable solvers - but that's handled in the generated C-code, and doesn't influence other parts.

Co-Simulation LS-Dyna and Simulink/MATLAB

I would like to make a CoSimulation of LS-Dyna and MATLAB or Simulink.
The idea: A force applied in my dyna model displaces nodes in the first iteration. For the next iteration LS-Dyna passes over the nodal displacements to matlab or simulink. An algorithm calculates the new forces to be applied on each node by using the displacement of the previous iteration and gives it back to LS-Dyna and so on. I am really new to Fem and coding. Can anyone please help me out with my problem and give me ideas or solutions how to implement that?
Do u want to update your model with matlab after every iteration?If not, u can define a force curve in dyna. Of course, if the inertia of model changes little, you can read d3plot or nodout file with matlab to update your model and force.

How to get differential equations from simscape system?

I have simmechanics model in simscape, I have imported it from solidworks design
I'm able to get linearized model using linmod and it is okay as any system in simulink
The problem here that I want to get the differential equations for that system, Is there any way to extract them?
The short answer: no, there is no built-in functionality.
The long answer: you'd have to parse the Simscape network, query the source equations (if the source code is visible). And stack these together. That's a pretty involved workflow.

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.

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...