MATLAB: While loop not working

plotpolyfit

Hello everyone!
So, I've been working on a code that will allow me to separate five cycles of stress and strain into there segement going up and then down making 10 plots. (Mechanics of materials). The code here is for the first two cycles, so four plots are attempted. My code is as follows;
%%Cycle 1 Separation
g=1;
Cycle1UpStop=1;
A=[5 5];
while (A(1,1)>-0.0001) & (AverageStrain(g,1)<0.35)
h = AverageStrain(1+g:20+g,:);
i = AverageStress(1+g:20+g,:);
A = polyfit(h,i,1);
Cycle1UpStop = g+10;
g = g+20;
end
Cycle1UpStrain=AverageStrain(1:Cycle1UpStop,1);
Cycle1UpStress=AverageStress(1:Cycle1UpStop,1);
subplot(3,3,1)
plot (Cycle1UpStrain, Cycle1UpStress);
k=100;
Cycle1DownStop=Cycle1UpStop;
while k>0.17 & k<150
Cycle1DownStop=Cycle1DownStop+1;
k=AverageStress(Cycle1DownStop,:);
end
Cycle1DownStrain=AverageStrain(Cycle1UpStop+1:Cycle1DownStop,:);
Cycle1DownStress=AverageStress(Cycle1UpStop+1:Cycle1DownStop,:);
subplot(3,3,2)
plot (Cycle1DownStrain, Cycle1DownStress);
%%Cycle 2 Separation
g2=Cycle1DownStop;
Cycle2UpStop=Cycle1DownStop;
A2=[5 5];
while (A2(1,1)>-0.0001) & (AverageStrain(g2,1)<0.35)
h2 = AverageStrain(1+g2:20+g2,:);
i2 = AverageStress(1+g2:20+g2,:);
A2 = polyfit(h2,i2,1);
Cycle2UpStop = g2+10;
g2 = g2+20;
end
Cycle2UpStrain=AverageStrain(Cycle1DownStop:Cycle2UpStop,1);
Cycle2UpStress=AverageStress(Cycle1DownStop:Cycle2UpStop,1);
subplot(3,3,3)
plot (Cycle2UpStrain, Cycle2UpStress);
k2=100;
Cycle2DownStop=Cycle2UpStop;
while k2>.17 & k2<150
Cycle2DownStop=Cycle2DownStop+1;
k2=AverageStress(Cycle2DownStop,:);
end
Cycle2DownStrain=AverageStrain(Cycle2UpStop+1:Cycle2DownStop,:);
Cycle2DownStress=AverageStress(Cycle2UpStop+1:Cycle2DownStop,:);
subplot(3,3,4)
plot (Cycle2DownStrain, Cycle2DownStress);
The problem I'm having that on the second cycle my plot for Cycle2UpStrain vs, Cycle2UpStress ends to early. See plots picture;
Note: AverageStress and Average Strain are used as X and Y values for a plot of all 5 cycles. They are both 1785×1 matrices.
Thanks for your time and aid!
Neil

Best Answer

The Cycle 2 Separation looks like
%%Cycle 2 Separation
g2=Cycle1DownStop;
Cycle2UpStop=Cycle1DownStop;
A2=[5 5];
while (A2(1,1)>-0.0001) & (AverageStrain(g2,1)<0.35)
h2 = AverageStrain(1+g2:20+g2,:);
i2 = AverageStress(1+g2:20+g2,:);
A2 = polyfit(h2,i2,1);
Cycle2UpStop = g2+10;
g2 = g2+20;
end
Cycle2UpStrain=AverageStrain(Cycle1DownStop:Cycle2UpStop,1);
Cycle2UpStress=AverageStress(Cycle1DownStop:Cycle2UpStop,1);
The code then plots the Cycle2UpStrain vs Cycle2UpStress. Note that it looks like h2 and i2 data are populated with 20 elements each, the polyfit is executed and the end point, Cycle2UpStop is set, and g2 is incremented for the next iteration. Note the differences between these last two statements
Cycle2UpStop = g2+10;
g2 = g2+20;
The 20 makes sense for g2 since we seem to want to extract 20 distinct elements from the AverageStrain and AverageStress on each iteration. So why is the same not done for Cycle2UpStop? As we have already extracted data from 1+g2:20+g2 then it seems that we should want
Cycle2UpStop = g2+20;
Try that and see what happens!