clc; clearvars; close all;

a=4*10^6;
b=5.0*10^6;
error=0.000001;

p2=4*10^6;
M=2.0156*10^-3;
R=8.314;
T=281.1;
w=62.43;
lambda=0.0219;
l=100;
d=0.200;

F = @(x) log(x/p2)-(M/(R*T*w^2))*(x^2-p2^2)+lambda*(l/d);

% Metoda půlení intervalů
if(F(a)*F(b)<0)
    p1_try=(a+b)/2;

    if(abs(F(p1_try))<error)
        p1=p1_try;

    else
        while(abs(F(p1_try))>error)

            if(F(a)*F(p1_try)<0)
                b=p1_try;
                p1_try=(a+p1_try)/2;
            else
                a=p1_try;
                p1_try=(b+p1_try)/2;
            end

        end

    end
    p1=p1_try;

else
    disp("ERROR");
end

disp("Tlak p1= "+p1);