How to use "fsolve" for optimizing the objective function?
Show older comments
How can I optimize the below function using "fsolve". It only takes input argument in the form of vectors but I have to pass input arguments to the below function in the form of matrix.
I am getting the below error while using the optimization toolbox
Error running optimization. Inner matrix dimensions must agree.
function f = object(w)
k=10;
B=20;
f = sum ((w(1,:)/(w(2,:).^w(3,:)*k)+((w(3,:)-1)*w(4,:)/B*w(3,:))));
end
Answers (1)
Alan Weiss
on 10 Feb 2017
You have many problems with your formulation. One of them is this line:
f = sum ((w(1,:)/(w(2,:).^w(3,:)*k)+((w(3,:)-1)*w(4,:)/B*w(3,:))));
You cannot divide a vector w(1,:) by a vector w(2,:) by using /. Instead, you must use ./, such as
f = sum ((w(1,:)./(w(2,:).^w(3,:)*k)+((w(3,:)-1).*w(4,:)./B.*w(3,:))));
While this change will allow your code to run, it does not end the problems with your code.
- It is bad form to name a function object, as this name is a MATLAB command.
- fsolve prefers a vector, not scalar, as the value of its objective function. Likely (but not definitely), you should get rid of the sum command in the definition of f.
- If I read your code correctly, then setting w(1,:) = 0 and w(3) = 1 will solve the equations. So why ask fsolve?
Alan Weiss
MATLAB mathematical toolbox documentation
Categories
Find more on Solver Outputs and Iterative Display 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!