Co-Simulation LS-Dyna and Simulink/MATLAB - 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.

Related

How to compute derivative of a neural network model at a particular point using MATLAB?

In using neural networks for solving differential equations, using MATLAB, I could find derivative of the model using the command 'dlgradient'. i.e.
gradientsU = dlgradient(sum(U,'all'),{dlX},'EnableHigherDerivatives',true)
where U is the model found using fullyconnect and dlX is the input.
Now, how we can calculate derivative at a particular point of the model U.
To be specific I wanted to add derivative of the model at a particular point, say U_0=U'(5) to the loss function. So, how can I compute that?
I have followed the documentation given in MATLAB, given by,
"To evaluate Rosenbrock's function and its gradient at the point [–1,2], create a dlarray of the point and then call dlfeval on the function handle #rosenbrock.
x0 = dlarray([-1,2]);
[fval,gradval] = dlfeval(#rosenbrock,x0);
But I have already called using dlfeval to evaluate the model, so, not able to call dlfeval again. And when I'm trying to directly compute using the command dlfeval(#U,U_0), the output is always zero.
It will be really helpful if some insight could be provided. Thanks in advance.

Iteratively display values during optimization process in MATLAB

I am using lsqnonlin in MATLAB to align a rigid skeleton model to raw joint center data recorded from a device that does not necessarily yield a rigid skeleton. Therefore, my objective function to be minimized is the distance between model joint locations and raw data joint locations.
My algorithm does a pretty good job (red is the rigid model and blue is the raw data):
except for some select frames where my skeleton is off:
I am interested in examining the solution process graphically so that I can visualize what lsqnonlin is trying to do for every frame. Ideally, I would output a History but this seems to be only available for other solvers.
For lsqnonlin it seems that I need to specify a plotting function for the PlotFcn option in optimoptions. I have extracted from the optimation Toolbox the function optimplotx.m which displays a bar graph of the state variables during the iteration process as lsqnonlin converges. My plan is to tweak this function to reproduce the plot for the red skeleton previously displayed.
My problem is that the quantity to be optimized is not the joint locations themselves, but joint angles. I have another function which does forward kinematics and gives me joint positions from joint angles. I would like to pass model parameters into optimplotx.m so that I may call my forward kinematics function and get the plot done but I am unsure how to do that.
These are the inputs to optimplotx.m:
It seems that somewhere deep in the Optimization Toolbox, MATLAB expects these inputs, and they are not to be changed. It is required that I call #optimplotx anonymously anyway, without passing in arguments. It seems that the only way I might be able to get around this is to make my model parameters global in both my main file and optimplotx. Is there any way around this?

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.

Matlab Simulink Model of nonlinear Model

I'm trying to create a Matlab simulink model of the following equation:
I am very new to simulink and need some help getting started.
Ok, this is very easy to do.
set the equation so the result is the highest derivative. in you case d^3y/dt^3
There you have. nothing more to do.
How to follow from here you may ask:
you got x, and you can derive it, or apply any equation you want to it. The only doubt may come is: where the hell should i get y from?
Easy! you have the equation, integrate the result once and use that value for 4*(dydy/dt^2)^2 , integrate it again and use it for the last item and integrate it again and use it to multiply x. That's the advantage of simulink. You can close a loop using the "result" in the equation to calculate the "result" (this is no 100% true, as you use the value of 1 step before in each integration, but it works).
This is the power of simulink, still I strongly recommend you to read a bit about it, so you can understand why to use simulink, but I think playing with it is necessary to learn so: go!
In general when setting up equations in Simulink you should set up a number of integrator blocks to get all your states. When that is done you can sum the different factors together.
Unfortunately I can not post the model I made for the equaltion because of my low reputation points (new here).
dddy ddy dy y
+ --------> 1/s ------> 1/s -----> 1/s ----->

Rotational mechanical system in Simulink

I'm simulating a shaft system in Simulink, where I have to find the displacement of a mass. I'm not sure how to model this in Simulink because of the shaft and pulley. I'm looking through the documentation and the closest thing I see to a shaft is the wheel and axle block. But the shafts are connected by a flexible shaft, which is similar to a spring. Any ideas?
This is a fairly trivial task when using SimScape, which is especially made to simulate physical systems. You'll find most of the blocks you need ready from the library.
I've used SimScape to create a model of a complete hybrid truck... In Simulink it can be done, but you'll need to build your own differential equations for the task. In your case, the flexible axle could be translated to another block with a spring/damper system inside.
If you haven't got access to SimScape, you may also consider to use .m (matlab) files to write your differential equations. This can then be used as a block in Simulink, varying (only) a few parameters over time.
Take this step by step:
1. Draw a free body diagram, write out equations for all the forces as a function of displacement, velocity and acceleration of every element (including rotation obviously). For instance, you know that force on the box m will be *c*dy/dt* plus whatever the pulley experiences.
2. Sort out the rotation of the rod first. You know that *T=I*d(omega)/dt* if you get rid of the rest of the system. So, do something analogous to the car engine example of MatLab: Divide the input T by I to get the acceleration, integrate it to get velocity and one more time to get rotational displacement.
3. Keep adding bits one by one. First, you know that there will be a moment proportional to k*(theta_1-theta_2) acting. This will oppose the motion of rod 1 and act to create motion of rod 2. Add a new "branch" to your model to get theta_2 same way you got theta_1.
4. Go on including further elements...