Matlab Code for Mathematics Problems

Root Mean Square average

The sample solution provided here is for a set of five questions. The brief of questions, the solution to which has been demonstrated by the expert, is summarized as follows : –

Q 1. The expert has written a Matlab code using if else statements which implements a given function.

Q 2. Expression for friction factor as a function of Reynolds number is given for incompressible flow in a smooth pipe. The expert has demonstrated by writing the Matlab code for friction factor for some given values of Reynolds number.

Q 3&4. The power series for π is given in question. Matlab code has been demonstrated by the expert using “while” loop and using “for” loop in two separate answers for the given condition.

Q 5. Data set is given, expert has showcased a Matlab code using “for” loop for finding out statistical values like Harmonic mean, Geometric mean, Root Mean Square average.

SOLUTION : –

clc, clear all, close all

%% Question 1

fprintf(‘%%%%%%%% QUESTION 1 %%%%%%%%\n\n’);

x = [8; 4; 3; 9; 0; -5];            % Define the data

n = length(x);                      % number of elements

f_x = zeros(n, 1);                  % Vector to store the results

k = 1;

for i = 1:n                         % iterate through all the value of x

xi = x(i);

if xi <= 1              % x <= 1

f_x(k) = 0;

elseif xi > 1 && xi < 5 % 1 < x < 5

f_x(k) = (xi-1)^2;

elseif xi >= 5          % x >= 5

f_x(k) = 16;

end

k = k + 1;

end

Results_Q1 = table(x, f_x)          % Create table with results

%% Question 2

fprintf(‘\n%%%%%%%% QUESTION 2 %%%%%%%%\n\n’);

Re = [3e6; 4e4; 9e8; 3500; 1800; 500];   % Data

n = length(Re);                     % Number of elements

f_Re = zeros(n,1);                  % Vector to store the results

k = 1;

for i = 1:n                         % iterate through all the values

Rei = Re(i);

if Rei <= 2100                  % Re <= 2100

f_Re(k) = 16/Rei;

elseif Rei > 2100 && Rei < 10^5 % 2100 < Re < 10^5

f_Re(k) = 0.0791/(Rei^0.25);

elseif Rei >= 10^5              % Re >= 10^5

f_Re(k) = 0.004;

end

k = k + 1;

end

Results_Q2 = table(Re, f_Re)        % Create table with results

%% Question 3

fprintf(‘\n%%%%%%%% QUESTION 3 %%%%%%%%\n\n’);

e = 10^(-8);                        % epsilon (min error)

error = 1e50;                       % initial error

k = 0;

pi_calc = 0;                        %  variable to store the calculated value for pi

sumvar = 0;                         % Variable to store the sum of the terms

while(error > e)

term = ((-3)^(-k))/(2*k+1);     % current term of the sum

sumvar = sumvar + term;         % Update the value of the sum

pi_calc = sqrt(12)*sumvar;      % Current value of pi calculated

error = abs(term/pi_calc); % calculate current error

k = k + 1;

end

k = k – 1;

fprintf(‘After %s iterations, the value of pi calculated is: %s\n’, num2str(k), num2str(pi_calc));

%% Question 4

fprintf(‘\n%%%%%%%% QUESTION 4 %%%%%%%%\n\n’);

e = 10^(-8);                        % epsilon (min error)

error = 1e50;                       % initial error

pi_calc = 0;                        %  variable to store the calculated value for pi

sumvar = 0;                         % Variable to store the sum of the terms

n = 1e6;                            % Max number of terms to consider. This value is reached only if the condition error < e is never met

for k = 0:n

term = ((-3)^(-k))/(2*k+1);     % current term

sumvar = sumvar + term;         % Update the value of the sum

pi_calc = sqrt(12)*sumvar;       % Current value of pi calculated

error = abs(term/pi_calc);

if error < e                    % if current error is less than e (10^-8) then break the loop

break;

end

end

fprintf(‘After %s iterations, the value of pi calculated is: %s\n’, num2str(k), num2str(pi_calc));

%% Question 5

fprintf(‘\n%%%%%%%% QUESTION 5 %%%%%%%%\n\n’);

DATA = [92.3, 93.2, 91.9, 93.5, 92.7, 93.1, 93.8, 92.4];    % Define the data

n = length(DATA);

%%      PART a

fprintf(‘   Part a)\n’);

sum_inv = 0;

for i = 1:n

xi = DATA(i);

sum_inv = sum_inv + 1/xi;                       % Calculate the sum of the inverse of the values (1/x1 + 1/x2 + … + 1/xn)

end

Hm = n/sum_inv;                                     % Formula for Hm

fprintf(‘The value of Hm is: %s\n\n’, num2str(Hm));

%%      Part b

fprintf(‘   Part b)\n’);

product_vals = 1;                                   % Variable to store the multiplication of all data values

for i = 1:n

xi = DATA(i);

product_vals = product_vals*xi;                 % Calculate the product of all the values (x1*x2*…*xn)

end

Gm = product_vals^(1/n);                            % Formula for Gm

fprintf(‘The value of Gm is: %s\n\n’, num2str(Gm));

%%      Part c

fprintf(‘   Part c)\n’);

sum_sq = 0;                                         % Variable to store the sum of the squares of all the values

for i = 1:n

xi = DATA(i);

sum_sq = sum_sq + xi^2;                         % Calculate the sum of the squares (x1^2 + x2^2 + … + xn^2)

end

RMSa = sqrt(sum_sq/n);                              % Formula for RMSa

fprintf(‘The value of RMSa is: %s\n’, num2str(RMSa));