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

8 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2020-9-25
Could I do something analogous in nested for-loops?
Every for-loop has a loop index (otherwise, what action is it performing), so if you are going to use 3 loops, you will inevitably have 3 loop indices.
Noob
Noob 2020-9-25
编辑:Noob 2020-9-25
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
Matt J 2020-9-25
编辑:Matt J 2020-9-25
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.
  5 个评论
Noob
Noob 2020-9-25
编辑:Noob 2020-9-25
Hi Matt,
What if the function has a parameter c (a constant value) that I would also like to vary -- and time T?
Could I expand the n-dimensional grid?
The range of guesses would be much smaller for c though, say, .5 to 1.5. I could also use linspace for c, but that seems very inefficient to do.
And would it make sense to loop through time T, or manually change it but keep it fixed, like, say, T = 20 seconds?
Thanks,
Matt J
Matt J 2020-9-25
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(),

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by