MATLAB: Error when integrating using a loop

error when integrating a matrix

CrossSection1 = [80 80 2]; %mm



CrossSection2 = [80 55 1.5]; %mm
Load = [900 300 400]; %[N xmm ymm]
Length = 2; %m


YoungsMod = 70; %GPa
Density = 7800;%7800; %kg/m^3
Spacing = 800; %m
theta = 45;
Distance = 1.8; %m
Magnitude=900; %N
xcoordinate=300; %mm
ycoordinate=400; %mm
Density=7800 ;%kg/m^3
Distance=1.8; %m%
%BEAM1
Height=80;%Height of beam1 in mm
Width=80; %Width of beam1 in mm
Thickness=2; %Thickness of beam1 in mm
CrossSectional1=[ Height Width Thickness];
Load=[ Magnitude xcoordinate ycoordinate];
ForceApplied=Load(1,1);
xDistance=Load(1,2);
yDistance=Load(1,3);
%calculate the applied force acing on the beam
FBeam1=(ForceApplied*(Spacing-xDistance))/Spacing;
Area1=(Height*Width)-[(Width-2*Thickness)*(Height-2*Thickness)]; %cross-sectional area of beam1
I1=(Width*(Height)^3)/12-((Width-2*Thickness)*(Height-2*Thickness)^3)/12;%second moment of area for beam1 in mm^4
DistLoad1=(Density*Area1*9.81)/1000^3; % distributed load due to weight of the beam
%Reaction forces on the beam
Rdiagonal1=((DistLoad1*Length*1000*Length/2)+(FBeam1*(Length-(yDistance/1000))))/(sind(theta)*Distance);
Rpinx1=-(Rdiagonal1.*cosd(theta));
Rpiny1=FBeam1+DistLoad1*Length*1000-(Rdiagonal1*sind(45));
Reaction1=[ Rpinx1; Rpiny1; Rdiagonal1];
X1=[0:1:(Length*1000-yDistance)];% limits of first shear force distribution
V1=-DistLoad1*X1+Rpiny1; %first equation of shear force
X2=[(Length*1000-yDistance+1):1:Distance*1000]; % limits of second shear force distribution
V2=-DistLoad1*X2+(Rpiny1-FBeam1); %second equation of shear force
X3=[(Distance*1000+1):1:(Length*1000)];
V3=-DistLoad1*X3+(Rpiny1-FBeam1+(Rdiagonal1*sind(theta)));% third equation of shear force acting on the beam
X=[X1 X2 X3];%length of the beam cut into unit sections
V=[V1 V2 V3];%shear forces acting on the beam
ShearForce1=[X' V'];
%integrate ShearForce1 to get the bending moment on the beam in matrix form
BendingMoment1=[0 0];
for k=1:Length*1000
BendingMoment1=[BendingMoment1;k(BendingMoment1(k,2)+(0.5*(ShearForce1(k,2)+(ShearForce1(k+1,2)))))]
endfor
when i try running this code i get this error
error: k(104.918): subscripts must be either integers 1 to (2^63)-1 or logicals
error: called from
can someone please help me fix this error ,i have tried many times to find where i went wrong but i cant figure it out
project at line 43 column 16

Best Answer

This code let the output grow iteratively, what is extremely inefficient:
BendingMoment1=[0 0];
for k=1:Length*1000
BendingMoment1=[BendingMoment1;k(BendingMoment1(k,2)+(0.5*(ShearForce1(k,2)+(ShearForce1(k+1,2)))))]
end
This is most likely a typo:
k(BendingMoment1(k,2)+(0.5*(ShearForce1(k,2)+(ShearForce1(k+1,2)))))
Perhaps you mean:
[k, BendingMoment1(k,2) + 0.5*(ShearForce1(k,2)+(ShearForce1(k+1,2)))]
Pre-allocate the output to accelerate it massively:
BendingMoment1 = zeros(Length*1000, 2);
for k = 2:Length*1000
BendingMoment1(k, 1) = k;
BendingMoment1(k, 2) = BendingMoment1(k-1,2) + ...
0.5 * (ShearForce1(k-1,2) + ShearForce1(k,2));
end
Or easier:
Len = Length*1000;
BendingMoment1 = zeros(Len, 2);
BendingMoment1(:, 1) = (0:Len).';
BendingMoment1(2:end, 2) = cumsum(0.5 * (ShearForce1(1:end-1) + ShearForce1(2:end)))
Related Question