 So, today's class we are going to look at simulation and animation of dynamics using MATLAB for mainly ordinary differential equations. How do you simulate ordinary differential equations and do their animation ok. Very interesting way we can do very nice stuff using MATLAB function called OD45 ok. So, before going to through this like know so we need some representation of a system for the simulation, but I would say before going through this part you may look at actually the MATLAB help on OD function ok. So, for that maybe I will show you first what it produces and then like know maybe we can come back ok. So, when we go through this window and look at see this is a this is a command I have issued here help OD45 and it gives all this kind of you know non-steep differential equation and so you just kind of read through all these things and then maybe we can come back and then whatever I am talking here will make much better sense to you ok. So, let us come back here so you can read through this help ok. You can pause and read through or you can have your own MATLAB software and then go through ok. So, let us come back now to the simulation here or PPT here ok. Now so for this you can see now that this MATLAB OD function will be able to solve the ordinary differential equations which are represented in the in the specific form. This form is like this where this x here is a vector function and its only first derivative is here is equal to f of x t and f is some general non-linear function of x and t ok. So, and again this is a function of x as a as a state coming up here ok. So, whatever state it is a function of it is giving the derivative of the state up here ok that is what is a meaning of this equation. And this equation is what we need to convert all our systems to this form of equations and we will see how we do we do that by an example. So, if you have a system of the kind simple spying mass damper system so m x double dot c x dot and k x are three terms is equal to 0. So, this is not in this form right because this has two derivatives up here. So, we need to kind of convert to this form we need to do something. So, what do we do? Think about this what can be done to get into this form pause for a while here and see what comes to your mind and then we can see the next ok. So, what we can do is we define this introduce these additional states into the system. Since this is double derivative here and I want only single derivative up here, but many different kind of variables can be possible. So, I define these additional variables here x 1 and x 2 are the two states they are defined such that you know when x x 1 is equal to x and x 2 is equal to x dot. So, when I take derivative x 1 dot it will be equal to x dot here and x 2 dot will be equal to x double dot ok and that is how I can get this state derivative here. Now, I want to represent the state derivative in the in this form here ok. So, how do I represent that when I take x dot you can see that x 1 dot is equal to x dot. Now, I cannot use x variable because I have to convert I have already like to transform this system into these two additional variables. So, I will use now x dot I know is to be defined as is defined as x 2. So, x 1 dot is equal to x 2 will form my post equation and then x 2 dot is equal to again this expression comes from this equation and here again I cannot use x I have to substitute them with either x 1 or x 2 which is done here ok. So, this is how I can get this function defined. So, this is my f 1 here in this equation and this is my f 2 in this equation and this function is now ready for coding for doing the ODE sorting ok. So, now we will see this for applied for the spring pendulum kind of a system example ok. So, again the ordinary differential equations we simulated which will come for this spring pendulum system. I will show you what these equations are in a minute, but just to give the settings like know this is pendulum is considered as rigid body and it has its own inertia of rotation about z axis and radius of gyration or mass moment of inertia will be coming from the radius of gyration is given as r. So, mass moment of inertia is m r square for this bob around c g axis passing through c g and z axis ok perpendicular to this page ok. Then the spring is like considered as a stiffness non-linear stiffness why because typically like you know for actual example or actual practical case this can be a rubber like a string and rubber as it stretches its stiffness starts increasing. So, it has some kind of a cubic non-linearity which will be increasing the stiffness as the stretching happens ok. So, that will that is also incorporated in the equations ok. So, considering this non-linear spring and important other thing is this L is unstretched length of this spring ok this unstretched length is L and then beyond that the stretch axis deformation that happens with this kind of a setting these are the equations of the system that are obtained ok. So, now you can see there are two equations which are second order equations. So, m x double dot like know equation in the direction of this L it moves like know up and down like that here and then like know there is a equation in the direction of theta ok. So, theta double dot direction ok theta is another kind of degree of freedom. So, theta is a degree of freedom x is another degree of freedom and you have this two degrees of freedom system with two differential equations governing that dynamics. Now, the question is how do we convert them into our form ok which you remember the form is x naught is equal to f of x and t. So, again now we need to define like a new vector of four variables. So, what is the variable of your choice here since we have these two degrees of freedom x and theta naturally we use them in the and their derivatives in the in defining this vector ok in the similar way as we did for the simple spring mass system ok. So, theta double dot theta dot we say as y 2 variable theta as y 1 variable then y 3 is x y 4 is x dot and now when we if you see the derivative of this state it is having like you know this y 1 dot y 2 dot y 4 dot now y 1 dot by definition is theta dot here and theta dot by definition is y 2 so, this becomes y 2 like that the y 2 dot by definition becomes theta 2 double dot theta double dot and y 3 dot becomes y 4 and y 4 dot becomes x double dot. Now, this theta double dot and x double dot can be obtained from these previous equations of dynamics that you saw here these equation of dynamics this will give you the x double dot and this equation will give you theta double dot ok. So, how do you obtain them by using trans like you know moving all these terms on the other side and then dividing that by m will get x double dot moving all these terms on the other side and dividing them by this term will get theta double dot ok and then we substitute them here and like you know our function f 1 f 2 f 3 f 4 is ready for simulation in that land. Now, how do you code them ok. So, this is first function first like you know you have to code this function and how do you use this function in the od 45 command is what we will see actually on the in the program ok. So, clear for so, now how do you develop this code. So, I would suggest like you know you do not see this code first you just go ahead and like you know write your own way this code assuming that this state is given is vector is given. So, f there is a function f we have to write for these arguments are t and y. So, you are you are given y as a vector quantity that is we have to keep in mind that ok it is coming in argument as a vector quantity and then you know this y 1 y 2 y 3 y 4 and from this you need to produce like you know their derivatives y 1 dot y 2 dot y 3 dot y 2 dot by using the this this this part and these equations as we explained ok. So, this is how you will write a code and once you write a code then so, pause it for now here write your own code and then like you know you come back then you will find like know if you are made any mistakes or your understanding is missing somewhere that will open up like you know that becomes like a firm understanding within you. So, do not like go to the next part unless you have written the code yourself ok. Now, we will go to the next slide and see this code ok. So, here now I am defining all these terms in some kind of a fashion whatever some kind of a physical quantities and then writing like so, I am assuming like know that in for this function y is coming as a argument in vector form. So, y 1 y 2 y 3 y 4 will be my 4 states y of 1 y of 2 y of 3 and y of 4 like that and then so, this like know is how it is used here and then I need to produce a derivative. So, dy are second derivatives of the states. So, dy 1 is equal to y 2 dy 3 is equal to y 4 and dy 2 is a as I explained from this formula now you wherever there is a theta you substitute corresponding y and then you can kind of get your equation coded ok. So, this is the equation for theta and this is a theta double dot this is the equation for x double dot ok. This is how like know you write this code and this this function hence here. Once this function is written now let us see how we can go ahead and simulate it ok. So, let us switch back to the to my lab window. Now, here we have this function written in my lab I will show you already written function here ok. So, this code gives you the function written as we have seen the slide ok. And now you can see all these arguments and functions written properly and you can write whatever comments you want to write ok. How do you use this function? Now, once you write this function then let us save it you use it in the ODE command like this. So, for ODE command you need to define this initial conditions. Initial conditions in this case are taken theta is equal to minus pi by 3 and x some deformation is given to the x to begin with some spring is deformed by some amount and then time is defined for which you want to produce this output. And then you can see that this time and initial conditions are going as a other arguments to this ODE 45 function along with the function that you have written. So, this will output the state evolution ok y in the same order y is in the same order as you have coded in this function it will be output here and then time will come as a. So, this is like a time trajectory of of all the states that is coming up here. And then you have the animation creation up here. So, you you define create some figure and in that figure you draw these lines and this bob of the pendulum is drawn as a simple point or circle with the size of the circle big enough for us to visualize it nicely ok. So, that is how like you know this these two this these things are plotted and you plot it delete the plot and again we plot it for the next update of the time like that you can do in the loop and like you know create this animation. There are many different other ways also of creating animation you may find some some more links on the on the youtube or on the matlab website and you can create different different kind of a ways one animations can be created. So, if you run this file now you will find it produces this nice animation of the pendulum ok. So, you can see that pendulum is is oscillating and at the same time it is having the vertical or you know the radial direction stretching and stretching that is happening for the for the spring ok. So, see of course, here we are not modeling it as a string. So, for example, like the if the compressive force becomes too high like the string cannot get compressed ok, but spring can get compressed. So, if it is a rubber band like a string then it will just simply not get compressed further, but we are not like capturing that part here. So, if the spring force is always positive like or there is some stretching in the spring always there then you will have no issue like this will be a realistic kind of a simulation happening in the in the system ok. Then I would like to kind of give give a little bit of more attention to to this other close this finger part of the files. This latex.fig is a file that is written for setting the global variables of a figure that can be controlled in a nice way. So, as to create a figures which are good for the latex document or their manuscript or paper that you write know. So, this file will be of immense help if you run this file then and then use figure command in matlab to plot any or plot command in matlab to plot any figures then all the settings will be done nicely for say latex kind of a document ok. And you have other file where ppt understood pick kind of a name is given this file when you run and plot your any of the figures then this will create a setting which is good for for print presentations ok. So, you may use this files in a in a way to for your future activities as well. This is very interesting a nice way to kind of create environments for you and you do not have to worry about like know somebody pointing that ok, we are pixel looking so small I cannot see them and those kind of thing will not come into picture when you start presenting with this using use of these files ok. So, we will come back to our presentation now here. So, one more case we need to discuss here before we close is you know the case of systems where you have the coupled dynamics coming up here coupled in the sense that it has a double derivatives coupled ok. So, you have this d 1 1 q 1 double dot and d 1 2 q 2 double dot coming in the same equation ok. So, so far we had systems where like only one double derivative was there in one equation. So, there are now these two different quantities double derivatives are coming how do you handle such case ok. So, how do you basically now represent a system in the form that is required for the ODE solver to have like you remember that form the vector x dot is equal to f of x and d ok. So, we want to kind of get the system in that form. These other quantities will not bother us because they are all as coming as q q dots only ok. There is no any double dot terms anymore here ok. So, how do you handle such case how do you do what you do how do you transform think about that ok and then you go to the next slide pause here and then go to the next slide ok. Now, here what we see what we do here is basically convert this system into matrix form ok. This is like a matrix vector form this is a matrix here this is a matrix here which is a vector of this dv by dou v by dou q 1 and dou v by dou q 2 ok. So, that gv forms a vector tau is forms a vector of tau 1 and tau 2. So, what we do here is to get q double dot as a isolated like a vector here q double dot consists of q 1 double dot and q 2 double dot. So, this is a d matrix that is multiplying here. So, we need to transfer these terms on the other side as we did for other cases also, but now instead of dividing we take a inverse of d d d matrix here. dq this is a typically function of variables of a system not a dot variables like you know not q dots here only q. So, this dq inverse multiplied with your transfer terms here these are now forming a vector here. So, that will give us q double dot and once we obtain q double dot our job is easy for us to kind of code this for the ODE solver ok. So, you go through like we can solve such as examples also then very easily. So, with these we can solve you know n degree of freedom robotic manipulator equations and simulate them in MATLAB or whatever purposes you know dynamics and control purposes that you want ok. So, this is how like you know this leads you pathway for like multiple degree of freedom system simulation by using ODE purpose solver. There are like you know some more issues that may come up regarding similarities or some cases where your slopes are very high or your derivatives are becoming very high. Those cases we need to handle separately to make sure that you know there are some kind of other solvers called stiff solvers that can be used or. So, these are like that is a kind of a future pathway for us to get to. For now you can like look at many different parameters that ODE function gives you to play around with and change to look at setting different settings of solving ok. So, you can play around with that that is all given in the MATLAB help we will not get to do that here, but that is a kind of a forward path to look for ok. So, you can also see the demo of ODE 45 in MATLAB and like you know see play around with different things in the ODE function to get a good grasp of like know what you want to do ok. So, we have already seen the animation code. So, we will stop here for now and you enjoy coding with this background many different systems and assignments and things like that. For your future research also this may be of quite different help ok. So, thank you and bye for now we will stop here.