Reputation: 843
This error is driving me crazy! I've spent 3 hours searching for what the cause might be and how to fix it but to no avail. I simply want to use fsolve
to solve a non linear system of equations. Here is what I've done:
Creating a function file named myfun.m
:
function F = myfun(x)
F = [x(1)*x(2)+exp(x(1))+x(1)-3;
x*sin(x)+(x(2))^2-2];
end
Call upon my function to compute the roots :
x0 = [0,0];
fsolve(@myfun,x0)
Yes, I have saved the functionfile with a name "myfun" in the exact same path as the rest of my Matlab files.
For this method, I followd this guy: https://www.youtube.com/watch?v=WPVuyJ3uHhE
It works perfectly for him but not for me. Does anyone what causes my error? Here follows the whole error message :
Undefined function 'myfun' for input arguments of type 'double'.
Error in fsolve (line 230)
fuser = feval(funfcn{3},x,varargin{:});
Error in Bonus_a (line 14)
fsolve(@myfun,x0)
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Upvotes: 0
Views: 1963
Reputation: 1460
First, your second equation is incorrect because you mix x
as vector and x
as scalar. The corrected code would be something like this:
function F = myfun(x)
if nargin<1,
x=[0;0]
end
F = [x(1)*x(2)+exp(x(1))+x(1)-3;
x(1)*sin(x(2))+(x(2))^2-2]
end
The first three lines after the function definition is useful addition to test your function without the need to call it from other functions. Running this function alone will give you results such as
ans =
-2.00
-2.00
Now you can use the optimization:
x0 = [0,0];
fsolve(@myfun,x0)
This will produce results without error:
ans =
0.56 1.21
Upvotes: 3
Reputation: 23695
Actually, I see two problems here. The first one is that the exception this code should actually produce is not:
Undefined function 'myfun' for input arguments of type 'double'.
but:
Error using *
Inner matrix dimensions must agree.
So, unless you are using a Matlab version different from mine (2017a) and consistent changes have been made on the inner data validation code of fsolve
... I think there is a mismatch between the code you posted and the code you are actually running.
Anyway, the problem arises from the fact that you are trying to solve a system of equations with two uknowns but the code in your function is a mix between a vectorized calculation and an indexed calculation with errors in between (if you want to perform an element wise multiplication between two vectors, you must use the .*
operator instead of *
, the same goes for ^
and so on). Once every computation has been performed, the output argument F
of myfun
must contain no less and no more than 2
elements.
Basically, the second equation must be changed so that it returns a single scalar, for example like this (it's up to you to choose the proper offsets 1
and 2
of x
in the second equation, since you didn't provide the original formulation of your problem):
function F = myfun(x)
F = [
x(1)*x(2)+exp(x(1))+x(1)-3;
x(1)*sin(x(1))+(x(2))^2-2
];
end
If you run the code with the above function, it will output the following without errors:
ans =
1.10175749069473 -1.0085776227304
Upvotes: 1