Solving a non-linear problem for several lines

Hello!
I'm trying to solve non linear equations for 8760 lines, meaning each line has a non-linear equation, all the vectors are of the size (8760,1). The code is below:
% Model start
Vwind_low = data(:,4)*0.51;
T_water = mi + ((alpha-mi)./(1+exp(gamma*(beta-data(:,3)))));
Tcell = 1.8081+0.9282*data(:,3)+0.021*data(:,2)-1.221*Vwind_low+0.0246*T_water;
TcellK = Tcell+273.15;
Isc = Isc_ref*data(:,2)/G_ref;
A = A_ref*TcellK/T_STC;
Ebg = 1.12*(1-0.0002677*(TcellK-T_STC));
Io = Io_ref*((TcellK/298.15).^3).*exp((1/k)*((1.12/T_STC)-(Ebg./TcellK)));
% Objective function
fun = @myfun
x0 = ones(8760,1)*39; % Initial guess is 39
for i=1:8760
x(i) = fsolve(fun,x0);
end
My myfun file is below:
function F = myfun (x)
for j=1:8760
F = exp(x(i)./A(j)) - ((Isc(j)./Io(j) + 1)./(1 + x(i)./A(j)));
end
My question is: the non-linear equation depends on the parameters A, Isc and Io that I calculated in the main code. How do I call these calculated parameters to the function?
Also, is this the way you guys would solve this problem? Is there another way?
Thank you!

9 comentarios

I was able to use Parameterizing Using Anonymous Functions for the code as below, which worked:
x0 = 39*ones(8760,1); % Initial guess
Vmp=zeros(8760,1);
for i=1:8760
objective = @(x) exp(x/A(i)) - (Isc(i)/Io(i) + 1)/(1 + x/A(i));
x = fsolve(objective,x0(i));
Vmp(i)=x;
end
However, now I need to implement a second equation here, therefore "x" was one paramater but now, for example, "Isc" will be the other parameter (2 equations 2 unknows, both non linear). Is there a way to implement it in the code I did or would I have to do nested functions?
Thank you.
put the two unknown into a vector and index the vector as needed.
Bruno Martins
Bruno Martins el 12 de En. de 2019
I’m sorry Walter, I didn’t understand what you meant, could you give an example, please?
Both equations will be dependent on let’s say x1 and x2 and are of course different from eachother. It is a system of two equations for every value of i.
objective =@(xx) [f1(xx(1),xx(2)), f2(xx(1), xx(2))]
vector input indexed as needed in two expression in a list .
Hello Walter,
I implemented what you said as follows (the formulas are big, but whatever)
x0=[39; 9];
Vmp=zeros(8760,1);
Imp=zeros(8760,1);
for i=1:8760
%x(1)=vmp, x(2)=imp
objectiveVI = @(xx) [xx(2)+xx(1)*(-(1/Rsh(i))-((Rsh(i)*Isc(i)-Voc(i)+Rs*Isc(i))/(A(i)*Rsh(i)))*exp((xx(1)+Rs*xx(2)-Voc(i))/A(i))/((1+(Rs/Rsh(i))+Rs*(Rsh(i)*Isc(i)-Voc(i)+Rs*Isc(i))/(A(i)*Rsh(i)))*exp((xx(1)+Rs*xx(2)-Voc(i))/A(i)))), -xx(2)+ Isc(i) - ((Isc(i) - (Voc(i)-Rs*Isc(i))/Rsh(i))*exp((xx(1)+Rs*xx(2)-Voc(i))/A(i))) - ((xx(1)+Rs*xx(2)-Rs*Isc(i))/Rsh(i))];
xx = fsolve(objectiveVI,x0);
Vmp(i)=xx(1);
Imp(i)=xx(2);
end
Although it is returning the following error:
Error using trustnleqn (line 28)
Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 408)
trustnleqn(funfcn,x,verbosity,gradflag,options,defaultopt,f,JAC,...
Error in Floating_standard (line 67)
xx = fsolve(objectiveVI,x0);
Is my structure wrong? I cannot seem to solve the error.
we cannot tell without access to your data. For example A might be the wrong size.
Use
dbstop if caught error
to debug.
Bruno Martins
Bruno Martins el 14 de En. de 2019
Editada: Bruno Martins el 14 de En. de 2019
I got the code finished finally! Thanks a lot for the help, Walter.
PS.: I can't find where to accept your answer!
ahmad mohmad
ahmad mohmad el 16 de Sept. de 2019
hello mr. Bruno
colud you help me ih have a such problem
Thanks

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 12 de En. de 2019

Comentada:

el 16 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by