How to access the current iteration number in the program where we supply the non-linear equations,F =0 and the Jacobian for FSOLVE?

3 ビュー (過去 30 日間)
For example, in the sample code for fsolve:
fun = @root2d;
x0 = [0,0];
x = fsolve(fun,x0)
where :
function F = root2d(x)
current_iteration= ?
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;
In the code root2d, if my non linear problem formulation depends on the current iteration, how can i access the present iteration number?
I understand that once all the iterations have been completed, we can get the total number of iterations for convergence using 'output paramter' but this is not what i want.
  2 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 9 月 15 日
Can you show an example of where this might be relevant? The problem definition is usually independent of the algorithm. Different algorithms will take a different number of iterations to reach the same optimal solution. How can you use the iteration number in the equations in any useful way?
Rahul Gulati
Rahul Gulati 2020 年 9 月 15 日
編集済み: Rahul Gulati 2020 年 9 月 15 日
I am solving a set of non linear equations for a finite element code. At each increment, for the first iteration, i need to add some terms to the non linear equations (this will apply 'displacement boundary condition'). This value will be zero for all the following iterations (except for the first iteration). To be able to add a term only for the first iteration, i want to access the current iteration.

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 15 日
編集済み: Ameer Hamza 2020 年 9 月 15 日
Something like this will work
global iter_number % global variable for a quick example, i recommend creating a handle class object
iter_number = 0;
fun = @root2d;
x0 = [0,0];
opts = optimoptions('fsolve', 'OutputFcn', @outputFcn);
x = fsolve(fun,x0, opts);
root2d(x)
function F = root2d(x)
global iter_number
current_iteration = iter_number
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;
end
function stop = outputFcn(x, optimValues, state)
global iter_number
iter_number = optimValues.iteration;
stop = 0;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNonlinear Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by