5 equations in 5 unknowns with fsolve

5 views (last 30 days)
M_Shaban
M_Shaban on 24 Dec 2021
Commented: M_Shaban on 25 Dec 2021
how to solve these equations with fsolve function or any function
and this my code:
clc
clear
close all
function main
P0 = rand(5,1);
P = fsolve(@myfun,P0);
end
function F = myfun(p)
Isc = p(1);
Iph = p(2);
Imp = p(3);
Vmp = p(4);
Rsh = p(5);
K = 1.38 * 10^-23;
T = 298;
q = 1.602 * 10^-19;
Ns = 12;
Voc = 21.7;
Isc = 3.45;
Imp = 3.15;
Vmp = 17.4;
F(1) = (Iph - Io*exp(((q/(A*K*T))*Isc*Rs) - 1) - ((Isc*Rs) / (Rsh))) - Isc;
F(2) = (Io*exp(((q/(A*K*T*Ns))*Voc) - 1) - ((Voc) / (Rsh*Ns))) - Iph;
F(3) = (Io*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs))) - 1) - ((Vmp+(Imp*Ns*Rs)) / (Rsh*Ns))) - Imp;
F(4) = ((Imp*((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (Rs/Rsh))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (1/(Rsh*Ns)))) - (Vmp) ;
F(5) = (((1+((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T))*(Isc*Rs))))) + (Rs/Rsh)))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T))*(Isc*Rs))))) - (1 / (Rsh*Ns)))) - (Rsh);
end
These equations
These known parameters
These unknown Parameters
  8 Comments
Walter Roberson
Walter Roberson on 25 Dec 2021
syms p [1 5]
eqn = myfun(p).'
eqn = 
sol = solve(eqn)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
sol = struct with fields:
p1: 623.56321836916405737252534979669 p2: 522866116720.47960924503351867384 p3: 0.000001738010020294959200618660648178 p4: 0.000000060482748706264580181529390556595 p5: 0.034800000000444295968636998564308
function F = myfun(p)
Isc = p(1);
Iph = p(2);
Imp = p(3);
Vmp = p(4);
Rsh = p(5);
K = sym(1.38) * 10^-23;
T = sym(25+273);
q = sym(1.602) * 10^-19;
Ns = sym(12);
Voc = sym(21.7);
%Isc = sym(3.45);
%Imp = sym(3.15);
%Vmp = sym(17.4);
Io = sym(5.5118e-6);
A = sym(1.7571);
Rs = sym(2.9e-3);
F(1) = (Iph - Io*exp(((q/(A*K*T))*Isc*Rs) - 1) - ((Isc*Rs) / (Rsh))) - Isc;
F(2) = (Io*exp(((q/(A*K*T*Ns))*Voc) - 1) - ((Voc) / (Rsh*Ns))) - Iph;
F(3) = (Io*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs))) - 1) - ((Vmp+(Imp*Ns*Rs)) / (Rsh*Ns))) - Imp;
F(4) = ((Imp*((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (Rs/Rsh))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (1/(Rsh*Ns)))) - (Vmp) ;
F(5) = (((1+((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T))*(Isc*Rs))))) + (Rs/Rsh)))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T))*(Isc*Rs))))) - (1 / (Rsh*Ns)))) - (Rsh);
end

Sign in to comment.

Answers (3)

John D'Errico
John D'Errico on 24 Dec 2021
Edited: John D'Errico on 24 Dec 2021
Do you expect your function to do anything? LOOK AT YOUR CODE CAREFULLY. Read the code you wrote yourself. Too often, we forget to do that. It is easy to just assume that what we wrote is what we wanted to write.
function F = myfun(p)
Isc = p(1);
Iph = p(2);
Imp = p(3);
Vmp = p(4);
Rsh = p(5);
It starts as above. So Isc is the first unknown. Ok, No problem there.
But then what do you do IMMEDIATELY AFTERWARDS?
Ns = ;
That will simply cause an immediate syntax error.
And then we see these lines:
Isc = 3.45;
Imp = 3.15;
Vmp = 17.4;
So even though you pass in values for those variables, you immediately overwrite them as CONSTANTS. Maybe you put those lines in when you were debugging the code. Maybe not. But this objective function as written can not be used to solve anything.
The code you show will do nothing useful. It cannot.
It is possible that your real code is different from what we see. But what we see will not even execute, and even if you fixed the syntax error I pointed out, it will still never optimize those variables, because you are overwriting them inside the objective function.
Can I actually try to solve your problem? No. You do not show what is Ns. So I can go no further.

