# Array indices must be positive integers or logical values.

2 views (last 30 days)
JM on 1 Dec 2019
Answered: Image Analyst on 1 Dec 2019
Hello, I have this code and for some reason it is giving me the error "Array indices must be positive integers or logical values." Then it mentions line 7 of the function as the point of error. Please help!
Here is the script:
clear all; clf
xspan = [0 100]; % solve over this interval (length of the beam)
y0 = 0; % beam displacement = 0 at x=0;
v0 = .001; % choose slope of beam at x = 0;
[x,z] = ode45('beamfunc', xspan, [y0 v0]);
y = z(:,1); % the deflection of the beam
and here is the function 'beam func'
function dz = beamfunc(x,z)
% This function describes the nonlinear deflection of a bean
L = 100; w = 100; E = 10^7; T = 500; I = 500; % These are constants
v = z(2); % v is the second element contained in z
y = z(1); % y is the first element contained in z
yprime=v ; % this is f
vprime=(((1+v^2)^(3/2))*(T*y/E*I + w*x(x-L)/2*E*I)); % this is g
dz = [yprime ; vprime];
end

#### 1 Comment

Adam Danz on 1 Dec 2019
Maybe you could take a shot at troubleshooting it.
From the editor, click the downward black arrow under the green "run "arrow. Then select "pause on errors" (this may be in a different location in older releases). Run your code and the when there is an error, it will break and stop at the line that is causing the error. All of you variable values will be available and you can look at what variables are causing the error. You already know that the variables should positive integers or logicals which obviously isn't the case. Look into why they aren't correct. I see your previous quesiton was on the same topic so this exercise will help you independently solve this issue.

Walter Roberson on 1 Dec 2019
x(x-L)
is an attempt to index the scalar x at offset (x-L) . I would suggest to you that your forgot a multiplication symbol.