how to use fsolve with more equation
Show older comments
function F = fullfun(S_c,S_d,Ru,T,mu_s0,mu_w0,F,lambda_0,alpha,C_c,C_d,V_cp,r_am,r_cm,b_d,sigma_d,sigma_c,N_cp,h_r,sigma)
F = [T_s+(4*10^(-6)*S_d^(2))-(4*10^(-5)*S_d)-0.96;
T_w+(4*10^(-5)*S_c^(2))+(1.9*10^(-2)*S_c)-11.2;
L_s-(min(2*10^(-12)*S_d^(2)-3*10^(-10)*S_d+...
6*10^(-8),2*10^(-12)*S_c^(2)-3*10^(-10)*S_c+6*10^(-8)));
L_w-(5*10^(-4)*S_c^(-0.416));
mu_sc-(mu_s0+Ru*T*log(S_c/1000));
mu_sd-(mu_s0+Ru*T*log(S_d/1000));
mu_wc-(mu_w0+Ru*T*log((1000-S_c)/1000));
mu_wd-(mu_w0+Ru*T*log((1000-S_d)/1000));
E_m-(T_s/F*(mu_sc-mu_sd)+T_w/F*(mu_wc-mu_wd));
E_am-E_m;
E_cm-E_m;
lambda_c-lambda_0+alpha*sqrt(C_c);
lambda_d-lambda_0+alpha*sqrt(C_d);
lambda_r-((lambda_c + lambda_d)/2);
C_r-((C_c+C_d)/2);
k-(lambda_r * C_r);
I-((V_cp-E_am-E_cm)/(r_am+r_cm+b_d/(sigma_d*lambda_d*C_d)+(b_c/(sigma_c*lambda_c*C_c))+...
r_cm/N_cp+2*h_r/(sigma*k*N_cp)));
J_s-(T_s*I/F - L_s*(C_c-C_d));
J_w-(T_w*I/F + L_w*Ru*T*(C_c-C_d)*10^(-5)*2);
Answers (2)
Walter Roberson
on 21 Sep 2019
Bundle all of the variables into a single vector for the purposes of fsolve. You can unbundle them inside the function.
vars20init = vector of 20 initial values
sol = fsolve(@fullfun, vars20init)
function result = fullfun(vars20)
tcell = num2cell(vars20);
[S_c,S_d,Ru,T,mu_s0,mu_w0,F,lambda_0,alpha,C_c,C_d,V_cp,r_am,r_cm,b_d,sigma_d,sigma_c,N_cp,h_r,sigma] = tcell{:};
result = [T_s+(4*10^(-6)*S_d^(2))-(4*10^(-5)*S_d)-0.96;
T_w+(4*10^(-5)*S_c^(2))+(1.9*10^(-2)*S_c)-11.2;
L_s-(min(2*10^(-12)*S_d^(2)-3*10^(-10)*S_d+...
6*10^(-8),2*10^(-12)*S_c^(2)-3*10^(-10)*S_c+6*10^(-8)));
L_w-(5*10^(-4)*S_c^(-0.416));
mu_sc-(mu_s0+Ru*T*log(S_c/1000));
mu_sd-(mu_s0+Ru*T*log(S_d/1000));
mu_wc-(mu_w0+Ru*T*log((1000-S_c)/1000));
mu_wd-(mu_w0+Ru*T*log((1000-S_d)/1000));
E_m-(T_s/F*(mu_sc-mu_sd)+T_w/F*(mu_wc-mu_wd));
E_am-E_m;
E_cm-E_m;
lambda_c-lambda_0+alpha*sqrt(C_c);
lambda_d-lambda_0+alpha*sqrt(C_d);
lambda_r-((lambda_c + lambda_d)/2);
C_r-((C_c+C_d)/2);
k-(lambda_r * C_r);
I-((V_cp-E_am-E_cm)/(r_am+r_cm+b_d/(sigma_d*lambda_d*C_d)+(b_c/(sigma_c*lambda_c*C_c))+...
r_cm/N_cp+2*h_r/(sigma*k*N_cp)));
J_s-(T_s*I/F - L_s*(C_c-C_d));
J_w-(T_w*I/F + L_w*Ru*T*(C_c-C_d)*10^(-5)*2);]
end
I notice you use F as both input and output. That is unlikely to be a good idea when you are calling fsolve with the function.
2 Comments
chirag patel
on 22 Sep 2019
Edited: chirag patel
on 22 Sep 2019
Walter Roberson
on 22 Sep 2019
You use T_s in your function, but it is not a parameter to the function. Where do you want your code to find T_s from?
chirag patel
on 23 Sep 2019
3 Comments
Walter Roberson
on 23 Sep 2019
Equation solved, solver stalled.
That means fsolve worked and found an answer to you that was within the constraints you specified. Not all of the function outputs were exactly zero, so it kept looking for a better solution, trying smaller and smaller steps near the locations it had arrived at, but was unable to find any improvement by the time it reached the minimum step size you had configured.
Is the solution perfect ? No, it is not. But it is within the tolerances you set.
You should never expect perfect solutions, because of round-off and truncation effects. For example you cannot even solve x^2-2 perfectly because of those effects.
>> 2-sqrt(2)^2
ans =
-4.44089209850063e-16
chirag patel
on 23 Sep 2019
Walter Roberson
on 23 Sep 2019
Okay, go ahead. Follow the same strategy as before: all inputs should go into a single vector, and when you get the vector output from the solver, break it up into meaningful variables. Inside the function to be solved, expect everything as a single input, and break the values out into meaningful variables (this is what the num2cell and {:} is for)
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!