Fix an infinite while loop by properly counting the number of elements added to an array

2 views (last 30 days)
I am trying to calculate the condition numbers of multiple square 2x2 matrices. I generate a matrix A with random value of l, m and n and of the form A = [l,m;m,n];. Then I calculate A's eigenvalues with the built in MATLAB function. If the eigenvalues are real I store matrix A and the matlab condition number in their appropriate arrays, and then calculate the theoretical condition number for A an store that value in an array. The total amount of stored matrices should be 100, but my code currently runs forever b/c my acceptance counter which is supposed to track how many matrices Ive stored does not activate. Any help with solving this problem would be appreciated. My code is posted below.
clear
close
clc
% Create matrix A and calculate Eigenvalues
acceptance_count = 0;
itr = 0;
A_storage = {};
while acceptance_count < 100
itr = itr+1;
A = zeros(2);
l = rand(1);
m = rand(1);
n = rand(1);
A(1,1) = l;
A(1,2) = m;
A(2,1) = m;
A(2,2) = n;
[K_mat, A, tf] = K_matlab(A);
Kmat(itr) = K_mat;
A_storage(itr) = {A};
if tf == 1
A_storage(itr) = {A};
acceptance_count = acceptance_count+1;
[lambda, K_th] = K_quadtheory(1,(l+n),(l*n-m^2));
Kth(itr) = K_th;
end
end
%%
function [K_mat, A, tf] = K_matlab(A)
eigen_values = eig(A);
tf = isreal(eigen_values);
if tf == 1
eigen_max = max(eigen_values);
eigen_min = min(eigen_values);
K_mat = abs(eigen_max/eigen_min);
end
end
function [lambda, K_th] = K_quadtheory(a,b,c)
lambda = zeros(2,1);
d = sqrt(b*b-4*a*c);
lambda(1) = (-b-d)/(2*a);
if b*b/(4*a*c) > 1.e5
lambda(2) = -2*c/(b+d);
else
lambda(2) = (-b+d)/(2*a);
end
K_th = abs(max(lambda)/min(lambda));
end
  2 Comments
Star Strider
Star Strider on 5 Sep 2020
To do the Updates:
In the MATLAB window, either:
Add-Ons —> Check for Updates
or:
Help —> Check for Updates
(I don’t remember when that changed.)
I deleted my Answer since it was not Acecepted.

Sign in to comment.

Accepted Answer

James Peach
James Peach on 5 Sep 2020
EDIT: I solved my problem by moving the acceptance counter into my if statement within the while loop instead of relying on changing the size of the storage matrix.

More Answers (1)

Steven Lord
Steven Lord on 5 Sep 2020
There's at least one bug and a bit of an unusual pattern in your code. Let's skip down to your call to K_matlab.
[K_mat, A, tf] = K_matlab(A);
If K_matlab modified its input argument A internally, returning it would make sense. But looking at that function:
function [K_mat, A, tf] = K_matlab(A)
eigen_values = eig(A);
tf = isreal(eigen_values);
if tf == 1
eigen_max = max(eigen_values);
eigen_min = min(eigen_values);
K_mat = abs(eigen_max/eigen_min);
end
end
A is not modified in K_matlab. So why return the (unmodified) A from K_matlab?
This is also where a bug is located. What happens if your eigenvalues are not real? tf will be false and the body of the if statement will not execute. So what does K_matlab return as its first output argument in that scenario? K_mat was never assigned a value (that only occurs in the body of the if statement that was skipped.) So MATLAB will error.
I'm assuming this is part of a homework assignment, since otherwise you should just use the built-in cond function.
  1 Comment
James Peach
James Peach on 5 Sep 2020
Edited: James Peach on 5 Sep 2020
I removed the A cause intially I was modifiying it in the function, but abandon that tack once I came up with the current code. Also the eigenvalues can never be not real numbers, since each of the values of l m and n are always positive and 0 isn't a positve number so the calculations for lambda can be a bit less robust.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!