#!/usr/bin/octave -q

% Jonathan R. Senning <jonathan.senning@gordon.edu>
% Gordon College
% January 28, 1999
%
% Demonstrates the use of the Octave function "fsolve" to solve nonlinear
% equations in the form f(x) = 0 where x is a vector of one or more
% unknown values.

% Define the function to pass to fsolve.  Notice that both the input and
% output data is IN VECTOR FORM.  This means that even if you want to solve
% the equation f(x) = 0 in one variable you must code the function f(x) to
% accept a vector x and return a vector.  Of course, in the one variable
% case these vectors will have only one element.

f = @(x) 1 - x .* exp( x );

% Now we can call fsolve.  The first argument is the name of the function
% whose root we are seeking.  The second argument is a VECTOR containing the
% initial guess of the solution.  Although not strictly necessary, I've
% used the transpose of the vector [ 0.5 ] as the input vector, since I want
% to pass in a column vector (the "'" is the transpose operator).

format long
x = fsolve( f, [ 0.5 ]' )
