Mismatch in vector size within anonymous function

Hi, Im trying to declare a fucntion in matrix form (Jemp) with my input being a vector my issue is that when I try to excute the code I get the following error:
Inner matrix dimensions must agree. Function 'MATLAB Function' (#23.721.737), line 29, column 23: "A*xy_dot(1:10,:)" Launch diagnostic report.
The code is for this is:
b=0.14;%dustance from cog to rear wheels
x_refa=zeros(5,1);%array of references for x to the end of the prediction horizon
y_refa=zeros(5,1);%array of references for y to the end of the prediction horizon
t_ref=zeros(10,1);
x_refa=circshift(x_refa,-1);
y_refa=circshift(y_refa,-1);
x_refa(5)=x_ref;
y_refa(5)=y_ref;
t_ref(1:5)=x_refa;
t_ref(6:10)=y_refa;
Q=ones(5,5);
A=ones(10,5);
H=ones(5,5);
H=tril(H);
A(1:5,:)=H;
A(6:10,:)=H;
A(1:5,1)=x_cur;
A(6:10,1)=y_cur;
T=0.1;%sampling time
%creating error fucntion with prediction
Jemp=@(xy_dot)(t_ref-(A*xy_dot(1:10,:)))'*Q*(t_ref-(A*xy_dot(:,1)));
x0=[x_dot y_dot];
I understand that the problem is that the vector does not have a defined index so the multiplication with matrix A is not working so my question would be how do i define xy_dot in a way that it allows me to define the function.
Edit: I made an edit in the code that solved an initial mistake in the matrix arrangements. However the problem is presisting. Also x_cur is a scalar that i wan to assign to all values in the first column of A.

 Risposta accettata

We don’t have ‘x_cur’ and ‘y_cur’, so I assume they are each (1 x 5) row vectors.
With that addition, this code snippet:
x_cur = rand(1,5); % Create Vector
y_cur = rand(1,5); % Create Vector
Q=ones(5,5);
A=ones(10,5);
H=ones(5,5);
H=tril(H);
A(1:5,:)=H;
A(1,1:5)=x_cur;
A(1,6:10)=y_cur;
Asize = size(A)
Hsize = size(H)
produces:
Asize =
10 10
Hsize =
5 5
resulting in an impossible matrix or array multiplication.
It’s relatively easy to check these, and even easier if you have the Workspace window open, so that you can see the sizes of your variables.

6 Commenti

Hi thanks for your response I´ve solved the mistake and ammended the code acordingly now when I run the worksapce shows A as a 10x5 matrix Thanks!
However the error command is still there so I have uploaded the ammended code.
My pleasure.
What do you mean by: ‘However the error command is still there ...’
Are you still having problems with your code? If so, please describe them.
By making the corrections you suggested I get matrix A to be 10x5 as intended but when I try to declare the function Jemp like this
Jemp=@(xy_dot)(t_ref-(A*xy_dot(1:5,1)))'*Q*(t_ref-(A*xy_dot(1:5,1)));
From what I understand A*xy_dot should be a 10x1 matrix however I get this error when I try to run:
Inner matrix dimensions must agree. Function 'MATLAB Function' (#40.616.644), line 24, column 15: "(t_ref-(A*xy_dot(1:5,1)))'*Q" Launch diagnostic report.
It might be worth noting that this script is inside a matlab function in a simulink model.I think it is something to do with declaring the fucntion however I am not sure.
This:
(t_ref-(A*xy_dot(1:5,1)))'
may produce a (10x1) vector originally, however the transpose is going to convert it to a (1x10) vector, and neither of those is incompatible with ‘Q’ being a (5x5) matrix.
I suggest you take the expression apart as:
Test1 = (A*xy_dot(1:5,1))
Test2 = (t_ref-(A*xy_dot(1:5,1)))
Test3 = (t_ref-(A*xy_dot(1:5,1)))'*Q
Test4 = (A*xy_dot(1:5,1))
Test5 = (t_ref-(A*xy_dot(1:5,1)))
and evaluate the separate sections of it until the problem reveales itself. Then correct it.
Just solved it by changing Q into a 10x10 thanks a lot for the help!
My pleasure!
If my Answer helped you solve your problem, please Accept it!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Centro assistenza e File Exchange

Prodotti

Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by