error when integrating using a loop

1 次查看(过去 30 天)
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
  2 个评论
Rik
Rik 2019-3-27
I can't find the offending line in the code you posted, but the error itself seems self-explanatory. Where are you trying to index k?
Does this error also occur in Matlab? You should make sure your problem also occurs in Matlab and not just Octave. This forum (at the very least the website itself) is maintained by Mathworks, and Octave is ostensibly a competing product, so you can expect only limited help here for the cases where the code runs fine under Matlab.
Torsten
Torsten 2019-3-27
Maybe you mean
k*(BendingMoment1(k,2)+(0.5*(ShearForce1(k,2)+(ShearForce1(k+1,2)))))
instead of
k(BendingMoment1(k,2)+(0.5*(ShearForce1(k,2)+(ShearForce1(k+1,2)))))
?

请先登录,再进行评论。

采纳的回答

Jan
Jan 2019-3-27
编辑:Jan 2019-3-27
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)))

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by