Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
feed vectors into fzero for speed

Subject: feed vectors into fzero for speed

From: Dhrue

Date: 19 Apr, 2009 20:42:01

Message: 1 of 6

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

Subject: feed vectors into fzero for speed

From: Roger Stafford

Date: 20 Apr, 2009 02:45:04

Message: 2 of 6

"Dhrue " <dhritiman-bhattacharya@uiowa.edu> wrote in message <gsg2ap$dts$1@fred.mathworks.com>...
> 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

Subject: feed vectors into fzero for speed

From: Bruno Luong

Date: 20 Apr, 2009 06:11:05

Message: 3 of 6

"Dhrue " <dhritiman-bhattacharya@uiowa.edu> wrote in message <gsg2ap$dts$1@fred.mathworks.com>...

> 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

Well you could hack the function FSOLVE to work parallel the zero solver (just program a huge system of (nxn) x (nxn) independent equations). However as Roger said, it might be not the right approach. It might be necessary to find another way of solving your problem differently.

Bruno

Subject: feed vectors into fzero for speed

From: Brendan

Date: 25 Aug, 2012 07:57:07

Message: 4 of 6

"Dhrue" wrote in message <gsg2ap$dts$1@fred.mathworks.com>...
> 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


You could try using arrayfun

Subject: feed vectors into fzero for speed

From: Sky Sartorius

Date: 2 Nov, 2012 10:41:08

Message: 5 of 6

Since your exponents are constants, it may be possible to reformulate your equation explicitly so that all terms with a and b are on one side and all terms with x on the other side. Especially easy is alpha and beta are, say, 1 and 2.

You could then use a vectorized search method like bisection, e.g. http://www.mathworks.com/matlabcentral/fileexchange/28150-vectorized-bisection-search

Subject: feed vectors into fzero for speed

From: Sky Sartorius

Date: 9 Nov, 2012 14:10:26

Message: 6 of 6

"Sky Sartorius" wrote in message <k70804$j7u$1@newscl01ah.mathworks.com>...
> Since your exponents are constants, it may be possible to reformulate your equation explicitly so that all terms with a and b are on one side and all terms with x on the other side. Especially easy is alpha and beta are, say, 1 and 2.
>
> You could then use a vectorized search method like bisection, e.g. http://www.mathworks.com/matlabcentral/fileexchange/28150-vectorized-bisection-search

I uploaded a new version of bisection, hopefully to be approved soon. This new version can solve this problem handily:

        [A, B] = meshgrid(linspace(1,2,10),linspace(4,12,30));
        f=@(x) A.*x.^.2+B.*x.^.87-15;
        xstar = bisection(f,0,5);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us