# Error message when running code: "Dimension argument must be a positive integer scalar"

조회 수: 52 (최근 30일)
Abdalrazzaq Aljammal 2022년 11월 14일
답변: Image Analyst 2022년 11월 15일
Error using size
Dimension argument must be a positive integer scalar or a vector of positive integers.
Error in practice (line 36)
x=zeros(size(n,q));
The code:
clear;
m1=3000;k=13650;
mass=[m1,0,0;0,4*m1,0;0,0,m1];
M=input('mass');
[n,o]=size(M);
if n~=o
error('mass');
end
stiff=[k+k,-k,0;-k,k+k,-k;0,-k,k+k];
K=input('stiff');
[n,o]=size(K);
if n~=o
error('stiff');
end
qu=0;
[u,l]=eig(K,M);
% Using eigin this way allows us to subtract M*w^2
% from K, instead of I*w^2 (where I is the n by n identity
% matrix).
% The output from eiggives unit-length eigenvectors.
% We need to scale them with respect to M.
%
for s=1:n
alfa=sqrt(u(:,s)'*M*u(:,s));
u(:,s)=u(:,s)/alfa;
end
inid=[0;0;0];
iniv=[0;0;0];
x0=input('inid');
xd0=input('iniv');
final=[];
tf=input('final');
t=0:0.1:tf;
q=tf/0.1;
x=zeros(size(n,q));
% Applying Equation 7.183.
%
for j=1:n
w(j)=sqrt(l(j,j));
xt=u(:,j)*(u(:,j)'*M*x0*cos(w(j).*t)+u(:,j)'*M*xd0/...
w(j)*sin(w(j).*t));
x=x+xt;
end
% Plotting the modes in a subplot format.
% Note that, for more than 3 or 4 degrees
% of freedom, the plots will become nearly
%
for r=1:n
subplot(n,1,r)
plot(t,x(r,:))
xlabel('Time, seconds');
ylabel(['Response x',num2str(r)]);
end

댓글을 달려면 로그인하십시오.

### 답변 (2개)

Chandler Hall 2022년 11월 14일
편집: Chandler Hall 2022년 11월 14일
x=zeros(size(n,q))
Perhaps you intend:
x = zeros(size(n, 1), numel(t));
##### 댓글 수: 2없음 표시없음 숨기기
Walter Roberson 2022년 11월 14일
As a special case you can just use x=0; in this context.
Abdalrazzaq Aljammal 2022년 11월 15일
thak you sir its worked.

댓글을 달려면 로그인하십시오.

Image Analyst 2022년 11월 15일
What values did you enter? I entered 4, 5, 4, 4, and 4 and got no such error with the code above.
Are you sure you actually entered a number and didn't just hit Enter (to give a null value)?
Also, n and q need to be integers. So you might correct
q=tf/0.1;
x=zeros(size(n,q));
to this
q = 10 * tf;
x = zeros(size(n, q));
Why? See the FAQ:

댓글을 달려면 로그인하십시오.

### 카테고리

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

### Community Treasure Hunt

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

Start Hunting!

Translated by