MATLAB: Loop through data until a specific point

area under curvedatadata setfor loopinfinite loopintegralwhile loop

I've run an experiment where a machine exerts a force on a bridge until it breaks. I need to cycle through my entire data set and calculate the toughness until I've hit the breaking point. The toughness is calculated by summing up all the rectangles under my load vs. distance curve. (Basically the integral) However, I have not been able to find a reasonable way of doing so and my current loop is an infinite loop and I'm not sure why.
%Initializing variables
bridge1Data = xlsread('Bridge1Data.xlsx', 'A2:C2971');
bridge2Data = xlsread('Bridge2Data.xlsx', 'A2:C1440');
bridge1Load = bridge1Data(:, 2);
bridge2Load = bridge2Data(:, 2);
bridge1Dist = bridge1Data(:, 3);
bridge2Dist = bridge2Data(:, 3);
[row1, col1] = size(bridge1Dist);
[row2, col2] = size(bridge2Dist);
bridge1Disp = zeros(row1, col1);
bridge2Disp = zeros(row2, col2);
fdValue1 = 0.000407350000000029;
&Main code
%Displacement
for k = 2:length(bridge1Dist)
bridge1Disp(k-1, 1) = bridge1Dist(k, 1) - bridge1Dist(k-1, 1);
end
%Max Load Bridge 1
maxLoad1 = 0;
for n = 1:length(bridge1Load)
for k = 1
if bridge1Load(n, k) > maxLoad1
maxLoad1 = bridge1Load(n, k);
end
end
end
%Cycle through data till failure, change proj data
totalRect1 = 0;
for j = 2:length(bridge1Disp)
while bridge1Disp(j, 1) ~= fdValue1
rectangle = (bridge1Disp(j, 1) - bridge1Disp(j-1, 1))*...
((bridge1Load(j, 1) + bridge1Load(j-1, 1))/2);
totalRect1 = totalRect1 + rectangle;
end
end
Basically, I make an array for the load and distance the machine pushes down on the bridge, assign a 'Failure Distance' value (fdValue). I then calculate displacement, calculate the maximum load, through the variable 'rectangle' and sum them all up in 'totalRect1', and use that to calculate the toughness by finding the area under the curve. Is anyone able to see why the loop is an infinite loop? Thanks

Best Answer

The problem is likely with this line:
while bridge1Disp(j, 1) ~= fdValue1
because I don’t see that you ever change ‘bridge1Disp(j,1)’ within that while loop.
EDIT —
‘However, this time I get the error: Cell contents reference from a non-cell array object. Whats the reason?’
There is no reason to define ‘bridge1Disp(j,1)’ as a cell. Leave it as the double matrix it is.
Your best option is to restate the while statement as:
bridge1Disp(j, 1) <= fdValue1
so as long as the bridge displacement is less than fdValue1 (which is what I believe you want), the loop will continue to execute. However you must change bridge1Disp(j, 1) in the loop, or the condition will never change and the loop will run forever.