fzero: Not Enough Input Arguments

2 ビュー (過去 30 日間)
Ash Ash
Ash Ash 2018 年 6 月 27 日
コメント済み: Ameer Hamza 2018 年 6 月 28 日
EDIT: I have edited this section of my question as advised by Ammer Hamza.
Problem Statement:
To minimise an underconstrained linear symbolic system equations in the form of:
a1*X1i + a2*X2i + a3*X3i + a4*X4i + a5*X5i = Yi
(where i=1 to 100)
I have 5 coefficients (X1 to X5), which I have as inputs from a spreadsheet, 5 unknown variables (a1 to a5), and 100 equations.
======================================================================
Hi, I have referred to the other threads on the same error message but I am still unable to solve my problem. I have an underconstrained linear symbolic equation generated from other variables that I would like to minimise. Below is a simplified example of the script I have:
EDITED after comments from Alan Weiss:
a=sym('a',[2,1]);
a1=a(1);a2=a(2);
eqn_a(1)=a(1)+a(2)-3;
eqn_a(2)=2*a(1)+a(2)-4;
eqn_a(3)=a(1)-a(2)+1;
fun=matlabFunction(eqn_a);
x0=[1 10];
fzero(fun,x0)
This is the error message I get:
Error using fzero (line 241)
FZERO cannot continue because user-supplied function_handle ==> @(a1,a2)[a1+a2-3.0,a1.*2.0+a2-4.0,a1-a2+1.0] failed with the error
below.
Not enough input arguments.
Additionally, is this a suitable function or the best function to solve this problem? I have 5 coefficients, 5 variables and 100 equations.
Thank you very much.
  3 件のコメント
Ash Ash
Ash Ash 2018 年 6 月 27 日
編集済み: Ash Ash 2018 年 6 月 27 日
Thank you for your reply Ameer. This is my problem statement:
Problem Statement:
To minimise an underconstrained linear symbolic system equations in the form of:
a1*X1i + a2*X2i + a3*X3i + a4*X4i + a5*X5i = Yi
(where i=1 to 100)
I have 5 coefficients (X1 to X5), which I have as inputs from a spreadsheet, 5 unknown variables (a1 to a5), and 100 equations.
Ameer Hamza
Ameer Hamza 2018 年 6 月 27 日
Now the problem is clear. This problem has a much easier solution in MATLAB as compared to the using syms, writing all equations and using fminunc(). Please refer to my answer.

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

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 6 月 27 日
Since your number of equations (100) is greater than the number of variables (5), the best you can expect is a least square solution, i.e. formulate this as a least square optimization problem as you rightly mentioned in your question. Fortunately solving such system for unconstrained cases require just one line in MATLAB. You don't need to use any function such as fminunc().
X = [x1 x2 x3 x4 x5]; % values from spreadsheet, it will be a [100 x 5] matrix
Y = y; % value of |y| from the spreadsheet.
coefficients = X\Y; % that's it
coefficients will contain the value of a1 to a5 in order.
  2 件のコメント
Ash Ash
Ash Ash 2018 年 6 月 27 日
編集済み: Ash Ash 2018 年 6 月 27 日
Thank you! I didn't know that mldivide,\ was that powerful, I thought it only worked for a square system.
I would like to compare my results from mldivde against fzero, and I have made another attempt based on Alan's advice. Would you please guide me on how to proceed?
Thank you.
Ameer Hamza
Ameer Hamza 2018 年 6 月 28 日
fzero can only be used when an exact solution to your system of equation exist. Since you are solving it as a least square problem, fzero is not the right tool. There are several other tools which you can look into, few are listed below with examples for your matrices X and Y,
  • lsqlin()
coefficients = lsqlin(X,Y,[],[])
  • lsqcurvefit()
f = @(a, X) X*a;
coefficients = lsqcurvefit(f, [0;0;0;0], X, Y)
  • fitlm()
coff = fitlm(X,Y,'y~x1+x2+x3+x4+x5-1')
fitlm() will provide much more details than other methods but the all of them will give the same estimate for a since all of them are solving the same problem.

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

その他の回答 (1 件)

Alan Weiss
Alan Weiss 2018 年 6 月 27 日
As formulated, your problem does not make sense.
  • You give a scalar value of x0, but you have a 2-D problem.
  • You give a 3-output function eqn_a. when objective functions must be scalar-valued.
  • You are attempting to solve a set of equations using fminunc, when fsolve is the appropriate solver for a system of equations.
  • You do not give the equations in the correct syntax for fsolve.
  • For systems of linear equations, you should probably use backslash ( mldivide )
I suggest that you read the documentation on systems of linear equations.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 件のコメント
Ash Ash
Ash Ash 2018 年 6 月 27 日
編集済み: Ash Ash 2018 年 6 月 27 日
Thank you for your reply. I am not experienced with using function handles and objective functions. I have made an attempt with fzero but I am still unable to get it to work. I don't know what am I missing.
I understand that mldivide is recommended, and I have got that to work thanks to Ameer, but I would like to compare my results from mldivide against fzero.
I appreciate your help and guidance.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by