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));

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by