Which documentation and example can I use for iterative solution.

Dear expert,
I was struglling with my matlab model. I have a cascade of functions which each set of function recieves the outputs of the other function as an input arguments. To run all these cascaded functions, the first set of functions needs guess values. The outputs of this functions set should be used as inputs for the next set of functions and continue like this untill the final part. Then the outputs of the last set of functions should be the same with guessed values in the begining using iterative approach. This will be the solution of my model. However, I don't know how to make an iterative solution for this problem which will stop iterating when the guess value is equal to the final calculated value (the difference between them converges to zero).
Can someone help me with examples or which documentation can I use, please? I thank you a lot in advance.
N.B I can share codes but it is too much for help.

12 commentaires

Usually behind such an iterative procedure, there are equations in unknowns that have to be solved:
eq1 = 0
...
eqn = 0
Instead of iterating, formulate these equations and use one of the MATLAB solvers (e.g. "fsolve") to get a solution.
Thank you for your inportatant comment.
That is true. Eventhough I have a bunch of equations, I did the way you mentioned.
My doubt is if fslove also changes the guess value till it gets the same value as the calculated one using the guess value. For example, does the code below iterates y and Cp_guess_arb till the difference of them converges from the function find_CP_MSNP?
options = optimoptions('fsolve', 'Display','iter','algorithm', 'levenberg-marquardt','FunctionTolerance', 1e-4,'StepTolerance', 1e-4,'MaxFunctionEvaluation', 300);
[Cp_calc_arb, fval, exitflag] = fsolve(@(y)find_CP_MSNP(y, C_ret_arb, Jv_arb, Kc,DP, phi, Z_arb, z_pol, X_mem), Cp_guess_arb(1:3),options);
Cp_guess_arb(1:3) are the initial values that "fsolve" uses for y(1:3).
Thereafter, "fsolve" adapts the values for y(1:3) trying to satisfy the equations defined in "find_CP_MSNP" .
That is very clear.
And of course "fsolve" solves the equation defined by "find_CP_MSNP" shown below.
My last question is:
Can we say that the solver is using ''Cp_guess_arb(1:3)'' as an initial value for the cascaded functions and trying to satisfy the equations defined in "find_CP_MSNP" that is set to converge the condition defined by "sol"? (Is my approach correct?)
Equation defined by "find_CP_MSNP" looks as shown below.
function [sol] = find_CP_MSNP( Cp_guess, phi_start, Jv, Kc, DP,phi, Z, z_pol, X_mem)
Cp_guess_MS = Cp_guess;
[y0, yp0] = decic(@MS_AA_imp, 0, [phi_start 0], [1 1 0 0 0], [0 0 0 0 0], [],[],Cp_guess_MS, Jv, Z);
stepsMS = linspace(0 , z_pol, 500) ;
[zsol_MS, ysol_MS] = ode15i(@(z, x, dx_dz)MS_AA_imp(z, x, dx_dz ,Cp_guess_MS, Jv, Z), stepsMS, y0, yp0);
C_surf = ysol_MS(end,:);
%-------------------------------------------------------------------------------
% define anonymous function 'partionAnnonymous' which accepts
% C_surf(out put from the top) as an input for the next function
zz=[0.00001 0.00001 0.00001];
partitonAnonymous = @(z)partition(z,C_surf(1:3));
C_entrance = fsolve(partitonAnonymous,zz);
%-------------------------------------------------------------------------------
% define anonymous function 'nernstPlanckAnonymous' which accept
% C_entrance as an input arguement for the next function
nernstPlanckAnonymous = @(z)NernstPlanck_Vol(z,C_entrance);
C_end = fsolve(nernstPlanckAnonymous,zz);
%-------------------------------------------------------------------------------
% define anonymous function 'secondpartitionAnonymous' which accept
% C_end as an input arguement and computes the Cp_calc
secondpartitionAnonymous = @(z)partitionsecond(z,C_end(1:3));
Cp_calc = fsolve(secondpartitionAnonymous,zz);
%-------------------------------------------------------------------------------
%% the last par that I want the solver [Cp_calc_arb, fval, exitflag] solves
sol = ((y(1:3) - Cp_guess(1:3)).^2);
end
Thank you very much. I really appreciate it, if you can help me with this part.
Since y(1:3) is not input to or calculated in "find_CP_MSNP", I wonder what it is.
That was to mean "Cp_calc(1:3)", not "y(1:3)". Sorry! It is typo when I prepare this question to ask for help. But on my original file it is "Cp_calc(1:3)"
For the rest, is this approach then correct if I correct this mistake? Do you have any suggestion, please?
Torsten
Torsten le 12 Jan 2023
Modifié(e) : Torsten le 12 Jan 2023
Which solver uses "find_CP_MSNP" as problem-defining function ? fminsearch ?
And why don't you solve the algebraic equations all together in one call to "fsolve" instead of three subsequent calls ?
For the first question: I just made a separate script with some given parameters and I used "fsolve" as shown on the code below to find a solution by calling "find_CP_MSNP". I don't know if fsolve is not the right one. (Below is the code I posted earlier with my question)
options = optimoptions('fsolve', 'Display','iter','algorithm', 'levenberg-marquardt','FunctionTolerance', 1e-4,'StepTolerance', 1e-4,'MaxFunctionEvaluation', 300);
[Cp_calc_arb, fval, exitflag] = fsolve(@(y)find_CP_MSNP( y, C_ret_arb, Jv_arb, Kc,DP, phi,Z_arb, z_pol, X_mem), Cp_guess_arb(1:4),options);
For the second question: I find it dificult to merg all together and call them in one call. I did it first that way and I got errors everytime then I switch to calling three subsequent calls.(I don't know how to pass the output of one function as an input argument for the next function in a single file). Do you think that gives different results?
Torsten
Torsten le 12 Jan 2023
Modifié(e) : Torsten le 12 Jan 2023
If "fsolve" with the problem-defining function "find_CP_MSNP" converges, everything should be fine, shouldn't it ? So why did you ask this question ? Do you encounter difficulties ?
In the beginning, I didn't know if the guess value is passing through all the cascaded functions and if I followed the right approach. This is because I get different answers when I use different guess vlues and and it is also different from the laboratory results which should be similar with. But now, I have a feeling that at leaste my approach makes sense and I need to check other things.
You also helped me at the start of my modeling. I got important feedbacks from you thank you very much for your time.
I hope it is still fine if I ask others questions, if something goes unclear.
It's not unusual that you get different results from "fsolve" if you use different initial guesses for the solution.
In the list of return parameters, you should check "exitflag" and "fval" to get an idea of whether "fsolve" succeeded or not.
Since "fsolve" wants to make the residuals equal to 0, it's not necessary to square y(1:3) - Cp_guess(1:3) elementwise. Just use
sol = Cp_calc(1:3) - Cp_guess(1:3)
Tadele
Tadele le 12 Jan 2023
Modifié(e) : Tadele le 12 Jan 2023
That is a very good point. I will check the return parameters. I will also use just y(1:3) - Cp_guess(1:3) without squaring.
Thank you very much, Torsten. It helps me a lot.

Connectez-vous pour commenter.

Réponses (0)

Question posée :

le 12 Jan 2023

Modifié(e) :

le 12 Jan 2023

Community Treasure Hunt

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

Start Hunting!

Translated by