clear
close
clc
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
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/589255-fix-an-infinite-while-loop-by-properly-counting-the-number-of-elements-added-to-an-array#comment_997699
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/589255-fix-an-infinite-while-loop-by-properly-counting-the-number-of-elements-added-to-an-array#comment_997699
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/589255-fix-an-infinite-while-loop-by-properly-counting-the-number-of-elements-added-to-an-array#comment_1019950
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/589255-fix-an-infinite-while-loop-by-properly-counting-the-number-of-elements-added-to-an-array#comment_1019950
Sign in to comment.