[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: octave 3.2.4 functions
From: |
Liam Groener |
Subject: |
Re: octave 3.2.4 functions |
Date: |
Fri, 29 Jul 2011 14:16:12 -0700 |
On Jul 29, 2011, at 6:45 AM, 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 <address@hidden>
> % 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????
>
> Can you help me?
>
> Thank you
I think this is a good setup for beginners:
(1) Create a directory (folder) in which to save most all of your m-files. Call
it, say, mfiles.
(2) Use addpath to add this folder to your octave path:
octave[1]> addpath <path to your folder, e.g. ~/mfiles for unix type OS's >
octave[2]> savepath
Now save the two attached files, copied from your email, in mfiles:
rk2a.m
Description: Binary data
f.m
Description: Binary data
These are both "function files" and must have the same names as your functions,
with the .m extension added.
The rest of your code:
x0 = [1; 2];
t = linspace (0, 50, 200)';
x = lsode ("f", x0, t);
plot (t, x)
can be entered interactively, one line at a time, in your octave window, and
you will get your plot. Or you could type them in an editor and save them to
mfiles as a "script m-file" with a name like test.m. Then you will get your
plot by typing test, or whatever you named the file (less the .m), into your
octave window.
As an aside, it is now says considered better form to use @f, rather than "f"
when passing a function as a parameter:
x = lsode (@f, x0, t);
So far, I haven't talked about rk2a.m. This is a differential equation solver
like the built in lode, but appears to be limited to single first order
equations. If, for example, you wanted to plot the solution of:
dy/dx = x+y*x for 0<=x<=2 and y(0) = .1
You could type:
function ydot = f2(y,x)
ydot = x + y*x;
endfunction
directly into the octave window ( or into an editor, in which case you would
save it as f2.m).
The you would enter the following octave commands:
x=[0:.01:2];
y = rk2a(@f2,.1,x);
plot(x,y)
However, rather than typing out the function as above, you could just use an
"anonymous function," as:
y = rk2a(@(y,x)(x+y*x),.1,x);