Break statement inside an if statement

for count = 1:Iteration_lim + 1
%Error check for function not converging
if count == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
break
end
This is a section of my code, when I put in the break statement I get an error saying "This statement (and possibly following ones) can not be reached". The code runs and returns a final answer but the break statement does not seem to be doing anything. This is problematic as I want to return the count and the equivalent answer at that iteration, not just the final answer, at the end of the for loop. My professor has uploaded identical code which performs this task without problem. Any help is appreciated.

Risposte (2)

Darragh - the error throws an error and displays a message, so the break will never be called...and so the error message makes sense (I get the same error when using it in my code). To get around this, change the error to an fprintf like
fprintf('Iteration limit reached, function failed to converge.\n');

11 Commenti

Thanks for the quick reply, maybe the break statement is not my problem. My function has one output, a matrix called angleset, made up of angleset1, angleset2, angleset3, and angleset4. Each angleset is one for loop.
for count = 1:Iteration_lim + 1
%Error check for function not converging
if count == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
end
%Closure equations 1 and 2
C1 = L1*cos(angleset1(1)) - 2.4*cos(angleset1(2) + (pi/2)) - 4.5*cos(angleset1(2)) - 2.4;
C2 = L1*sin(angleset1(1)) - 2.4*sin(angleset1(2) + (pi/2)) - 4.5*sin(angleset1(2)) + 2.1;
f1 = [C1;C2];
%Error check for tolerance limit being exceeded
if abs(f1(1)) < Tolerance && abs(f1(2)) < Tolerance
break
end
%Partial derivatives of C1 and C2
derivC1 = [-L1*sin(angleset1(1)), (4.5*sin(angleset1(2))+2.4*cos(angleset1(2)))];
derivC2 = [L1*cos(angleset1(1)), (-4.5*cos(angleset1(2))+2.4*sin(angleset1(2)))];
derivf1 = [derivC1;derivC2];
%Newton Raphson step
angleset1 = angleset1 - (inv(derivf1))*f1;
end
What I would like to do is return the value of the count for each loop, i.e. total number of iterations, and also the value of the corresponding angleset at each iteration. I tried adding
angleset1;
count1;
just before the end of the four loop but it did not work. Does the count need to be an output of my function? Thanks.
Darragh - use an array to manage the angleset data. The length of the array will then be used to determine the number of iterations:
angleset = [];
angleset(1) = angleset1; % <------- presumably this is set somewhere above
for count = 1:Iteration_lim + 1
%Error check for function not converging
if count == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
end
%Closure equations 1 and 2
C1 = L1*cos(angleset1(1)) - 2.4*cos(angleset1(2) + (pi/2)) - 4.5*cos(angleset1(2)) - 2.4;
C2 = L1*sin(angleset1(1)) - 2.4*sin(angleset1(2) + (pi/2)) - 4.5*sin(angleset1(2)) + 2.1;
f1 = [C1;C2];
%Error check for tolerance limit being exceeded
if abs(f1(1)) < Tolerance && abs(f1(2)) < Tolerance
break
end
%Partial derivatives of C1 and C2
derivC1 = [-L1*sin(angleset1(1)), (4.5*sin(angleset1(2))+2.4*cos(angleset1(2)))];
derivC2 = [L1*cos(angleset1(1)), (-4.5*cos(angleset1(2))+2.4*sin(angleset1(2)))];
derivf1 = [derivC1;derivC2];
%Newton Raphson step
angleset(count+1) = angleset(count) - (inv(derivf1))*f1;
end
Ok thats great help thank you.
Hi Geoff sorry to have to get back to you but I could not get that method to work. Below is my full function as it was before I attempted to implement your method.
function [angleset] = Backhoe_Angles(X,Y,Z)
%[angleset] = Backhoe_Angles(X,Y,Z)
%This function is used to calculate the angles of the backhoe in question
%for a given configuration.
%
%Inputs:
%X (real scalar)the percentage of elongation from the minimum length of the
%first ram
%Y (real scalar)the percentage of elongation from the minimum length of the
%second ram
%Z (real scalar)the percentage of elongation from the minimum length of the
%third ram
%
%Outputs:
%angleset is the array containing the eight calculated angles
%
%Version 2: created 02/04/2020. Author: Darragh Tobin
%Ensure inputs are valid percentages
if ((X || Y || Z) < 0) || ((X || Y || Z) > 100)
error('Inputs X,Y,Z must be in the range of 0-100')
end
%Defining the lengths of the three rams, i.e min length of L1 is estimaated to be 2.4 and
%max length is estimated to be 2.4+4.8=7.2
L1 = 2.4 + (X/100)*(4.8);
L2 = 8.7 + (Y/100)*(1.8);
L3 = 11.1 + (Z/100)*(3.1);
Tolerance = 10^-5; %Sets toleranc limit
Iteration_lim = 20; %Sets iteration limit
%Angles 1 and 2
angleset1 = [30*pi/180; 40*pi/180]; %Initial estimates of theta1 and theta2, converted to radians
for count1 = 1:Iteration_lim + 1
%Error check for function not converging
if count1 == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
end
%Closure equations 1 and 2
C1 = L1*cos(angleset1(1)) - 2.4*cos(angleset1(2) + (pi/2)) - 4.5*cos(angleset1(2)) - 2.4;
C2 = L1*sin(angleset1(1)) - 2.4*sin(angleset1(2) + (pi/2)) - 4.5*sin(angleset1(2)) + 2.1;
f1 = [C1;C2];
%Error check for tolerance limit being exceeded
if abs(f1(1)) < Tolerance && abs(f1(2)) < Tolerance
break
end
%Partial derivatives of C1 and C2
derivC1 = [-L1*sin(angleset1(1)), (4.5*sin(angleset1(2))+2.4*cos(angleset1(2)))];
derivC2 = [L1*cos(angleset1(1)), (-4.5*cos(angleset1(2))+2.4*sin(angleset1(2)))];
derivf1 = [derivC1;derivC2];
%Newton Raphson step
angleset1 = angleset1 - (inv(derivf1))*f1;
end
%Angles 3 and 4
angleset2 = [165*pi/180, 105*pi/180]; %Initial estimates of theta3 and theta4, converted to radians
for count2 = 1:Iteration_lim + 1
%Error check for function not converging
if count2 == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
end
%Closure equations 3 and 4
C3 = -L2*cos(angleset2(1)) - 0.7*cos(angleset2(2) - (pi/2)) - 0.7*cos(angleset2(2)) - 9;
C4 = -L2*sin(angleset2(1)) - 0.7*sin(angleset2(2) - (pi/2)) - 0.7*sin(angleset2(2)) + 2.9;
f2 = [C3;C4];
%Error check for tolerance limit being exceeded
if abs(f2(1)) < Tolerance && abs(f2(2)) < Tolerance
break
end
%Partial derivatives of C3 and C4
derivC3 = [L2*sin(angleset2(1)), (-0.7*cos(angleset2(2))+0.7*sin(angleset2(2)))];
derivC4 = [-L2*cos(angleset2(1)), (-0.7*sin(angleset2(2))-0.7*cos(angleset2(2)))];
derivf2 = [derivC3;derivC4];
%Newton Raphson step
angleset2 = angleset2 - (inv(derivf2))*f2;
end
%Angles 5 and 6
angleset3 = [pi, 60*pi/180]; %Initial estimates of theta5 and theta6, converted to radians
for count3 = 1:Iteration_lim + 1
%Error check for function not converging
if count3 == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
end
%Closure equations 5 and 6
C5 = L3*cos(angleset3(1)) + 2.1*cos(angleset3(2)) + 11.3;
C6 = L3*sin(angleset3(1)) + 2.1*sin(angleset3(2)) + 1.5;
f3 = [C5;C6];
%Error check for tolerance limit being exceeded
if abs(f3(1)) < Tolerance && abs(f3(2)) < Tolerance
break
end
%Partial derivatives of C5 and C6
derivC5 = [-L3*sin(angleset3(1)), (-2.1*sin(angleset3(2)))];
derivC6 = [L3*cos(angleset3(1)), (2.1*cos(angleset3(2)))];
derivf3 = [derivC5;derivC6];
%Newton Raphson step
angleset3 = angleset3 - (inv(derivf3))*f3;
end
%Angles 7 and 8
angleset4 = [160*pi/180; 50*pi/180]; %Initial estimate for theta7 and theta8, converted to radians
for count4 = 1:Iteration_lim + 1
%Error check for function not converging
if count4 == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
end
%Closure equations 7 and 8
C7 = 2.9*cos(angleset4(1)) + 3.3*cos(angleset4(2)) - 2.1*cos(angleset3(2)) + 1;
C8 = 2.9*sin(angleset4(1)) + 3.3*sin(angleset4(2)) - 2.1*sin(angleset3(2)) - 0.6;
f4 = [C7;C8];
%Error check for tolerance limit being exceeded
if abs(f4(1)) < Tolerance && abs(f4(2)) < Tolerance
break
end
%Partial derivatives of C7 and C8
derivC7 = [-2.9*sin(angleset4(1)), -3.3*sin(angleset4(2))];
derivC8 = [2.9*cos(angleset4(1)), 3.3*cos(angleset4(2))];
derivf4 = [derivC7;derivC8];
%Newton Raphson step
angleset4 = angleset4 - (inv(derivf4))*f4;
end
%Output angleset is an array containing the eight calculated angles
angleset = [angleset1(1);angleset1(2);angleset2(1);angleset2(2);angleset3(1);angleset3(2);angleset4(1);angleset4(2)];
Darragh - please clarify what you mean by I could not get that method to work. Were there errors? If so, what were they? It seems that angleset1 is a 2x1 array and not a scalar (which I assumed). I think that you could change the code to do
angleset = [];
angleset(:,1) = angleset1; % a 2x1
for count = 1:Iteration_lim + 1
% other code
%Newton Raphson step
angleset(:, count+1) = angleset(:, count) - (inv(derivf1))*f1;
end
You would need to step through the code to make sure the above works....
It returns an error saying "Unable to perform assignment because the left and right sides have a different number of elements"
When you say
angleset = [];
Do you mean I should include my estimates for all eight angles in array at the beginning of the code, rather than estimating two at the beginning of each loop?
angleset = [];
just initializes a local variable named angleset to an empty array.
%Angles 1 and 2
angleset1 = [30*pi/180; 40*pi/180]; %Initial estimates of theta1 and theta2, converted to radians
angleset = [];
angleset(:,1) = angleset1;
for count1 = 1:Iteration_lim + 1
%Error check for function not converging
if count1 == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
end
%Closure equations 1 and 2
C1 = L1*cos(angleset1(1)) - 2.4*cos(angleset1(2) + (pi/2)) - 4.5*cos(angleset1(2)) - 2.4;
C2 = L1*sin(angleset1(1)) - 2.4*sin(angleset1(2) + (pi/2)) - 4.5*sin(angleset1(2)) + 2.1;
f1 = [C1;C2];
%Error check for tolerance limit being exceeded
if abs(f1(1)) < Tolerance && abs(f1(2)) < Tolerance
break
end
%Partial derivatives of C1 and C2
derivC1 = [-L1*sin(angleset1(1)), (4.5*sin(angleset1(2))+2.4*cos(angleset1(2)))];
derivC2 = [L1*cos(angleset1(1)), (-4.5*cos(angleset1(2))+2.4*sin(angleset1(2)))];
derivf1 = [derivC1;derivC2];
%Newton Raphson step
angleset(:, count+1) = angleset(:,count) - (inv(derivf1))*f1;
end
This is what I have written for the first for loop. The error I get is "Error using count. Not enough input arguments"
The error message suggests that a function count exists somewhere in your MATLAB search path and that your code is trying to call that function at (probably) line
angleset(:, count+1) = angleset(:,count) - (inv(derivf1))*f1;
Note here you are using count but your indexing/iterating variable is count1
for count1 = 1:Iteration_lim + 1
so just do
angleset(:, count1+1) = angleset(:,count1) - (inv(derivf1))*f1;
instead.
Ok I corrected that mistake but now my function fails to converge for all input values.
Have you tried stepping through the code to check if the results at each line make sense? Are the angles in angleset reasonable?

Accedi per commentare.

I stepped through all of my code and could still not get the function to converge. My professor supplied this code and said this is how he returned a vlue for count. However, I dont see how it would work as the count on the second line does not appear to do anything and he does not have count as an output. Also, he includes a break statement after an error message which also confuses me.
for count = 1:Iteration_limit + 1
count;
if count == Iteration_limit + 1 % Function returns error if function doesn't convergeerror('Iteration limit reached. Iteration did not converge')
break
end
He claims that the calling the variable count on the second line is sufficient however, when I try to do this I get the error " 'count' produces a value that might be unused.

1 Commento

I suspect that "count' produces a value that might be unused." is a warning and not an error so it can be ignored. You are right that having "count;" on the second line does nothing at all. The problem with the convergence probably has nothing to do with this but may instead have something to do with your implementation of the code. Presumably when you say that fails to converge for all input values you mean that you don't get the correct answer and that your code always reaches the maximum number of iterations. Is this correct?

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 3 Apr 2020

Commentato:

il 8 Apr 2020

Community Treasure Hunt

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

Start Hunting!

Translated by