help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Functions and handles


From: Juan Pablo Carbajal
Subject: Re: Functions and handles
Date: Fri, 6 Jun 2014 23:28:38 +0200

On Fri, Jun 6, 2014 at 11:00 PM, Fausto Arinos de A. Barbuto
<address@hidden> wrote:
>
> On Friday, June 6, 2014 5:42:36 PM, Juan Pablo Carbajal
> <address@hidden> wrote:
>
>>
>>
>>On Fri, Jun 6, 2014 at 7:33 PM, Fausto Arinos de A. Barbuto
>><address@hidden> wrote:
>>> function xdot = f(x,t)
>>>     r = 0.25;
>>>     k = 1.4;
>>>     a = 1.5;
>>>     b = 0.16;
>>>     c = 0.9;
>>>     d = 0.8;
>>>     xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
>>>     xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
>>> endfunction
>>
>>r = 0.25;
>>k = 1.4;
>>a = 1.5;
>>b = 0.16;
>>c = 0.9;
>>d = 0.8;
>>f = @(x,t) [r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1)); xdot(2) =
>>
>>c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2)];
>>
>>Does that suits you?
>>
>
> Yes, I think it does (haven't tried it), but suppose that "r", or "k",
> or "a" are functions of "x" and/or "t" and f() is a function to be
> called by ode45.  Will that work?  I'm afraid it won't.
>
> The example I provided in my original post is not exactly the best.  I
> have a better one home, but I'm working now.  Also, I forgot to mention
> the "ode45" issue.  My bad.
>
> All this because I don't seem to be able to call a function from within
> an Octave script.  Apparently, to be callable, functions must be saved
> as separate *.m files and I want it to be in the body of the said script.
> Otherwise, I'm goofing terribly somewhere.
>
> Thanks for your reply, JP, much appreciated.
>
> Fausto
>
>
>
If the parameters are functions of the state you cannot have them
outside of the function...more or less..you can do this (however there
might be efficiency issues)
r = @(x) x +1;
k =@(x) x.^2;
a =@(t) sin (t);
f = @(t,x) [x(1)*r(x) + k(x); a(t).*x(2)]
However, if the function is complex, just define it in a separate m-file.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]