Break statement inside an if statement
Mostra commenti meno recenti
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)
Geoff Hayes
il 3 Apr 2020
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
Darragh Tobin
il 3 Apr 2020
Modificato: Geoff Hayes
il 3 Apr 2020
Geoff Hayes
il 3 Apr 2020
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
Darragh Tobin
il 3 Apr 2020
Darragh Tobin
il 3 Apr 2020
Geoff Hayes
il 3 Apr 2020
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....
Darragh Tobin
il 4 Apr 2020
Geoff Hayes
il 4 Apr 2020
angleset = [];
just initializes a local variable named angleset to an empty array.
Darragh Tobin
il 6 Apr 2020
Modificato: Geoff Hayes
il 6 Apr 2020
Geoff Hayes
il 6 Apr 2020
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.
Darragh Tobin
il 7 Apr 2020
Geoff Hayes
il 7 Apr 2020
Have you tried stepping through the code to check if the results at each line make sense? Are the angles in angleset reasonable?
Darragh Tobin
il 8 Apr 2020
1 Commento
Geoff Hayes
il 8 Apr 2020
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?
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!