Torsten
Torsten on 25 Dec 2021
This code starts running although producing Inf and NaN values:
function main
P0 = rand(5,1);
P = fsolve(@myfun,P0)
end
function F = myfun(p)
Isc = p(1);
Iph = p(2);
Imp = p(3);
Vmp = p(4);
Rsh = p(5);
K = 1.38 * 10^-23;
T = 25;
q = 1.602 * 10^-19;
Ns = 12; % set since missing in your code
Voc = 21.7;
Io = 1.0; % set since missing in your code
A = 1.0; % set since missing in your code
Rs = 1.0; % set since missing in your code
F(1) = (Iph - Io*exp(((q/(A*K*T))*Isc*Rs) - 1) - ((Isc*Rs) / (Rsh))) - Isc;
F(2) = (Io*exp(((q/(A*K*T*Ns))*Voc) - 1) - ((Voc) / (Rsh*Ns))) - Iph;
F(3) = (Io*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs))) - 1) - ((Vmp+(Imp*Ns*Rs)) / (Rsh*Ns))) - Imp;
F(4) = ((Imp*((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (Rs/Rsh))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (1/(Rsh*Ns)))) - (Vmp) ;
F(5) = (((1+((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T))*(Isc*Rs))))) + (Rs/Rsh)))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T))*(Isc*Rs))))) - (1 / (Rsh*Ns)))) - (Rsh);
end
  3 Comments
Torsten
Torsten on 25 Dec 2021
Sure, but you didn't define them in the code you posted.

Sign in to comment.


Walter Roberson
Walter Roberson on 25 Dec 2021
This is not the only solution.
format long g
p0 = rand(6,1);
Isc = 3.45;
Imp = 3.15;
Vmp = 17.4;
p0(1) = Isc;
p0(3) = Imp;
p0(4) = Vmp;
P = fsolve(@myfun, p0)
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
No solution found. fsolve stopped because the last step was ineffective. However, the vector of function values is not near zero, as measured by the value of the function tolerance.
P = 6×1
2.05488354263412 2.07231772577932 81.604849760093 14.1285080468827 -0.00260376090944614 0.0135113004526667
function F = myfun(p)
Isc = p(1);
Iph = p(2);
Imp = p(3);
Vmp = p(4);
Rsh = p(5);
K = 1.38 * 10^-23;
T = 25+273;
q = 1.602 * 10^-19;
Ns = 12;
Voc = 21.7;
Io = 5.5118e-6;
A = 1.7571;
Rs = 2.9e-3;
F(1) = (Iph - Io*exp(((q/(A*K*T))*Isc*Rs) - 1) - ((Isc*Rs) / (Rsh))) - Isc;
F(2) = (Io*exp(((q/(A*K*T*Ns))*Voc) - 1) - ((Voc) / (Rsh*Ns))) - Iph;
F(3) = (Io*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs))) - 1) - ((Vmp+(Imp*Ns*Rs)) / (Rsh*Ns))) - Imp;
F(4) = ((Imp*((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (Rs/Rsh))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T*Ns))*(Vmp+(Imp*Ns*Rs)))))) + (1/(Rsh*Ns)))) - (Vmp) ;
F(5) = (((1+((Io*(q/(A*K*T)*Rs*exp(((q/(A*K*T))*(Isc*Rs))))) + (Rs/Rsh)))) / ((Io*(q/(A*K*T*Ns)*exp(((q/(A*K*T))*(Isc*Rs))))) - (1 / (Rsh*Ns)))) - (Rsh);
end

Community Treasure Hunt

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

Start Hunting!