How to solve "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side" error?

2 ビュー (過去 30 日間)
I am not understanding this indices error. Please, take a look at the code and help me with the solution. I have highlighted the line in the code in which the error is shown.
dis0=input('dis0=');
disd0=input('disd0=');
phi0=input('phi0=');
phid0=input('phid0=');
theta0=input('theta0=');
thetad0=input('thetad0=');
fmin=input('fmin=');
fmax=input('fmax=');
for f=fmin:fmax
tp=1/f;
dt=1/(25*f);
for i=0:dt:2*tp
t=i;
j=i+2;
f1=sin(2*pi*f*t);
f2=a*sin(2*pi*f*t);
f3=b*sin(2*pi*f*t);
F=[f1; f2; f3];
if i==0
% The error is shown in the below line.
x(j)=[dis0; phi0; theta0];
xd(j)=[disd0; phid0; thetad0];
xdd(j)=M\(F-(K*x(j))-(C*xd(j)));
else
break
end
x(j-1)=x(j)-(dt*xd(j))+(0.5*(dt^2)*xdd(j));
x(j+1)=((M/(dt^2))+(C/(2*dt)))\(F-(K-((2*M)/(dt^2)))*x(j)-((M/(dt^2))-(C/(2*dt)))*x(j-1));
end
end

採用された回答

Cris LaPierre
Cris LaPierre 2020 年 9 月 15 日
x(j) is a single array element. You are trying to assign it 3 values, [dis0; phi0; theta0]. I'm unsure what the intent is, but one way to solve this is to specify row and column indices:
x(:,j)=[dis0; phi0; theta0];
You will need to double check everywhere you use x to take this change into account.
  8 件のコメント
Cris LaPierre
Cris LaPierre 2020 年 9 月 15 日
編集済み: Cris LaPierre 2020 年 9 月 15 日
This code does not create an error:
x(:,j)=[dis0; phi0; theta0];
It is the next line of code that does:
xd(j)=[disd0; phid0; thetad0];
You will need to make a similar change to handle xd and xdd. You will then need to modify everywhere in your code you use x(j) and its variations: x(j-1) and x(j+1), as well as xd(j) and xdd(j).
For xdd, you have a more complicated issue since M\(F-(K*0)-(C*0)) produces a 3x3 array. You might need to do something like
xdd(:,:,j)=M\(F-(K*0)-(C*0));
Neeraj Kumar
Neeraj Kumar 2020 年 9 月 15 日
Thank you Cris for the solution and Thank you Steven for the analogy.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by