# In nested for-loops, how and where should I set the counting index (indices?) correctly, for doing numerical root-finding using fsolve?

1 view (last 30 days)
Noob on 25 Sep 2020
Commented: Matt J on 25 Sep 2020
Hi,
Let's say I have a function, f, that maps R^3 to R^3, and I want to find its multivariable roots numerically, using fsolve.
What's a good way to use the counting index, i, in the loops?
For instance, if f(x,y,z) = ( f_1(x,y,z), f_2(x,y,z), f_3(x,y,z) ), and I want to solve f = (0,0,0), then I could write nested for-loops that do something like this:
for x_guess = linspace(-10,10,10) % first, fix a guess for x
for y_guess = linspace(-10,10,10) % now for a fixed x, also fix a guess for y
for z_guess = linspace(-10,10,10) % now for a fixed x and y, check all values of z, before fixing a new x and y
i = ...
end
end
end
[ multivariable_roots, FVAL, EXITFLAG, OUTPUT, JACOB ] = fsolve( f, [x_guess, y_guess, z_guess] )
Where's a good place to put the counting index? At the innermost loop? Could I do so at the outermost loop?
My mentor showed me a code that uses the counting index in the innermost loop (the code structure that I provided above), but I currently don't understand it, so I'd like to write my own nested for-loops in order to understand what it does.
Also, would using more than one counting index be helpful? I'm thinking of using counting indices i, j, k, if it makes the nested for-loops easier to understand. For instance, in a double summation, we would typically fix i and sum through j -- and then fix another i and sum through j again, etc. Could I do something analogous in nested for-loops? I think I might actually prefer using a different counting index for each loop, since things might look more explicit that way.
Thanks,
##### 2 CommentsShowHide 1 older comment
Noob on 25 Sep 2020
Hi Matt,
Yes, I understand what you're getting at, which is the reason why I want to write my own nested for-loops. However, the code I have from my mentor only has one loop index -- at the innermost loop. The code runs fine too, and the solutions seem valid. I've edited my question to show you the code structure of what I mean.
Thanks,

Matt J on 25 Sep 2020
Edited: Matt J on 25 Sep 2020
Well, first of all, you would not use fsolve when doing a gridded root search. In general, the search could be done like this:
[x_guess,y_guess,z_guess] = ndgrid(linspace(-10,10,10));
fvals=nan(size(x_guess));
for i=1:numel(x_guess)
fvals(i) = f( [x_guess(i), y_guess(i), z_guess(i)] );
end
iroot=abs(fvals)<=something_small;
solutions=[x_guess(iroot),y_guess(iroot),z_guess(iroot)];
Note however that you would only ever resort to a for-loop if your objective function f() is absolutely not vectorizable. For a vectorizable function like f(x,y,z)=x+y+z, the above could be simplified to something that calls f() only once (and hence is much more efficient):
[x_guess,y_guess,z_guess] = ndgrid(linspace(-10,10,10));
fvals=f(x_guess, y_guess, z_guess);
iroot=abs(fvals)<=something_small;
solutions=[x_guess(iroot),y_guess(iroot),z_guess(iroot)];
Once you have done one of the above search schemes, and assuming the solution space contains only a finite set of isolated points, you could then loop through solutions(i), and try using fsolve to calculate more exact roots. Note, however, that there is generally no systematic way of choosing initial guesses for fsolve that guarantees it will converge to a root you haven't already found previously. There is no numeric method that will guarantee that you find all roots to a general function.
Matt J on 25 Sep 2020
Yes, ndgrid() will let you make grid sample arrays in any dimension, assuming you have adequate memory to hold them. However, if your functions are vectorizable, it may be more memory-efficient to use ndgridVecs(),

R2017a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!