help-octave
[Top][All Lists]

## Re: octave 3.2.4 functions

 From: PhilipNienhuis Subject: Re: octave 3.2.4 functions Date: Fri, 29 Jul 2011 11:47:38 -0700 (PDT)

```john wrote:
>
> Hi,
>
> I have a problem to run the following programs with functions.It's concern
> of solution of a diff.vgl and
>
> eulers method for solving diff.vgl.
> function y = rk2a( f, y0, t )
>   % ----------------------------------------------------------------------
>   % Jonathan R. Senning &lt;address@hidden&gt;
>   % Gordon College
>   % March 22, 1999
>   %
>   % Usage: y = rk2a(@f, y0, t)
>   %
>   % Returns:
>   %   one-dimensional array of y values associated with the t values
>   %   passed into the function.
>   %
>   % Parameters:
>   %   f:      handle of function equal to dy/dt.
>   %   y0:     y value corresponding to t(1), initial t value
>   %   t:      array of points to evaluate solution at
>   %
>   % This function implements a 2nd order Runge-Kutta algorithm to solve
> the
>   % initial value problem
>   %
>   %   dy
>   %   -- = f(y,t),     y(t_0) = y_0
>   %   dt
>   %
>   % This particular version is based on the algorithm presented in
> "Numerical
>   % Analysis", 6th Edition, by Burden and Faires, Brooks-Cole, 1997.
>   % ----------------------------------------------------------------------
>
>   [m, n] = size( t );
>   y = zeros( m, n );
>   n = max( m, n );
>
>   y(1) = y0;
>
>   for i = 1 : n-1
>     h = t(i+1) - t(i);
>     k1 = h * f( y(i), t(i) ) / 2.0;
>     y(i+1) = y(i) + h * f( y(i) + k1, t(i) + h / 2.0 );
>   end
>
> end
> How do I run this program,I tried everything but no result.
>
> ----_--second program:
> function xdot = f (x, t)
>  r = 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
>  x0 = [1; 2];
> t = linspace (0, 50, 200)';
> x = lsode ("f", x0, t);
> plot (t, x)
>
> When I save this program as "function xdot" and I type function xdot at
> the octave prompt
> ,no result.
>
> I have a problem to save this functions and to run.
>
> How can I save and run this programs????
>
>

I suppose you either:

(1)-- Didn't know about m-files. Very very basic, but admittedly if you
don't know it you can't find it easily....
Write everyting between the line containing "function" and the last "end" of
that function into a plain text file named after the function, with suffix
.m.
Your two function files would be called:
rk2a.m
xdot.m
After this, simply type "y = rk2a( f, y0, t )" (w/o double quotes,
substitute your variable names) and it should work.
(Actually there's more to it but in the "default" situation this should work
OK.
I.e., most importantly your m-files should be in the "path" but usually that
path contains a "." (dot)  indicating the current working dir where you
(hopefully) saved these m-files)

- or -

(2)-- You do know about m-files (apologies for infantilizing you above) but
ran into a problem where newly created m-files are "seen" but cannot be
invoked until a restart of Octave (a bug I've seen reported in the bug
tracker).
So: after having created the m-file, just stop and re-start Octave.