fsolve error when I change the given matrix

7 ビュー (過去 30 日間)
Quynh tran
Quynh tran 2016 年 4 月 7 日
編集済み: Walter Roberson 2016 年 4 月 8 日
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.

採用された回答

Walter Roberson
Walter Roberson 2016 年 4 月 7 日
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.
  1 件のコメント
Quynh tran
Quynh tran 2016 年 4 月 8 日
Thank you very much, I found may errors.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by