Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

1 visualización (últimos 30 días)
I am trying to graph some responses for my system. However, I keep getting teh error code "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." and I cannot seem to figure out where I am going wrong
%% Clear Memory
clc;
clear all;
close all
global w F1 epsilon epsilon_r S
%% Excitation Amplitude
g = 9.81;
F1 = 0.05*g;
F2 = 0.1*g;
F3 = 0.5*g;
F4 = 1.0*g;
%%
wi =160*2*pi; % Initial frequency sweep 160
wf =170*2*pi; % Final frequency sweep 170
ns=3; % # of points in the FRC
step = (wf-wi)/ns;
tend = 3;
%% F1
i=1; % Initilize the loop counter
for h=1:ns
%w= wf-step*(h-1); % Full range backward sweep
w= wi+step*(h-1); % Full range forward sweep
Tint=[0 tend];
options=odeset('AbsTol',1e-10,'RelTol',1e-10);
[t,z]=ode15s(@ProjectFun_2_V2, Tint, [0;0;0;0;0]);
tindex=(t>2);
XMAX1(h)=max(z(tindex,1)); %Amplitude
CMAXall(h)=epsilon_r*epsilon*S/XMAX1(h); %Capcitance
QMAXall(h)=max(z(tindex,3)); %electric charge
VMAXall(h)=QMAXall(i)/CMAXall(h); %Voltage
Freq(h)= w;
end
%% Plots
figure (1)
plot(Omega/(2*pi),XMAX1,'*-b','LineWidth',1)
title('Frequency-Response')
grid on
xlabel('\Omega (rad/s)')
ylabel('Deflection (m)')
legend('F1 = 0.05g')
h=gca
get(h,'FontSize')
set(h,'fontweight', 'bold', 'FontSize',20)
% figure (2)
% plot(Omega/(2*pi),VMAX1,'*-b','LineWidth',1)
% title('Frequency-Voltage')
% grid on
% hold on
% % plot(Omega/(2*pi),VMAX2,'*-r','LineWidth',1)
% % hold on
% % plot(Omega/(2*pi),VMAX3,'*-g','LineWidth',1)
% xlabel('\Omega (rad/s)')
% ylabel('Voltage (V)')
% legend('F1 = 0.2g', 'F2 = 0.5g', 'F3 = 1.0g')
% h=gca
% get(h,'FontSize')
% set(h,'fontweight', 'bold', 'FontSize',20)
function dz = ProjectFun_2(t,z,d1,d2)
global w F1 epsilon epsilon_r S
m1 = 0.056; %kg
m2 = 0.0084; %kg
k1 = 1500; %N/m
k2 = 144; %N/m
c1 = 0.1833; %Ns/m
c2 = 0.0132; %Ns/m
w1 = sqrt(k1/m1);
w2 = sqrt(k2/m2);
zeta1 = c1/(2*sqrt(k1*m1));
zeta2 = c2/(2*sqrt(k2*m2));
ki = 2000; %N/m
ci = 80; %Ns/m
d1 = -5e-3; %m
d2 = 5e-3; %m
T1 = 10e-6;
T2 = 10e-6;
S = 1;
epsilon = 8.854e-12;
epsilon_r = 1;
sigma = 5;
A = 1;
R = 10e6;
dz(1,1) = z(2);
if z(1)-z(3) <= d1
dz(2,1) = -2*zeta1*w1*z(2)-w1^2*z(1)-2*zeta1*w1*(z(2)-z(4))-w1^2*(z(1)-z(3))-2*zeta1*(z(2)-z(4))-w1^2*(z(1)-z(3))-(ki*(z(1)-z(3)+d1)+ci*(z(2)-z(4)))/m1-F1*cos(w*t);
elseif d1 < z(1)-z(3) < d2
dz(2,1) = -2*zeta1*w1*z(2)-w1^2*z(1)-2*zeta1*w1*(z(2)-z(4))-w1^2*(z(1)-z(3))-2*zeta1*(z(2)-z(4))-w1^2*(z(1)-z(3))-z(5)^2/(2*epsilon*A*(d1+d2)^2)*((d1+(z(1)-z(3))))^2/m1-F1*cos(w*t);
else z(1)-z(3)>= d2;
dz(2,1) = -2*zeta1*w1*z(2)-w1^2*z(1)-2*zeta1*w1*(z(2)-z(4))-w1^2*(z(1)-z(3))-2*zeta1*(z(2)-z(4))-w1^2*(z(1)-z(3))-(ki*(z(1)-z(3)+d2)+ci*(z(2)-z(4)))/m1-F1*cos(w*t);
end
dz(3,1) = z(4);
if z(1)-z(3) <= d1
dz(4,1) = -2*zeta2*w2*(z(4)-z(2))-w2^2*(z(3)-z(1))-2*zeta2*(z(4)-z(2))-w2^2*(z(3)-z(1))-(ki*(z(1)-z(3)+d1)+ci*(z(2)-z(4)))/m2-F1*cos(w*t);
elseif d1 < z(1)-z(3) < d2
dz(4,1) = -2*zeta1*w1*z(2)-w1^2*z(1)-2*zeta1*w1*(z(2)-z(4))-w1^2*(z(1)-z(3))-2*zeta1*(z(2)-z(4))-w1^2*(z(1)-z(3))--z(5)^2/(2*epsilon*A*(d1+d2)^2)*((d2-(z(1)-z(3))))^2/m1-F1*cos(w*t);
else z(1)-z(3)>= d2
dz(4,1) = -2*zeta2*w2*(z(4)-z(2))-w2^2*(z(3)-z(1))-2*zeta2*(z(4)-z(2))-w2^2*(z(3)-z(1))-(ki*(z(1)-z(3)+d2)+ci*(z(2)-z(4)))/m2-F1*cos(w*t);
end
if z(1)-z(3)<=d1
dz(5,1) = -z(5)/(S*epsilon*R)*(T1/epsilon_r+d1+(z(1)-z(3)))+(sigma*A*(d1+(z(1)-z(3)))/(epsilon*S*R));
elseif d1 < z(1)-z(3) < d2
dz(5,1) = 0;
else z(1)-z(3)>= d2;
dz(5,1) = -z(5)/(S*epsilon*R)*(T2/epsilon_r+d2+(z(1)-z(3)))+(sigma*A*(d2+(z(1)-z(3)))/(epsilon*S*R));
end
end
  1 comentario
