matlab class methods as ode function handle
Show older comments
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)
Mohanarajah Gajamohan
on 9 Sep 2011
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!