Info
This question is closed. Reopen it to edit or answer.
Objection fuction for fsolve
1 view (last 30 days)
Show older comments
I have to solve a set of nonlinear equation using fsolve in MATLAB, for which i have to define an objective function like :
function F = myfun(f)
A = [1 2 3; 4 5 6; 4 2 1];
F = [f(1)^2 + sum(A(:,1).*f);
f(2)*sum(A(:,2).*f);
sum(A(:,3).*f)];
end
but it doesn't work! coefficients are in matrix form inside fuction and number of variables also change and so does the cofficeint matrix size.
error is not enough input argument
0 Comments
Answers (1)
Star Strider
on 3 May 2014
I need you to specify what f is in your function. I simply named it f(3), but if it is supposed to be a function, I need to know what function it is (at least one example as to what it might be).
If f is a function (more accurately, function handle) to be passed as an argument to myfun, what are f(1) and f(2)? If they are parmaeters you want to extimate, you need to name them something else.
I wrote an anonymous function for your code, and came up with:
myfun = @(f,A) [f(1)^2 + sum(A(:,1).*f(2));
f(2)*sum(A(:,2).*f(3));
sum(A(:,3).*f(3))];
A = [1 2 3; 4 5 6; 4 2 1];
fa = fsolve(@(f) myfun(f,A), ones(3,1))
This produced:
fa =
9.5317e-003
-10.0948e-006
0.0000e+000
2 Comments
Star Strider
on 4 May 2014
One of the significant strengths of MATLAB is making vector and matrix operations easy. Since you want to calculate the dot (inner) product of the first column of A by f, you need to transpose A so the first column of A becomes a row vector, and then do the multiplication:
b = A(:,1)' * f
This does the same as sum(A(:,1).*f.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!