Updating States in ODE45

12 views (last 30 days)
N/A
N/A on 18 Jul 2023
Commented: Walter Roberson on 19 Jul 2023
Hi everyone!
I modeled a free falling sphere in air with ODE45 as coded below, and it hits the ground. I wanted to update position and velocity of sphere as it hits the ground and bounce back to air again each time as it hits the ground. I coded the main function in a main file and modeled the equation of motion in a seperate file with a function named "Equation_of_Motion".
Can someone hlep me how I can update the position and velocity of the sphere with odeset and event function in MATLAB as the sphere reaches to ground and hits it each time? It would be great if you could hlep to how I can use events functions in the link I mentioned as below:
Thanks,
The main function code is as below:
clear all;clc;close all;
%% Static paramters
ball_radius = 0.15; % radius of the balls
%% Dynamic parameters
g=9.81; % gravity
COR=0.5;
Mass=1;
Force_Free_Falling=Mass*g;
v0_x=0;
v0_y=0;
v0_z=0;% initial velocity
z0=3;% initial position of center of cube in space
FinalTime=1.5;% Time priod
tolerance=0; % in what tolerance we would start the collision integration
%% Equation of Motion
[t,x] = ode45(@(tt,xx) Equation_of_Motion(tt, xx,Mass,Force_Free_Falling), [0, FinalTime], [z0, v0_z]);
z_Position=x(:,1);
z_Velocity=x(:,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
and the equation of motion file named "Equation_of_Motion" is as below as well:
function xdot = Equation_of_Motion(t,x,Mass,Force_Free_Falling)
pos = x(1);
vel = x(2);
xdot = zeros(2,1);
xdot(1) = vel;
xdot(2) = -Force_Free_Falling/Mass;

Accepted Answer

Steven Lord
Steven Lord on 19 Jul 2023
That documentation page refers to a different meaning of the term "event" in MATLAB, one unrelated to events in differential equations. Instead, look at the ballode example and adapt it to your needs.
edit ballode
  2 Comments
N/A
N/A on 19 Jul 2023
thank you for your response. Could you please extend the "edit ballode" for me for the code I provided? since I cannot undrestand the ballode function and cannot implement it for my code.
I would appreciate it if you could help me with this.
Thanks,
Shahin
Walter Roberson
Walter Roberson on 19 Jul 2023
Each time the sphere hits the ground, it undergoes a sudden state change. When you use any of the ode* functions such as ode45(), those functions are unable to deal with sudden state changes within any one call to the function.
The Runge-Kutta methods used to calculate correct coefficients for figuring out where to move to, are only valid if the code you provide has continuous derivatives to as many levels as used by the Runge-Kutta methods. In particular for ode45() the second derivatives of each value must be continuous.
A bounce introduces discontinuities in the equations. (Another example would be an injection of drugs for a drug system -- or even a bumpy road for a vehicle system.)
You must stop the ode45() call at each discontinuity; you then make any necessary adjustments to the boundary conditions (for example reverse the sign of the velocity) and then invoke ode45() again from where you left off.
In the case where the transition between states can be easily predicted based on time, the easiest approach is to pass in tspan vectors that cover only until the state change is just about to happen. But in cases where the transitions cannot be easily predicted based on time, then you must use the options parameter to pass in one or more event function function handles. After each time that ode45() would otherwise "accept" a new state, ode45() will call the event function(s) which will give information to ode45() about whether a boundary has been hit and whether it needs to terminate execution. ode45() will "back off" the new state a bit until it figures out where the boundary is, use that location as the final state, and terminate. Make whatever changes are necessary and call again, now on "the other side" of the state change.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!