From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: feed vectors into fzero for speed
Date: Mon, 20 Apr 2009 02:45:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 33
Message-ID: <gsgnjg$bkn$>
References: <gsg2ap$dts$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1240195504 11927 (20 Apr 2009 02:45:04 GMT)
NNTP-Posting-Date: Mon, 20 Apr 2009 02:45:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:533957

"Dhrue " <> wrote in message <gsg2ap$dts$>...
> n=1000
> Assume vectors a & b have the following dimension
> a(n,1)
> b(n,1)
> f0=[.001,3.5];
> xstar=zeros(n,n)
> for i=1:n
> for j=1:n
> anew=a(i)
> bnew=b(j)
> f=@(x) anew*x^alpha+bnew*x^beta+constant % alpha and beta are real no
> xstar(i,j)=fzero(f,f0)
> end
> end
> this for loop is inside another while loop and hence the run time is very high.
> So my question is......
> Is there a way to feed vectors a and b into fzero in one step without using the for loop to compute the value of xstar for every possible combination of elements in a and b 
> Thanks,
> Dhrue

  The function 'fzero' is not set up to remember any parameters like alpha and beta, and in any case the time spent accessing them is very likely quite small as compared with the time spent by 'fzero' in searching for solutions.  The same is undoubtedly true with regard to the time overhead involved in your for-loops, so there is really no point in eliminating them.

  What is fundamental time-wise is the time spent converging to solutions, and that in turn is dependent, at least to some degree, on initial information that is furnished.  Your interval [.001,3.5] is presumably designed to handle all possible combinations that occur in a and b vectors, and as such amounts to a rather crude initial guess.  If the successive values in vector b are in monotonically ascending order and therefore hopefully reasonably close together, it ought to be possible to make fairly good predictions of x solutions using extrapolation techniques after two or more of them have been found as you traverse the ascending b vector.  These could be sent to 'fzero' as single initial guesses rather than intervals.  The same method could be used in moving from one value of the a vector to the next in the outer loop.  With a million calls on 'fzero' these time savings could be 
very significant!  If these original vectors are not in ascending order, you can rearrange them so that they are with the 'sort' function, and then later reshuffle the xstar results back to their original order. 

  Also it should be pointed out that 'fzero' cannot be made aware of the derivative of f(x), and probably cannot search as rapidly as one using, say, the Newton-Raphson method which does utilize the derivative.  Such a method can be rather highly dependent on the quality of initial values, but with accurate ones can converge extremely rapidly.  You might consider that as a possibility once you are in a position to make good initial guesses.

  I would hope that the signs of the products anew*alpha and bnew*beta are the same, so that the terms anew*x^alpha and bnew*x^beta both increase or decrease together as x varies.  Otherwise you face the possibility of multiple solutions and 'fzero' will find only one of them.  In such a case you could produce some puzzling discontinuities in xstar.

Roger Stafford