Clear Filters
Clear Filters

fsolve error when I change the given matrix

1 view (last 30 days)
I have my prolems like this:
function F=myfun(x)
F=[x(1)+a*x(2)+b*x(3)
x(1)+5*x(2)+4*x(3)
x(1)+2*x(2)+3*x(3)]
P=[1 5 2
3 4 7] %I want to keep this matrix to make easier when I change a and b
a=P(1,1), b=P(1,3)
x0=[0 0 0]
[x,fval,exitflag]=fsolve(@myfun,x0)
and the error is:
Attempt to execute SCRIPT a as a function:
D:\Tinhtraoluucongsuat\flie goc\opt - KG_3_current_constraint_3_7_2015\a.m
Error in myfun (line 2)
F=[x(1)+a*x(2)+b*x(3)
Error in fsolve (line 219)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Apr 2016
On your line
F=[x(1)+a*x(2)+b*x(3)
you have not defined a or b yet. MATLAB assumes they might be functions, and goes looking for them. It finds an a.m that you have sitting around, but it happens that a.m is a script rather than a function, so it fails trying to execute it as a function to get a value for "a".
In MATLAB, if you name a function without passing it any parameters, it is the same as if you had used (), so MATLAB is treating your line as if it had been written
F=[x(1)+a()*x(2)+b()*x(3)
I suggest that you move your lines
P=[1 5 2
3 4 7] %I want to keep this matrix to make easier when I change a and b
a=P(1,1), b=P(1,3)
to before you assign anything to F.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!