Not enough input arguments in embedded function when using fsolve!

1 view (last 30 days)
I have a code which itself is a function but I have also many embedded functions in it. The main function defines a system of non-linear equations. Input arguments to this code are [w, del, v, Pg, Qg]. When I define input arguments and pass them to the function there is no problem and I have output. But when I want to solve it using fsolve (with trust-region-dogleg, because my non-linear system of equations is square) I receive "not enough input arguments" error for one of my embedded functions. What should I do? Why I don't get any errors in case of not using fsolve, but I get the mentioned error while using fsolve?
  4 Comments
Torsten
Torsten on 8 Jul 2015
What are the variables you want fsolve to solve for ?
What are the equations you want fsolve to solve ?
Where is the call to fsolve ?
Best wishes
Torsten.
Mohmmad Teymouri
Mohmmad Teymouri on 8 Jul 2015
fsolve should solve for [w,del,v,Pg,Qg]
It should be noted that w is a scalar. size(del)=size(v)=[25 3], size(Pg)=size(Qg)=[3 3].
The equations are mentioned in output in end of the mentioned codes.
The point is that for this case which is a 25-bus power grid I have 56*3 equations. And also regarding the variables I have 56*3+1 variables. w is unknown and also del is unknown. But for making a reference regarding which the other elements of del will be measured, I set del(1,1)=0.
I don't know also how should I feed this to fsolve, to know that del(1,1)=0 is a known variable and not one of the unknown variables, therefore I have a square problem in which I have 56*3 equations and 56*3 variables.
I call fsolve in command line in MATLAB as follows:
OPTIONS=optimoptions('fsolve','algorithm','trust-region-dogleg');
X=fsolve(@problem, [w,del,v,Pg,Qg],OPTIONS)

Sign in to comment.

Accepted Answer

Brendan Hamm
Brendan Hamm on 8 Jul 2015
Edited: Brendan Hamm on 8 Jul 2015
First off your function to be evaluated should accept only a vector as input. I notice you are providing an initial point:
x0 = [w,del,v,Pg,Qg];
If size(del) = [25 3], and w is a scalar, then you cannot concatenate them into a vector as it seems you are doing:
del = rand(25,3);
w = 4;
x0 = [w, del]
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
What you need to do is rearrange all of the design variables into a vector to be passed into your objective function. Inside your objective function you can then go ahead and extract them into their respective matrices etc..
x0 = [w;del(:);v(:);Pg(:),Qg(:)]; % A big long vector
X = fsolve(@problem,x0,OPTIONS);
function xnew = problem(x)
w = x(1);
del = x(2:76);
del = reshape(del,25,3);
v = x(77:151);
v = reshape(v,25,3);
Pg = x(152:160);
Pg = reshape(Pg,3,3);
Qg = x(161:169);
Qg = reshape(Qg,3,3);
% Rest of your function here
end
  4 Comments
Mohmmad Teymouri
Mohmmad Teymouri on 9 Jul 2015
Dear sir, your comments are really useful and helpful for me.
I have done your last comment and till now I have no problem running my code.
But there is a matter which makes my work still incomplete.
When I feed the variables as a (168*1) matrice, and after that sort it in my function (for example to reshape del, v, and so on) this codes are the first lines of my function (after declaring the function name of course).
Because of this in every iteration (when using fsolve) the assigned values are updated to their initial value, and therefore I have no progress in my convergence.
The question is that how could I make fsolve update my special variables (w, del, v, Pg, Qg) in every iteration to their new value? Also I mention that I need to keep del(1,1)=0 (always unchanged).
With best regards and thanks in advance
Brendan Hamm
Brendan Hamm on 14 Jul 2015
I should mention that the output of the function problem should be the function evaluation F(x) at the value of x passed in. fsolve will then attempt to find the x which satisfies F(x) = 0. So the definition of problem I give is not complete and it should actually compute the function evaluated at x, I just provide the method by which the objective function needs to be defined (1 input of design variables).
Once the function definition is complete and the anonymous function objFun created, you just need to pass objFun to the fsolve function.
x0 = rand(168,1);
x = fsolve(objFun,x0);
disp(objFun(x)) % Should be close to zero.
So fsolve first passes the values in x0 to your objective function and evaluates it. It then shifts x0 by a small amount to approximate the gradient and choose a direction to move each element of x in. It passes this new value of x back to the objective function and repeats until a termination criteria is met when it returns to you the vector which caused termination.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 8 Jul 2015
fsolve passes in a vector of values. Your routine expects 4 values. Try
fsolve(@(x) problem(x(1),x(2),x(3),x(4)),......

Categories

Find more on Systems of Nonlinear Equations 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!