Info

This question is closed. Reopen it to edit or answer.

Objection fuction for fsolve

1 view (last 30 days)
nuri
nuri on 3 May 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (1)

Star Strider
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
nuri
nuri on 4 May 2014
f is set of variables with 3 components
f(1) is x; f(2) is y and f(3) is z
when I do
sum(A(:,1).*f)
% which is A(1,1)*f(1) + A(2,1)*f(2) + A(3,1)*f(3)
% I dont want to manually do elementwise multiplication
% matrix is to be defined inside function and no. of variables (size of f is varying), so does size of matrix and no. of equation in F
Star Strider
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.

Community Treasure Hunt

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

Start Hunting!