lsqnonlin with matrices Problem to define the function

15 vues (au cours des 30 derniers jours)
Juan Guerrero-Fernandez
Juan Guerrero-Fernandez le 6 Fév 2019
Modifié(e) : Matt J le 6 Fév 2019
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];
B=[b1;b2;b3;b4];
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
h_test>@(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])
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
continue.
Now
  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!

Réponse acceptée

Alan Weiss
Alan Weiss le 6 Fév 2019
Modifié(e) : Alan Weiss le 6 Fév 2019
As clearly stated in the documentation, "...an 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 commentaires
Juan Guerrero-Fernandez
Juan Guerrero-Fernandez le 6 Fév 2019
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 le 6 Fév 2019
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

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by