Why do I get different outputs for fsolve when I use different initial point?
4 次查看(过去 30 天)
显示 更早的评论
Dear experts,
I was trying to solve the function cascade shown below using the fsolve on the second script. The problems uses a guess value for calculation of the functions on the first code below. The gues value will also be used on the end solution of the second code. But when I use different initial conditions, I get different answers. Can you please help me?
- Below I put my two code scripts (one to call functions and the other to run the solver)
function [sol] = find_CP_MSNP(Cp_guess, phi_start, Jv,Z, z_pol,phi,X_mem)
Cp_guess_MS = [Cp_guess 1- sum(Cp_guess)];
[y0, yp0] = decic(@MS_AA_imp, 0, [phi_start 0], [0 0 0 0 0], [1 1 0 0 0], [],[],Cp_guess_MS, Jv, Z);
[zsol_MS, ysol_MS] = ode15i(@(z,x,xp)MS_AA_imp(z,x,xp ,Cp_guess_MS, Jv, Z), [0 z_pol], y0, yp0);
zz=[0.00001 0.00001 0.00001 0.000001];
z=[0.00001 0.00001 0.00001];
C_surf = ysol_MS(end,:);
% define anonymous function 'f' for the partition function which accepts C_surf as an input parameter
f = @(zz)partitionFirst(zz,C_surf(1:3),Z,phi,X_mem);
% solve partitionFirst function to get C_entrance indirectly via anonymous function 'f'
C_entrance = fsolve(f,zz);
% define anonymous function 'nernstPlanckAnonymous' for the NernstPlanck_Vol function which accepts C_entrance as a parameter
% solve NernstPlanck_Vol function to get C_end indirectly via anonymous function 'nernstPlanckAnonymous'
nernstPlanckAnonymous = @(z)NernstPlanck_Vol(z,C_entrance(1:3));
C_end = fsolve(nernstPlanckAnonymous,z); %concentration on the end of membrane
%%second partition to calculate the permeate concentrate
secondpartitionAnonymous = @(z)partitionsecond(z, C_end(1:3)); %%%replaceing Co from partitionsecond to C_end for the next calculation of Cp_cal
Cp_calc = fsolve(secondpartitionAnonymous,z);
sol = Cp_calc(1:3) - Cp_guess(1:3);
end
2. The second code to solve this function is
%% fsolve for permeate concentration with linearized pore equation
global TempK Faraday R_id D_14_inf D_24 D_34
global VM_1 VM_2 VM_3 VM_4
TempK = 25 + 273.15; % Temp in Kelvin
VM_1 = 1.8e-5; % lysine molar volume [m3/mol]
VM_2 = 1.57e-5; % Na molar volume [m3/mol] * Calculated from rS
VM_3 = 4.47e-6; % Cl molar volume [m3/mol] * calcualted form rS
VM_4 = 1.8e-5; % Water molar volume [m3/mol]
D_14_inf = 6.1e-11; % D14 value under diluted conditions and IEP [m^2/s]
D_24 = 1.33E-9; % D24 value under diluted conditions [m^2/s]
D_34 = 2.03E-9; % D34 value under diluted conditions [m^2/s]
Faraday = 96485.336; % Faraday constant [C/mol]
R_id = 8.314; % R constant [J/(K mol)]
z_pol = 1.23e-5;
r_AA = 3.48e-9;
r_Na = 102e-12; % ion size
r_Cl = 181e-12; % ion size
rs = [r_AA; r_Na; r_Cl];
D = [ D_14_inf; D_24; D_34];
Vs = [VM_1; VM_2; VM_3];
rp = 9.75e-9;
Le = 1.65e-4; % effective membrane thickness
[Kc, Kd, DP, phi] = HindranceFactor(rs, rp, D);
X_mem = -0.5;
C_ret_arb1 = [0.02 0.02 0.02]; % using average value of pH 7 0.15 M
C_ret_arb=[C_ret_arb1 1-sum(C_ret_arb1)];
Z_arb = [-1 1 -1];
Cp_guess_arb = [0.003 0.003 0.003]; % I changed the above line into the current line by removing C_ret
%% for arbitrary flux value
options = optimoptions('fsolve', 'Display','iter','algorithm', 'levenberg-marquardt','FunctionTolerance', 1e-4,'StepTolerance', 1e-4,'MaxFunctionEvaluation', 300);
Jv_arb = 5e-6;
[Cp_calc_arb, fval, exitflag] = fsolve(@(y)find_CP_MSNP( y, C_ret_arb,Jv_arb,Z_arb, z_pol,phi,X_mem), Cp_guess_arb(1:3),options); %from the line above
%have to find out why.
if exitflag < 1
iter = 0;
mod = 0.01;
while xor(exitflag < 1, iter >50)
Cp_guess_arb = C_ret_arb .* [mod 1 1 1]; % can change all the value, this is just for demonstration purposes
[Cp_calc_arb,fval, exitflag] = fsolve(@(y)find_CP_MSNP( y, C_ret_arb, Jv_arb, Z_arb, ...
z_pol), Cp_guess_arb(1:3), options);
iter = iter +1
mod = mod + 0.01;
end
end
Cp_calc_arb
0 个评论
采纳的回答
Matt J
2023-2-5
编辑:Matt J
2023-2-5
Why do I get different outputs for fsolve when I use different initial point?
Because you have local solutions. You need to choose your initial guess as close as possible to the global solution.
4 个评论
Matt J
2023-2-5
Can I say that the approach I followed looks correct?
The use of global variables is discouraged. It would be better to use anonymous or nested functions as discussed in,
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!