help-octave
[Top][All Lists]

## Am I using varargin correctly?

 From: Jeff Sadowski Subject: Am I using varargin correctly? Date: Wed, 18 Jun 2003 14:21:18 -0600 (MDT)

```I see little to no information on how to use varargin
when I used ... with va_arg() it gave me warnings about
these being depriciated and told me to try varargin
yet there was no documentation about varargin
This is what I got through trial and error; is it correct?

% filename nfactor.m
% nfactor finds the factors for an nth degree polynomial
% if you have an equation a(x^n) + b(x^(n-1)) + c(x^(n-2)) + ...
% use nfactor like so nfactor(a,b,c,...)
% example
% equation=x^2 + 2*x + 1
% octave:1> nfactor(1,2,1)
% ans =
%
%   -1
%   -1
%

function ans=nfactor(varargin)

x=0;
ans=[];
while(x<nargin)
if(size(ans)!=0 || nth(varargin(x+1),1) != 0)
ans=[ans,nth(varargin(x+1),1)];
endif
x++;
endwhile;
ans=nfactor(ans);
endfunction

function ans=nfactor(VECTOR)
if(size(VECTOR)(1)==1)
ans=eig([-1/VECTOR(1)*VECTOR(2:size(VECTOR)(2));eye(size(VECTOR)(2)-2,size(VECTOR)(2)-1)]);
elseif(size(VECTOR)(2)==1)
ans=factor(VECTOR');
else
disp("a*x^n+b*x^(n-1)+c*x^(n-2)\nnfactor(a,b,c,...)")
ans=0;
endif
endfunction

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```