matlab class methods as ode function handle

Hi Guys, I am new to matlab OO and I want to know if there is a possibility to use a class method as a function handle.
To elaborate what I have now is the following,
classdef Ball < handle
%%BAll Class
properties (GetAccess = public, SetAccess = private)
initialPosition;
initialVelocity;
position;
time;
end
properties (GetAccess = private, SetAccess = private)
state;
end
methods
%%constructor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function obj = Ball(ballData)
obj.initialPosition = ballData.initialPosition;
obj.initialVelocity = ballData.initialVelocity;
obj.time = 0.0;
obj.state=[obj.initialVelocity; obj.initialPosition ];
obj.position=obj.initialPosition;
end
%%update %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function update(obj, world)
%update the ball to synchronize it with the world
deltaT = world.time - obj.time;
if deltaT > 0
% Solving ODE
[~,Y] = ode45(@obj.ballDynamics,[0 deltaT],obj.state);
[i, ~] = size(Y);
obj.state = Y(i,:)';
obj.position = obj.state(4:6);
% Update ball time;
obj.time = world.time;
end
end
end
methods (Static)
%%Ball Dynamics
function x_dot = ballDynamics(~,x)
mass = 56e-03;
diameter = 65.5e-03;
rho=1.25;
g=9.81;
Cd=0.47;
x_dot = [0 0 -9.8 x(1:3)']';
end
end
end
As you can see I am using a static method as a handle and it works file. But ideally I want to use a non static method, such that I can access the class properties.
I would appreciate any help.
Gajan

Answers (1)

I am answering my own question:
classdef Ball < handle
%%BAll Class
properties (GetAccess = public, SetAccess = private)
mass;
diameter;
rho;
g;
Cd;
initialPosition;
initialVelocity;
position;
time;
end
properties (GetAccess = private, SetAccess = private)
velocity;
state;
end
methods
%%constructor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function obj = Ball(ballData)
obj.initialPosition = ballData.initialPosition;
obj.initialVelocity = ballData.initialVelocity;
obj.mass = ballData.mass;
obj.diameter = ballData.diameter;
obj.rho = ballData.rho;
obj.g = ballData.g;
obj.Cd = ballData.Cd;
obj.time = 0.0;
obj.state=[obj.initialVelocity; obj.initialPosition ];
obj.position=obj.initialPosition;
end
%%update %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function update(obj, world)
%update the ball to synchronize it with the world
deltaT = world.time - obj.time;
if deltaT > 0
% Solving ODE
[~,Y] = ode45(@obj.ballDynamics,[0 deltaT],obj.state);
[i, ~] = size(Y);
obj.state = Y(i,:)';
obj.position = obj.state(4:6);
% Update ball time;
obj.time = world.time;
end
end
function x_dot = ballDynamics(obj,~,x)
A = pi*(obj.diameter^2)/4;
x_dot = [obj.g - (obj.rho*obj.Cd*A*norm(x(1:3))/(2*obj.mass)) * x(1:3);
x(1:3)];
end
end
end

Categories

Community Treasure Hunt

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

Start Hunting!