Using Object Properties with ODE45

1 view (last 30 days)
James Briaris
James Briaris on 28 Sep 2012
Hi All
I'm using ODE45 with my ODEs contained in the function derivs, i.e.,
[T,Y] = ode45(@(t,y)derivs(t,y,obj),[0 10],[0 0]);
The variable obj is an object which I use to perform some logical tests within derivs, i.e.,
function dy = derivs(t,y,obj)
if (t>obj.parameter1)
% Update parameter 1
obj.parameter1 = ... %some new value
end
dy = zeros(2,1);
dy(1) = ...
dy(2) = ...
end
The logical 'if' test works for the value of obj.parameter1 that is set when I initially call the constructor for the class obj, and appears to update obj.parameter1 with a new value when I view the object at the command line. However, for all subsequent calls to derivs the logical 'if' appears to be comparing t with the initial value of obj.parameter1 (i.e. the value initialised to it at construction) rather than the updated value in the if statement.
I can't figure this out since as far as I can tell obj.parameter1 is being updated in the object, but not in the if statement (all parameters are public in the class definition). I have a similar implementation in C++, and it works fine there.
Any ideas??
Many thanks
James

Answers (1)

James Briaris
James Briaris on 28 Sep 2012
Okay, so it's to do with the way Matlab holds the object in memory. Changes to properties doesn't appear to change the original value defined in the constrcutor. Instead it makes the change to a copy of the parameter. Appending '< handle' to the class definition allows you to change the original parameter values on the fly, i.e.,
classdef MyClass < handle
properties
methods
end
Whilst this fixes my problem, I'm a little loss for the reason for this extra layer of complexity. Perhaps I'm to used to C++, but the OOP functionality in C++ certainly seems more intuitive.

Categories

Find more on Programming 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!