k_testArray = linspace(kL,kU, 501);
rho_valid = NaN(size(k_testArray));
for jj = 1:length(k_testArray)
rho = mnhf_secant(@FourPlumes_Equation, [0.1 0.5], 1e-6, 0);
if rho > 0 && isreal(rho) && isfinite(rho)
fprintf('Detected critical k value (first contact with θ = π/4): k_crit = %.8f\n\n', k_crit);
Detected critical k value (first contact with θ = π/4): k_crit = 0.87670898
theta_array = linspace(-pi/2, pi/2, Nt);
area = zeros(1,length(k_array));
figure(1); hold on; box on
for jj = 1:length(k_array)
rho_array1 = zeros(1, Nt);
rho_array1(ii) = mnhf_secant(@FourPlumes_Equation, [1.5 2], 1e-6, 0);
[x1, y1] = pol2cart(theta_array, rho_array1);
plot(x1, y1, '-k'); axis square; xlim([0 2]); ylim([-1 1]);
x_diag = linspace(0, 2, Nt); y_diag = x_diag;
plot(x_diag, y_diag, 'r--'); plot(x_diag, -y_diag, 'r--');
area(jj) = 2 * abs(trapz(x1(min(iq):(Nt-1)/2+1), y1(min(iq):(Nt-1)/2+1)));
area(jj) = area(jj) + x1(min(iq)-1)^2;
rho_array1 = zeros(1, Nt);
rho_array1(ii) = mnhf_secant(@FourPlumes_Equation, [1.5 2], 1e-6, 0);
[x1, y1] = pol2cart(theta_array, rho_array1);
plot(x1, y1, '-k'); axis square;
ij = find(isnan(rho_array1(1:round(end/2))));
theta_min = theta_array(max(ij));
theta_array2 = linspace(theta_min, -theta_min, Nt);
rho_array2 = zeros(1, Nt);
theta = theta_array2(ii);
rho_array2(ii) = mnhf_secant(@FourPlumes_Equation, [0.1 0.9], 1e-6, 0);
[x2, y2] = pol2cart(theta_array2, rho_array2);
ik = (Nt-1)/2 + find(isnan(x2((Nt-1)/2+1:end)));
x = [x2((Nt-1)/2+1:min(ik)-1), x1(max(ij)+1:(Nt-1)/2+1)];
y = [y2((Nt-1)/2+1:min(ik)-1), -y1(max(ij)+1:(Nt-1)/2+1)];
plot(x, y, 'b--'); plot(x, -y, 'b--');
area(jj) = 2 * trapz(x, y);
pos1 = [1 - r0, -r0, 2*r0, 2*r0];
pos2 = [-1 - r0, -r0, 2*r0, 2*r0];
pos3 = [-r0, 1 - r0, 2*r0, 2*r0];
pos4 = [-r0, -1 - r0, 2*r0, 2*r0];
rectangle('Position', pos1, 'Curvature', [1 1], 'FaceColor', 'k');
rectangle('Position', pos2, 'Curvature', [1 1], 'FaceColor', 'k');
rectangle('Position', pos3, 'Curvature', [1 1], 'FaceColor', 'k');
rectangle('Position', pos4, 'Curvature', [1 1], 'FaceColor', 'k');
function f = FourPlumes_Equation(rho)
f = (rho.^2 - 2*rho*cos(theta) + 1 - r0^2) .* ...
(rho.^2 - 2*rho*cos(theta + pi/2) + 1 - r0^2) .* ...
(rho.^2 - 2*rho*cos(theta + pi) + 1 - r0^2) .* ...
(rho.^2 - 2*rho*cos(theta + 1.5*pi) + 1 - r0^2) - k^2;
function r=mnhf_secant(Fun,x,tol,trace)
if nargin < 4, trace = 1; end
if nargin < 3, tol = 1e-8; end
error('Please provide two initial guesses')
x3 = x(1)-f(1)*(x(2)-x(1))/(f(2)-f(1));
if trace, fprintf(1,'%3i %12.5f %12.5f\n', i,x3,f3); end
x(1) = x(2); f(1) = f(2); x(2) = x3; f(2) = f3;