MATLAB Answers

lsqnonlin with matrices Problem to define the function

18 ビュー (過去 30 日間)
Juan Guerrero-Fernandez
Juan Guerrero-Fernandez 2019 年 2 月 6 日
編集済み: Matt J 2019 年 2 月 6 日
Hi Guys,
I am a bit new to topic of optimization, and I need some help with a nonlinear-least square problem.
I have a time vector t and the value for the function at each time t .
Knowing h can be approximated by:
For the matrices:
A=[0 0 0 -a1
1 0 0 -a2
0 1 0 -a3
0 0 1 -a4];
C=[0 0 0 1];
where the unknowns are a1, a2, a3, a4, b1, b2, b3, b4.
I tried to used the function lsqnonlin
fun = @(a1,a2,a3,a4,b1,b2,b3,b4) h - (C*expm([0 0 0 -a1;1 0 0 -a2;0 1 0 -a3; 0 0 1 -a4]*t)*[b1;b2;b3;b4]);
x0 = [1 1 1 1 1 1 1 1];
x = lsqnonlin(fun,x0)
But I am getting some errors:
Not enough input arguments.
Error in
Error in lsqnonlin (line 206)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in h_test (line 35)
x = lsqnonlin(fun,x0)
Caused by:
Failure in initial objective function evaluation. LSQNONLIN cannot
  1. It looks like there are some problems with the anonymous function fun.
  2. I still have some doubts about the correct way to built the anonymous function, specially in the argument of the exponential function: expm(A*t), becuse here I have a matrix A times a vector t, however matrix A have to by multiplied by the time corresponding to the value of h. Let's say that in a for loop it should be something like: h(k) - (C*expm(A* t(k))*B), but I have no idea how that could be implemented in anonymous functions.
Any help are welcome,
Thanks in advance!

  0 件のコメント

サインイン to comment.


Alan Weiss
Alan Weiss 2019 年 2 月 6 日
編集済み: Alan Weiss 2019 年 2 月 6 日
As clearly stated in the documentation, " objective function ... accepts one input, say x." One input, not a1,a2,a3,a4,b1,b2,b3,b4.
You need to map x to a1,a2,a3,a4,b1,b2,b3,b4. So take x(1) = a1, x(2) = a2, x(3) = a3, x(4) = a4, x(5) = b1, x(6) = b2, x(7) = b3, and x(8) = b4. Then your function becomes (assuming h, C, and t are already in your workspace)
fun = @(x) h - (C*expm([0 0 0 -x(1);1 0 0 -x(2);0 1 0 -x(3); 0 0 1 -x(4)]*t)*[x(5);x(6);x(7);x(8)]);
Alan Weiss
MATLAB mathematical toolbox documentation

  2 件のコメント

Juan Guerrero-Fernandez
Juan Guerrero-Fernandez 2019 年 2 月 6 日
Thanks Allan for the aclaration on the input variable and for the proposed way to solve it.
I looks it works for the error of "Not enough input arguments." (the definition of the unknowns on the objective function), however I still have the problem of the multiplication of the Matrix A with the corresponding value of t(k).
expm([0 0 0 -x(1);1 0 0 -x(2);0 1 0 -x(3); 0 0 1 -x(4)]*t), this part is where I think is the error now, but I don't have idea how to solve it. Any help?
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns
in the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'.
I tried using .*, but I have problems with the dimensions of the matrix A with the vector t (which is completely understandable, since matrix A has to be multiplied by the independent variable time (t) and not by the whole vector of time series).
Alan Weiss
Alan Weiss 2019 年 2 月 6 日
The function as I gave it was for scalar t. For vector or matrix values of t, you will have to think it through. Write a loop? Use repmat?
And why do you think that it has to be an anonymous function? If you find it easier to use a function file, well, I encourage you to do so.
Alan Weiss
MATLAB mathematical toolbox documentation

サインイン to comment.

その他の回答 (0 件)

サインイン してこの質問に回答します。

Translated by