Error Message: 'Matrix dimensions must agree'

조회 수: 20 (최근 30일)
Tristen Hernandez
Tristen Hernandez 2019년 11월 2일
편집: Tristen Hernandez 2019년 11월 2일
m%%Declare Variables:
n = [.1, 1.0, 1.5];
t= [0:.1:10];
k = 10 %N/m;
m = 5 %kg;
A = 5; %m (initial postion);
omega0 = sqrt(k/m);
omegad = omega0.*sqrt(1.-n.^2);
s=zeros(1,101);
v=zeros(1,101);
a=zeros(1,101);
s = exp(-n.*omega0.*t).*(A*cos(omegad.*t));
v = -n.*omega0.*exp(-n.*omega0.*t).*(A.*cos(omegad.*t))-exp(-n.*omega0.*t).*(A.*sin(omegad.*t).*omegad);
a = (-n.*omega0).^(2).*exp(-n.*omega0.*t).*(A.*cos(omegad.*t))+exp(-n.*omega0.*t).*(A.*sin(omegad.*t).*omegad)...
+exp(-n.*om
ega0.*t).*(A.*sin(omegad.*t))-exp(-n.*omega0.*t).*(A.*cos(omegad.*t).*omegad);
On line 26 (the s variable) I keep getting the error that the matrix dimensions do not agree and quite frankly I have no idea why, very new to the program and still trying to figure everything out. I appreciate any and all help.
I guess while I'm here I also have a question in that I want A to be a function of s, with it's initial condition being five, so how would I be able to rewrite A in terms of s after the initial run through of it being 5?
The release version is R2019a if that helps with answering.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 2일
편집: KALYAN ACHARJYA 2019년 11월 2일
In the following expression
s = exp(-n.*omega0.*t).*(A*cos(omegad.*t));
n have length 3 and t have 101, how can you do element wise vector multiplecation.
As you defined
n =[.1, 1.0, 1.5];
t= [0:.1:10];

추가 답변 (1개)

Tristen Hernandez
Tristen Hernandez 2019년 11월 2일
편집: Tristen Hernandez 2019년 11월 2일
I was able to make the multiplication work by transforming the
t = [0:.1:10]
into
t = [0:.1:10]'
Now I'm trying to put these into a for loop that will use the time in increments of 20, so that I can print the results to a table, it's resulting in an error on line 35
%%Declare Variables:
n = [.1, 1.0, 1.5];
t= [0:.1:10]';
k = 10 %N/m;
m = 5 %kg;
A = 5; %m (initial postion);
omega0 = sqrt(k/m);
omegad = omega0.*sqrt(1.-n.^2);
s=zeros(1,101);
v=zeros(1,101);
a=zeros(1,101);
s = exp(-n.*omega0.*t).*(A*cos(omegad.*t));
v = -n.*omega0.*exp(-n.*omega0.*t).*(A.*cos(omegad.*t))-exp(-n.*omega0.*t).*(A.*sin(omegad.*t).*omegad);
a = (-n.*omega0).^(2).*exp(-n.*omega0.*t).*(A.*cos(omegad.*t))+exp(-n.*omega0.*t).*(A.*sin(omegad.*t).*omegad)...
+exp(-n.*omega0.*t).*(A.*sin(omegad.*t))-exp(-n.*omega0.*t).*(A.*cos(omegad.*t).*omegad);
fprintf("t(s) \t s(m) \t v(m/s) \t a (m/s^2) \t \n");
fprintf("-------------------------------------------------- \n");
for i= [1:20:101]
s(i) = exp(-n.*omega0.*i).*(A*cos(omegad.*i))
v(i) = -n.*omega0.*exp(-n.*omega0.*i).*(A.*cos(omegad.*i))-exp(-n.*omega0.*i).*(A.*sin(omegad.*i).*omegad);
a(i) = (-n.*omega0).^(2).*exp(-n.*omega0.*i).*(A.*cos(omegad.*i))+exp(-n.*omega0.*i).*(A.*sin(omegad.*i).*omegad)...
+exp(-n.*omega0.*i).*(A.*sin(omegad.*i))-exp(-n.*omega0.*i).*(A.*cos(omegad.*i).*omegad);
fprintf("%.4f \n", s(i));
fprintf("%.4f \n", v(i));
fprintf("%.4f \n", a(i));
end
The error reading "Unable to perform assignment because the left and right sides have a different number of elements." The error occurs in line 35.
Quite frankly I have very little idea in what I'm doing in regards to this program so I appreciate all the help. Should I just post this in a new question post?

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by