while loop not working
2 次查看(过去 30 天)
显示 更早的评论
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 1785x1 matrices.
Thanks for your time and aid!
Neil
1 个评论
采纳的回答
Geoff Hayes
2014-6-5
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!
4 个评论
Geoff Hayes
2014-6-6
The & (or better, the double ampersand &&) is the right operator to use to guarantee that the slope of your 1-degree polynomial is nearly positive and that the average strain is less than 0.35.
The call to polyfit passes in n=1 for the degree of the polynomial to fit the data to, so the result is A(1)*x + A(2). In your condition, A2(1,1)>-0.0001 (or just A2(1)>-0.0001)) you are allowing for that first coefficient to be anything greater than -0.0001 - so the slope of your function can be slightly negative, zero or positive. Why are you enforcing the nearly-positive slope? Is that because if it were negative then the next cycle has begun? Is that how you tell when to separate between cycles?
If you were to plot all of the data plot(AverageStrain,AverageStress), do you observe all five cycles?
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!