You are on page 1of 5

Assignment 6

a)

i) To find the constants c1 and c2 that best fit the model (1) to the experimental data, we
can formulate the residual in the form r = Ax - b, where x represents the vector [c1; c2].
In this special case with n = 4 data points, we can define the elements of matrix A and
column vector b as follows:

Matrix A:

Each row of the matrix A corresponds to a data point (Rej, λj).

The first column of A contains 1 for each data point.

The second column of A contains ln(Rej * sqrt(λj)) for each data point.

Column Vector b:

Each element of the column vector b corresponds to a data point and is calculated as 1 / sqrt(λj).

Now, we can express the residual as a linear combination of the coefficients c1 and c2:

rj = c1 + c2 * ln(Rej * sqrt(λj)) - 1 / sqrt(λj)

The residual vector r, the coefficient vector x, matrix A, and vector b can be defined as follows:

Residual Vector r:

r = [r1; r2; r3; r4], where each rj is given by the formula above.

Coefficient Vector x:

x = [c1; c2]

Matrix A:

A = [1, ln(Re1 * sqrt(λ1)); 1, ln(Re2 * sqrt(λ2)); 1, ln(Re3 * sqrt(λ3)); 1, ln(Re4 * sqrt(λ4))]

Column Vector b:

b = [1 / sqrt(λ1); 1 / sqrt(λ2); 1 / sqrt(λ3); 1 / sqrt(λ4)]

ii)

function c = fitFriction(Re, lam)

% fitFriction - Fit the friction factor model to experimental data

% c = fitFriction(Re, lam) takes column vectors Re and lam, containing


% the Reynolds numbers (Rej) and measured friction factors (λj), and

% calculates the coefficients c1 and c2 that minimize the norm of the

% residual vector for the model √λ = c1 + c2 * ln(Re√λ).

% Author: Adam Faisal

% Date: 31/10/2023

% Input:

% - Re: Reynolds number (a column vector)

% - lam: Friction factor (a column vector)

% Output:

% - c: Coefficients [c1, c2] as a column vector

% Check that Re and lam have the same length

if length(Re) ~= length(lam)

error('Input vectors must have the same length');

end

% Formulate the matrix A and vector b

n = length(Re);

A = [ones(n, 1), log(Re ./ sqrt(lam))];

b = 1 ./ sqrt(lam);

% Use the least squares method to find the values of c

c = A \ b;

end

b)

function c = fitFriction(Re, lam)


% Input:
% - Re: Reynolds number (a vector)
% - lam: Friction factor (a vector)
% Output:
% - c: Coefficients [c1, c2] as a column vector

% Check that Re and lam have the same length


if length(Re) ~= length(lam)
error('Input vectors must have the same length');
end

% Formulate the matrix A and vector b


n = length(Re);
A = [ones(n, 1), log(Re ./ sqrt(lam))];
b = 1 ./ sqrt(lam);

% Use the least squares method to find the values of c


c = A \ b;
end
function lam = evalFriction(Re, c)
% Input:
% - Re: Reynolds number (a scalar value)
% - c: Coefficients [c1, c2] as a column vector
% Output:
% - lam: Friction factor (a single value) that approximately satisfies (1)
for the input Re

% Extract coefficients
c1 = c(1);
c2 = c(2);

% Initialize lam_guess with an arbitrary value


lam_guess = 0.02; % Adjust this value as needed

% Define the function f(lambda) to find the root


f = @(lambda) c1 + c2 * log(Re * sqrt(lambda)) - 1 / sqrt(lambda);

% Perform the Newton-Raphson iteration


while true
residual = f(lam_guess);
if abs(residual) < 1e-6 % Check for convergence
lam = lam_guess;
return;
end
% Update lam_guess using the Newton-Raphson method
lam_guess = lam_guess - residual / derivative(f, lam_guess);
end
end

function df = derivative(f, x)
% Calculate the derivative of function f at point x using a small
perturbation
h = 1e-6; % Small perturbation
df = (f(x + h) - f(x)) / h;
end
c)

% Princeton SuperPipe Friction Factor Fitting


% Author: Adam Faisal
% Date: 31/10/2023

% Load the data


load('superpipe_friction.mat');

% Fit the data to Equation (1) using fitFriction


c = fitFriction(ReObs, lamObs);
c1 = c(1);
c2 = c(2);

% Create a range of Reynolds numbers for evaluation


Re = logspace(log10(3e4), log10(2e7), 20);

% Initialize an array to store the calculated friction factors


lamCalculated = zeros(size(Re));

% Calculate the friction factor using evalFriction for each Reynolds number
for i = 1:length(Re)
lamCalculated(i) = evalFriction(Re(i), c);
end

% Create a log-log plot


loglog(Re, lamCalculated, 'b-', ReObs, lamObs, 'ro', 'MarkerSize', 6);

% Add labels and a legend


xlabel('Reynolds Number');
ylabel('Friction Factor (\lambda)');
title('Friction Factor in the Princeton SuperPipe');
legend(sprintf('Model: c1 = %.4f, c2 = %.4f', c1, c2), 'Data Points',
'Location', 'Best');

% Save the plot as an image (optional)


% saveas(gcf, 'friction_factor_plot.png');

% Display the plot


grid on;

d)

You might also like