DGM
DGM el 26 de Abr. de 2021
Editada: DGM el 26 de Abr. de 2021
I don't know what the problem is, but this code by itself doesn't work. It calls ProjectFun_2_V2, but only includes ProjectFun_2. There are also lines like this:
else z(1)-z(3)>= d2
% ...
end
which don't do anything, though probably without causing other problems. Doing that is the same as doing this:
else
z(1)-z(3)>= d2
% ...
end
that logical comparison is dumped to console and not used for anything. Else statements don't have a condition. If you want to have that expression there for readability purposes, just comment it out so it's not wasting cpu time and spamming junk to console (which slows things down significantly).
The fact that it dumps a pile of warnings before it exits:
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
Warning: Failure at t=8.656960e-02. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (2.220446e-16) at time t.
suggests to me that the plotting isn't quite the priority yet.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Abr. de 2021
elseif d1 < z(1)-z(3) < d2
as far as MATLAB is concerned, that means
elseif ((d1 < z(1)-z(3)) < d2)
The first comparison returns true (1) or false (0), and then that 0 or 1 is compared to d2.
MATLAB does not (generally) have range operators like that; you have to use
elseif d1 < z(1)-z(3) && z(1)-z(3) < d2
You have the same kind of error a few lines down.
Also
else z(1)-z(3)>= d2;
Notice that is not an "elseif" statement, so that is the same as
else
z(1)-z(3)>= d2;
which calculates z(1)-z(3) and compares the result to d2, and then throws away the result of the comparison.
You have the same kind of error further down.
I experimented with making what looked to be the obvious changes, but ode45 was not able to integrate even one time step.
dz(2,1) = -2*zeta1*w1*z(2)-w1^2*z(1)-2*zeta1*w1*(z(2)-z(4))-w1^2*(z(1)-z(3))-2*zeta1*(z(2)-z(4))-w1^2*(z(1)-z(3))-z(5)^2/(2*epsilon*A*(d1+d2)^2)*((d1+(z(1)-z(3))))^2/m1-F1*cos(w*t);
Look towards the end of that,
/(2*epsilon*A*(d1+d2)^2)
d1 is the negative of d2, so d1+d2 is 0, and square of 0 is 0, so the result of the multiplications is 0, and so you are dividing by 0. The numerator happens to be 0 as well, so you get NaN, and once you have that, you cannot integrate.
  1 comentario
Alexandra Craig
Alexandra Craig el 26 de Abr. de 2021
Thank you for pointing out the mistakes. I fixed the elseif statements to "elseif d1 < z(1)-z(3) && z(1)-z(3) < d2" and changed the d1+d2 to d2+d2 to not get zero. These changes did allow me to graph, I just didnt get the graph I was expecting so I will have to play around with it a little bit more.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by