Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using matlab, please answer what you can. In a chemical engineering process, wat

ID: 3826959 • Letter: U

Question

using matlab, please answer what you can.

In a chemical engineering process, water vapor (H2O) is heated to sufficiently high temperatures that a significant portion of the water dissociates, or splits apart, to form oxygen (O2) and hydrogen (H2).

If it is assumed that this is the only reaction involved, the mole fraction, x, of H2O that dissociates can be represented by:

Where K is the reaction’s equilibrium constant and pt is the total pressure of the mixture.

QUESTIONS:

1. Write two function files which perform the bisection and modified secant methods separately. The bisection-method function you write must perform the search for the root in the upper bracket.

2. Assume pt to be 3.5 find the value of x when K=0.04. Plot the function that you are solving. Then prompt the user to enter a lower and upper limit. If the initial guesses do not bracket a root, your m-file should continue to prompt the user for new guesses until valid guesses are provided. Perform the bisection method using the function file written in part A with a precision of 1e-4 to find the root of the profile and print the root value.

2pt 1 x 2 x

Explanation / Answer

function [p errorAmount result] = bisection(f,a,b,tolerance)
    result = true;
    p = -1;
    errorAmount = -1;
  
    if f(a).*f(b)>0
        result = false;
    else
        p = (a+b)/2;
        errorAmount = abs(f(p));
      
        % some may never converge, so setting maximum limit      
        i = 1;
        MAX_ITERATIONS = 10000;
      
        while errorAmount > tolerance
            if (f(p) == 0.0)
                break;
            end
          
            if f(a)*f(p)<0
              b = p;
            else
              a = p;        
            end
          
            % Maximum number of iterations
            if (i>MAX_ITERATIONS)
                disp("Unable to Converge!!!");
                break;
            end
            i = i+1;
          
            p = (a + b)/2;
            errorAmount = abs(f(p));
        end
    end
end


function [p errorAmount] = secant(f,a,b,tolerance)

    % some may never converge, so setting maximum limit      
    i = 1;
    MAX_ITERATIONS = 10000;
  
    c = (a*f(b) - b*f(a))/(f(b) - f(a));
    errorAmount = abs(f(c));
    while errorAmount > tolerance
        a = b;
        b = c;
        c = (a*f(b) - b*f(a))/(f(b) - f(a));

        % Maximum number of iterations
        if (i>MAX_ITERATIONS)
            disp("Unable to Converge!!!");
            break;
        end
      
        i = i+1;
        errorAmount = abs(f(c));
    end
    p = c;
end


pt = 3.5;
K = 0.04;

% We can re-write the given equation as
f = @(x) ((x.^3) - (2*pt/K^2)*(x.^2) - (3*x) + 2);


% Computing using bisection method
while(true)
    a = input("Enter new guess for lower limit : ");
    b = input("Enter new guess for upper limit : ");
  
    [p errorAmount result] = bisection(f,a,b,exp(-4));
    if (result == false)
        fprintf("Wrong guess! Oops! ");
        continue;
    end  
    fprintf("Root = %f, Error = %f ", p, errorAmount);
    break;
end