0% found this document useful (0 votes)
3 views

EXP02_MATLAB(1)

The document contains MATLAB code implementing the Bisection and Newton-Raphson methods to solve equations for non-ideal gases. It calculates specific volume and mass of methane in a tank using the Bisection method, and finds the root of the equation using the Newton-Raphson method with user-defined iterations and accuracy. Both methods utilize properties of the gas and require initial guesses for convergence.

Uploaded by

ifyouwantcs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

EXP02_MATLAB(1)

The document contains MATLAB code implementing the Bisection and Newton-Raphson methods to solve equations for non-ideal gases. It calculates specific volume and mass of methane in a tank using the Bisection method, and finds the root of the equation using the Newton-Raphson method with user-defined iterations and accuracy. Both methods utilize properties of the gas and require initial guesses for convergence.

Uploaded by

ifyouwantcs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Bisection:

%Bisection method to solve equation for non-ideal gas


%Source code by Aryan Raj(123MM0686)

clc
clear all

R = 0.518; % kJ/(kg·K)
Tc = 191; % K
Pc = 4600; % kPa

a = 0.427 * R^2 * Tc^2.5 / Pc;


b = 0.0866 * R * Tc / Pc;

T = -40 + 273.15; % Convert temperature to Kelvin


P = 65000; % Pressure in kPa
V_tank = 3; % Tank volume in m³

% Non-ideal gas equation function


f = @(V) P - (R * T / (V - b)) + (a / (V * (V + b) * sqrt(T)));

tol = 1e-6; % Tolerance for convergence


max_iter = 1000; % Maximum number of iterations
V_low = b + 1e-6; % Lower bound of V (just above b)
V_high = 1; % Upper bound of V (initial guess)
iter = 0;

while iter < max_iter


V_mid = (V_low + V_high) / 2;
if abs(f(V_mid)) < tol
break;
elseif f(V_mid) * f(V_low) < 0
V_high = V_mid;
else
V_low = V_mid;
end
iter = iter + 1;
end

% Check for convergence


if iter == max_iter
error('Bisection method did not converge.');
else
fprintf('Converged in %d iterations.\n', iter);
end

% Specific volume V and mass calculation


V_specific = V_mid; % Specific volume (m³/kg)
mass = V_tank / V_specific; % Mass in kg

% Output the result


fprintf('The amount of methane that can be held in the tank is %.2f kg.\n',
mass);

OUTPUT:
Newton-Raphson:

%Newton Ralphson method to solve given equation.


%Source code by Aryan Raj(123MM0686)

Tc = 191; % K
Pc = 4600; % kPa
T = 233; % K
P = 65000; % kPa
R = 0.518; % kJ/kg/K
% Calculate a and b
a = 0.427 * R^2 * Tc^2.5 / Pc;
b = 0.0866 * R * T / P;

% Define the non-ideal gas equation and its derivative


f = @(v) P - (R * T / (v - b)) + (a / (v * (v + b) * sqrt(T)));
df = @(v) R * T / (v - b)^2 - (a * (2 * v + b)) / (v^2 * (v + b)^2 * sqrt(T));

% Input for iterations and accuracy


n = input('Enter the number of iterations: ');
acc = input('Enter the accuracy of the root: ');

% Initial guess
ans1 = 0;
while ans1 == 0
x0 = input('Enter the initial guess : ');
if abs(f(x0) / df(x0)) < 1
ans1 = 1;
else
fprintf('\nEnter a new guess:\n');
end
end

% Newton-Raphson Method
for i = 1:n
x1 = x0 - f(x0) / df(x0);
if abs(x1 - x0) < acc
break;
end
x0 = x1;
end

% Accuracy check
if abs(x1 - x0) > acc
fprintf('\nNumber of iterations are not sufficient to achieve desired
accuracy.\n');
else
fprintf('\nRoot (v) of the equation using Newton-Raphson Method = %.6f
m³/kg\n', x1);
end
Enter the number of iterations: 20
Enter the accuracy of the root: 0.001
Enter the initial guess (v): 0.003

Root (v) of the equation using Newton-Raphson Method = 0.002807 m³/kg

You might also like