Not enough input arguments in embedded function when using fsolve!
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2015-7-8
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.
采纳的回答
Brendan Hamm
2015-7-8
编辑:Brendan Hamm
2015-7-8
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 个评论
Brendan Hamm
2015-7-14
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.
更多回答(1 个)
Walter Roberson
2015-7-8
fsolve passes in a vector of values. Your routine expects 4 values. Try
fsolve(@(x) problem(x(1),x(2),x(3),x(4)),......
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!