Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Functions: overloading, including functions not named the same as the file

Subject: Functions: overloading, including functions not named the same as the file

From: Jeff

Date: 31 Mar, 2011 02:02:04

Message: 1 of 6

How can I include a file which has several functions and then, if one of the function names is named the same as a function in another file, make sure the right one is called? (I hope I'm asking this question clearly.)

Here's what I really want to do:

matrix1.m
---------
[t_steps,s1] = ode45(@ODE_linear_fixed, t_steps, s0);
% [t_steps,s1] = ode45(@ODE_cubic_periodic, t_steps, s0);
% [t_steps,s1] = ode45(@ODE_fpu_periodic, t_steps, s0);

% calculate the energy at each timestep

ODE_linear_fixed.m
------------------
...linear, fixed boundaries system here...

ODE_cubic_periodic.m
--------------------
...cubic, periodic boundary system...

I have six (I only showed two) different functions and I uncomment only the one I want to run. After the ODE executes, I want to calculate the energy at each timestep. But the energy calculation is slightly different for each type of function.

I don't want to write six different files with the energy calculation and have to make sure to comment and uncomment the right one for each system. Instead, I would like to include a function in each of the systems files called calcEnergy(), then, following the call to ODE45, I just call calcEnergy and the correct energy calculation would be called.

Is that doable in Matlab? If yes, can you give me a jump start or a pointer (or anything :D )?
Thank you.

Subject: Functions: overloading, including functions not named the same

From: Nasser M. Abbasi

Date: 31 Mar, 2011 02:13:48

Message: 2 of 6

On 3/30/2011 7:02 PM, Jeff wrote:
> How can I include a file which has several functions and then, if one of
>the function names is named the same as a function in another file, make sure the
>right one is called? (I hope I'm asking this question clearly.)
>
> Here's what I really want to do:
>
> matrix1.m
> ---------
> [t_steps,s1] = ode45(@ODE_linear_fixed, t_steps, s0);
> % [t_steps,s1] = ode45(@ODE_cubic_periodic, t_steps, s0);
> % [t_steps,s1] = ode45(@ODE_fpu_periodic, t_steps, s0);
>
> % calculate the energy at each timestep
>
> ODE_linear_fixed.m
> ------------------
> ...linear, fixed boundaries system here...
>
> ODE_cubic_periodic.m
> --------------------
> ...cubic, periodic boundary system...
>
> I have six (I only showed two) different functions and I uncomment
> only the one I want to run. After the ODE executes, I want to calculate
> the energy at each timestep. But the energy calculation is slightly
> different for each type of function.
>
> I don't want to write six different files with the energy
> calculation and have to make sure to comment and uncomment
> the right one for each system. Instead, I would like to
> include a function in each of the systems files called calcEnergy(),
> then, following the call to ODE45, I just call calcEnergy and the correct energy calculation would be called.
>
> Is that doable in Matlab? If yes, can you give me a jump start or a pointer (or anything :D )?
> Thank you.

I am a little confused, but if I understand you, I guess all what you
want is a flag (or in other languages, enumeration data type).

Can't you just write

system ='one'
CALL ode45
v = energy_calculations(system,...rest_of_arguments...)

and in the energy_calculation() function (one function in its own
m file, which is what you want), you then write

------------------------------------------
v = energey_calculations(system,...rest_of_arguments...)
switch system
    case 'one'
           v= ....
    case 'two'
           v=....
end
--------------------------------
      

Another option, if you want all the calculations to be in one file,
is to make a class, say energy, and make different methods
for each type. Then you can do

energy.method_one()
or
energy.method_two()

etc....
and use a case statment as before to decide which to call.

--Nasser

Subject: Functions: overloading, including functions not named the same

From: Nasser M. Abbasi

Date: 31 Mar, 2011 02:44:12

Message: 3 of 6

On 3/30/2011 7:13 PM, Nasser M. Abbasi wrote:
> On 3/30/2011 7:02 PM, Jeff wrote:
>> How can I include a file which has several functions and then, if one of
>> the function names is named the same as a function in another file, make sure the
>> right one is called? (I hope I'm asking this question clearly.)
>>
>> Here's what I really want to do:
>>
>> matrix1.m
>> ---------
>> [t_steps,s1] = ode45(@ODE_linear_fixed, t_steps, s0);
>> % [t_steps,s1] = ode45(@ODE_cubic_periodic, t_steps, s0);
>> % [t_steps,s1] = ode45(@ODE_fpu_periodic, t_steps, s0);
>>
>> % calculate the energy at each timestep
>>
>> ODE_linear_fixed.m

Another option, in case that is what you want becuase I am
not sure about your question, is the following: Pass a user defined
argument to ode45, this argument will be passed though to your
ode45 function. Then in that function, you can do a switch(..)
on this argument to determine which system you want to integrate
the step for.

Like this:

---- RHS.m ------------
function y=RHS(t,x,my_system)
switch my_system
    case 'one'
          y = .....
    case 'two'
          y = ...
end
-----------------------

And in the main line, you do this


---------------------------
my_system='one'; % user much decide which system they want to solve
x=0:.01:1;
initial_condition = 0;

[t,y]=ode45(@RHS, x, initial_condition ,[], my_system);
---------------------------

Now, you only need ONE m file for the ode, and inside
it you can put all the logic in one place.

After solving the ode, if you want to do something more,
then use the same idea, use a switch.

I still do not know if I answered your question or not.

--Nasser



y=3*exp(-t)

Subject: Functions: overloading, including functions not named the same

From: Jeff

Date: 31 Mar, 2011 04:05:22

Message: 4 of 6

"Nasser M. Abbasi" <nma@12000.org> wrote in message <in0o0u$4pd$1@speranza.aioe.org>...
> On 3/30/2011 7:02 PM, Jeff wrote:
> > How can I include a file which has several functions and then, if one of
> >the function names is named the same as a function in another file, make sure the
> >right one is called? (I hope I'm asking this question clearly.)
> >
> > Here's what I really want to do:
> >
> > matrix1.m
> > ---------
> > [t_steps,s1] = ode45(@ODE_linear_fixed, t_steps, s0);
> > % [t_steps,s1] = ode45(@ODE_cubic_periodic, t_steps, s0);
> > % [t_steps,s1] = ode45(@ODE_fpu_periodic, t_steps, s0);
> >
> > % calculate the energy at each timestep
> >
> > ODE_linear_fixed.m
> > ------------------
> > ...linear, fixed boundaries system here...
> >
> > ODE_cubic_periodic.m
> > --------------------
> > ...cubic, periodic boundary system...
> >
> > I have six (I only showed two) different functions and I uncomment
> > only the one I want to run. After the ODE executes, I want to calculate
> > the energy at each timestep. But the energy calculation is slightly
> > different for each type of function.
> >
> > I don't want to write six different files with the energy
> > calculation and have to make sure to comment and uncomment
> > the right one for each system. Instead, I would like to
> > include a function in each of the systems files called calcEnergy(),
> > then, following the call to ODE45, I just call calcEnergy and the correct energy calculation would be called.
> >
> > Is that doable in Matlab? If yes, can you give me a jump start or a pointer (or anything :D )?
> > Thank you.
>
> I am a little confused, but if I understand you, I guess all what you
> want is a flag (or in other languages, enumeration data type).
>
> Can't you just write
>
> system ='one'
> CALL ode45
> v = energy_calculations(system,...rest_of_arguments...)
>
> and in the energy_calculation() function (one function in its own
> m file, which is what you want), you then write
>
> ------------------------------------------
> v = energey_calculations(system,...rest_of_arguments...)
> switch system
> case 'one'
> v= ....
> case 'two'
> v=....
> end
> --------------------------------
>
>
> Another option, if you want all the calculations to be in one file,
> is to make a class, say energy, and make different methods
> for each type. Then you can do
>
> energy.method_one()
> or
> energy.method_two()
>
> etc....
> and use a case statment as before to decide which to call.
>
> --Nasser

Hi Nasser. You seem to get the idea of what I want.

If I were to go with making classes (which is in-line with how I'd like to do it), how do I specify the function passed to the ODE? If the function is a function which is inside a class, how will ODE45 find it? My understanding is that ODE45 requires the function to be in a file where the filename and function name match.

Thanks.

Subject: Functions: overloading, including functions not named the same

From: Nasser M. Abbasi

Date: 31 Mar, 2011 04:11:33

Message: 5 of 6

On 3/30/2011 9:05 PM, Jeff wrote:

>
> Hi Nasser. You seem to get the idea of what I want.
>
> If I were to go with making classes (which is in-line with how I'd like to do it),
> how do I specify the function passed to the ODE? If the function is a function which is
> inside a class, how will ODE45 find it? My understanding is that ODE45 requires the
> function to be in a file where the filename and function name match.
>
> Thanks.

Please see the next post I added, which is to pass an user argument to ODE45.

You will only need only ONE m file.


--Nasser

Subject: Functions: overloading, including functions not named the same

From: Steven_Lord

Date: 31 Mar, 2011 14:04:39

Message: 6 of 6



"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:in0ui2$for$1@fred.mathworks.com...
> "Nasser M. Abbasi" <nma@12000.org> wrote in message
> <in0o0u$4pd$1@speranza.aioe.org>...
>> On 3/30/2011 7:02 PM, Jeff wrote:
>> > How can I include a file which has several functions and then, if one
>> > of
>> >the function names is named the same as a function in another file, make
>> >sure the
>> >right one is called? (I hope I'm asking this question clearly.)
>> >
>> > Here's what I really want to do:
>> >
>> > matrix1.m
>> > ---------
>> > [t_steps,s1] = ode45(@ODE_linear_fixed, t_steps, s0);
>> > % [t_steps,s1] = ode45(@ODE_cubic_periodic, t_steps, s0);
>> > % [t_steps,s1] = ode45(@ODE_fpu_periodic, t_steps, s0);

*snip*

> Hi Nasser. You seem to get the idea of what I want.
>
> If I were to go with making classes (which is in-line with how I'd like to
> do it), how do I specify the function passed to the ODE? If the function
> is a function which is inside a class, how will ODE45 find it? My
> understanding is that ODE45 requires the function to be in a file where
> the filename and function name match.

ODE45 just needs to have access to the function. Most of the time this means
that the function is the main function in a file (and so is visible outside
the file) but you can also do this by using function handles to subfunctions
or methods.

% begin ODE_linear_fixed.m
classdef ODE_linear_fixed
    methods(Static=true)
        function yprime = odefun(t, y)
            yprime = t*y;
        end
        function e = computeEnergy(t, y)
            e = trapz(t, y);
        end
    end
end
% end ODE_linear_fixed.m

Now call ODE45 like:

[t, y] = ode45(@ODE_linear_fixed.odefun, [0 5], 1);
energy = ODE_linear_fixed.computeEnergy(t, y)

Alternately you could make odefun and computeEnergy non-static methods and
pass an instance of the object representing the system to be solved into the
ODE solver.

% begin ODE_linear_fixed2.m
classdef ODE_linear_fixed2
    methods
        function yprime = odefun(obj, t, y) % you could replace obj with ~
            yprime = t*y;
        end
        function e = computeEnergy(obj, t, y)
            e = trapz(t, y);
        end
    end
end
% end ODE_linear_fixed2.m

systemToSolve = ODE_linear_fixed2;
[t, y] = ode45(@(t, y) odefun(systemToSolve, t, y), [0 5], 1);
energy = computeEnergy(systemToSolve, t, y)

If you wanted to ensure that all the types of systems you use have odefun
and computeEnergy methods, you could create an odesystem class with abstract
odefun and computeEnergy methods and require each of your systems to
subclass odesystem (and implement concrete versions of those abstract
methods.)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us