Matrix dimensions must agree error

I have the following code and couldn't figure out what I am doing wrong. I am getting the following error,
X0 = [0.001;0.001;0.001;0.001;0.001;0.001];
maxIter =100;
tolX = .01;
% Computation
X = X0;
Xold = X0;
R = inv(j),
for i =1:maxIter
[f,j]=new(X);
X = (X-R*f);
err(:,i) = (X-Xold)
Xold = X;
X =[0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2;];
if (err(:,i)<tolX)
break;
end
end

5 件のコメント

Sindar
Sindar 2020 年 1 月 21 日
please copy the full error (all red text) into your answer
Avismit Dutta
Avismit Dutta 2020 年 1 月 21 日
function [fval,jac]=new(X)
a = X(1);
b = X(2);
c = X(3);
d = X(4);
e = X(5);
f = X(6);
fval(1,1)=cosd(5*a)+cosd(5*b)+cosd(5*c)+cosd(5*d)+cosd(5*e)+cosd(5*f);
fval(2,1)=cosd(7*a)+cosd(7*b)+cosd(7*c)+cosd(7*d)+cosd(7*e)+cosd(7*f);
fval(3,1)=cosd(11*a)+cosd(11*b)+cosd(11*c)+cosd(11*d)+cosd(11*e)+cosd(11*f);
fval(4,1)=cosd(13*a)+cosd(13*b)+cosd(13*c)+cosd(13*d)+cosd(13*e)+cosd(13*f);
fval(5,1)=cosd(17*a)+cosd(17*b)+cosd(17*c)+cosd(17*d)+cosd(17*e)+cosd(17*f);
fval(6,1)=cosd(19*a)+cosd(19*b)+cosd(19*c)+cosd(19*d)+cosd(19*e)+cosd(19*f);
jac= [-5*sind(5*X(1)),-5*sind(5*X(2)),-5*sind(5*X(3)),-5*sind(5*X(4)),-5*sind(5*X(5)),-5*sind(5*X(6));
-7*sind(7*X(1)),-7*sind(7*X(2)),-7*sind(7*X(3)),-7*sind(7*X(4)),-7*sind(7*X(5)),-7*sind(7*X(6));
-11*sind(11*X(1)),-11*sind(11*X(2)),-11*sind(11*X(3)),-11*sind(11*X(4)),-11*sind(11*X(5)),-11*sind(11*X(6));
-13*sind(13*X(1)),-13*sind(13*X(2)),-13*sind(13*X(3)),-13*sind(13*X(4)),-13*sind(13*X(5)),-13*sind(13*X(6));
-17*sind(17*X(1)),-17*sind(17*X(2)),-17*sind(17*X(3)),-17*sind(17*X(4)),-17*sind(17*X(5)),-17*sind(17*X(6));
-19*sind(19*X(1)),-19*sind(19*X(2)),-19*sind(19*X(3)),-19*sind(19*X(4)),-19*sind(19*X(5)),-19*sind(19*X(6))];
Avismit Dutta
Avismit Dutta 2020 年 1 月 21 日
X0 = [0.001;0.001;0.001;0.001;0.001;0.001];
maxIter =100;
tolX = .01;
% Computation
X = X0;
Xold = X0;
R = inv(j),
for i =1:maxIter
[f,j]=new(X);
X = (X-R*f);
err(:,i) = (X-Xold)
Xold = X;
X =[0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2; 0 pi/2;];
if (err(:,i)<tolX)
break;
end
end
Avismit Dutta
Avismit Dutta 2020 年 1 月 21 日
this error is shown..
Error using -
Matrix dimensions must agree.
Error in ad (line 10)
X = (X-R*f);
Sindar
Sindar 2020 年 1 月 21 日
So, if I'm reading this correctly, X should be 6x1, f should be 6x1, and R should be 6x6? So R*f should be 6x1. Check these sizes

回答 (1 件)

Guillaume
Guillaume 2020 年 1 月 21 日

0 投票

The error message is very clear, R*f is not the same size as X (or a size compatible with X), hence you can't do the subtraction. Since we don't know what R or f is we can't tell what their size is.
Note that numbered or sequentially named variables are a bad idea. It forces you to complicate the code. For example, this whole lot:
a = X(1);
b = X(2);
c = X(3);
d = X(4);
e = X(5);
f = X(6);
fval(1,1)=cosd(5*a)+cosd(5*b)+cosd(5*c)+cosd(5*d)+cosd(5*e)+cosd(5*f);
fval(2,1)=cosd(7*a)+cosd(7*b)+cosd(7*c)+cosd(7*d)+cosd(7*e)+cosd(7*f);
fval(3,1)=cosd(11*a)+cosd(11*b)+cosd(11*c)+cosd(11*d)+cosd(11*e)+cosd(11*f);
fval(4,1)=cosd(13*a)+cosd(13*b)+cosd(13*c)+cosd(13*d)+cosd(13*e)+cosd(13*f);
fval(5,1)=cosd(17*a)+cosd(17*b)+cosd(17*c)+cosd(17*d)+cosd(17*e)+cosd(17*f);
fval(6,1)=cosd(19*a)+cosd(19*b)+cosd(19*c)+cosd(19*d)+cosd(19*e)+cosd(19*f);
can be replaced with just:
%X is a column vector
fval = sum(cosd([5; 7; 11; 13; 17; 19] .* X.'), 2);

4 件のコメント

Avismit Dutta
Avismit Dutta 2020 年 1 月 21 日
this kind of error is shown
Error using .*
Matrix dimensions must agree.
Error in awa (line 1)
fval = sum(cosd([5; 7; 11; 13; 17; 19] .* X.'),
2);
Walter Roberson
Walter Roberson 2020 年 1 月 21 日
Which MATLAB release are you using?
Avismit Dutta
Avismit Dutta 2020 年 1 月 21 日
matlab 2016
Walter Roberson
Walter Roberson 2020 年 1 月 21 日
fval = sum(bsxfun(@times, cosd([5; 7; 11; 13; 17; 19], X.')),2);

この質問は閉じられています。

質問済み:

2020 年 1 月 21 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by