Error: Array indices must be positive integers or logical values.

2 views (last 30 days)
I been using ode45 to solve an coupled equation. I keep getting the error Array indices must be positive integers or logical values for this part of my code " -2*zeta2*(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);" I have tried changing the values, changing the variables and I still get the error.
%% Clear Memory
clc;
clear all;
close all
global w F1 F2 F3 F4 d1 d2
%% Excitation Amplitude
g = 9.81;
F1 = 0.05*g;
F2 = 0.1*g;
F3 = 0.5*g;
F4 = 1.0*g;
%%
Omega =[250:3:400];
%% Distance to determine which case
x1 = 2;
x2 = 12;
x = x1-x2;
%% F1
% if x<-d1
i=1; % Initilize the loop counter
for w=Omega
%Tint is the time interval for the time integration
%(start and end).
Tstep=60; % Time step, the maximum Tstep the lower time interval
Per =2*pi/w; % Period of Excitations
Tint=[0:Per/Tstep:500*Per]; % Tint is the time interval
x0v0=[0;0;0;0;0]; % Define IC's
[t,x]=ode45(@LIFun,Tint,x0v0); % Use the ode45 solver to call the Function Q1Fun get time respons
[rT,cT]=size(t); % Use the ode45 solver to get time respons
X=[x(rT-Tstep+1:rT,:)]; % Find the steady state amplitudes for the last 60 periods
XMAX1(i)=max(X(:,2))*1e3; % Find the Max amplitude from the steady state amplitude of the last 60 periods
%VMAX1(i)=max(X(:,4)); % Find the Max amplitude from the steady state amplitude of the last 60 periods
i=i+1; % Update the loop counter
end
%% Left Impact Function F1
function dzdt = LIFun(t,z)
global w F1 d1 d2
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;
dzdt=[z(2);
-2*zeta1*w1*z(2)-w1^2*z(1)+2*zeta1*w1*(z(4)-z(2))+w1^2*(z(2)-z(1))+2*zeta1*(z(4)-z(2))+w1^2*(z(3)-z(1))+(ki*(z(3)-z(1)-d1)+ci*(z(2)-z(4)))/m1-F1*cos(w*t);
z(4);
-2*zeta2*(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);
-z(5)/(S*epsilon*R)*(T1/epsilon_r+d1+(z(1)-z(3)))+(sigma*A*(d1+(z(1)-z(3)))/(epsilon*S*R))];

Accepted Answer

Steven Lord
Steven Lord on 21 Apr 2021
-2*zeta2(z(4)-z(2))
Is z(4)-z(2) guaranteed to be a positive integer value suitable for use as an index into the variable zeta2? Or did you mean to multiply zeta2 by the quantity (z(4)-z(2)) and accidentally omitted the multiplication sign?
  1 Comment
Alexandra Craig
Alexandra Craig on 21 Apr 2021
I accidentally omitted the multiplication sign. Thank you for pointing that out, it now runs

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!