####_CW1_Q1_####
clc
clear
close all
A = zeros(11, 11);
Dp = 0.001:0.001:0.01;
Vt = 0.5:0.5:5;
Vt = Vt’;
rho = 1000;
mu = 0.001;
D = meshgrid(Dp);
V = meshgrid(Vt)’;
Re = D.*V*rho/mu;
A(1, 2:11) = Dp;
A(2:11, 1) = Vt;
A(2:11, 2:11) = Re;
figure
mesh(D, V, Re)
xlabel("Diameter")
ylabel("Velocity")
zlabel("Reynolds Numebr")
figure
subplot(1, 2, 1)
plot(V, Re(:, 5), ’go-’)
xlabel("Velocity")
ylabel("Reynolds")
subplot(1, 2, 2)
plot(V, Re(:, 10), ’g*-’)
xlabel("Velocity")
ylabel("Reynolds")
####_CW1_Q2_####
clc
clear
close all
delp = (101.1-150)*10^3;
rho = 999.274;
mu = 7.6087*10^-4;
delz = 300;
d = 0.01:0.01:0.05;
L = 100:10:200;
for i = 1:length(d)
for j = 1:length(L)
va = 30;
for k = 1:1000000
re = d(i)*rho*va/mu;
f = 0.0000468*re^-0.2;
num = 9.8*delz+delp/rho;
den = 1/2-2*f*L(j)/d(i);
vc = sqrt(num/den);
if abs(vc-va) < 0.001
V(j, i) = vc;
break
else
va = vc;
end
end
end
end
A = zeros(12, 6);
A(1, 2:6) = d;
A(2:12, 1) = L;
A(2:12, 2:6) = V;
A
figure
plot(L, V)
xlabel("Length")
ylabel("Velocity")
legend("d1", "d2", "d3", "d4", "d5")
####_CW2_Q1_####
clc
clear
close all
P = 9.38;
R = 0.0821;
b = 0.02967;
alpha = 1.0115;
a = 3.654;
T = 300;
amr = @(V) R*T/(V-b) - alpha*a/(V*(V+b)) - P;
% fplot(amr, [1, 5])
p = 2;
q = 3;
r_ager = 0;
tol = 1519519;
while tol > 0.001
yp = amr(p);
yq = amr(q);
r = (p+q)/2;
yr = amr(r);
if yp*yr < 0
q = r;
else
p = r;
end
tol = abs(r-r_ager)/r;
r_ager = r;
end
disp("Your Volume is " + r)
####_CW2_Q3_####
clc
clear
close all
func = @(t) 77*exp(-1.5*t)+20*exp(-0.08*t)-0.01;
dfunc = @(t) -(115.5*exp(-1.5*t)+1.6*exp(-.08*t));
ratio = @(t) func(t)/dfunc(t);
t_ager = input("Enter guess : ");
t = 0;
% fplot(func, [5 500])
% ylim([-1, 1])
while abs(t-t_ager)/t_ager > 0.001
t = t_ager - ratio(t_ager);
t_ager = t;
end
disp("Your t is "+ t)
####_Trapezoidal_Rule_####
clc
clear
close all
f = @(x) x^2;
a = 2;
b = 5;
n = 100;
h = (b-a)/n;
sum = 0;
for i = 1:n-1
sum = sum + f(a+i*h);
end
area = h*(f(a)+f(b)+2*sum)/2;
disp("Your area is : " + area)
####_Simpsons_One_Third_####
clc
clear
close all
f = @(x) x^2;
a = 2;
b = 5;
n = 100;
h = (b-a)/n;
sum1 = 0;
for i = 1:2:n-1
sum1 = sum1 + f(a+i*h);
end
sum2 = 0;
for i = 2:2:n-2
sum2 = sum2 + f(a+i*h);
end
Area = h*(f(a)+f(b)+4*sum1+2*sum2)/3;
disp("Your area is : " + Area)
####_Simpsons_Three_Eight_####
clc
clear
close all
f = @(x) x^2;
a = 2;
b = 5;
n = 120;
h = (b-a)/n;
sum = 0;
for i = 1:n-1
if rem(i, 3) == 0
sum = sum + 2*f(a+i*h);
else
sum = sum + 3*f(a+i*h);
end
end
Area = 3*h*(f(a)+f(b)+sum)/8;
disp("Your area is : " + Area)
####_Weedle_####
clc
clear
close all
f = @(x) x^2;
a = 2;
b = 5;
n = 120;
h = (b-a)/n;
sum = 0;
for i = 1:n-1
if rem(i, 6) == 1 || rem(i, 6) == 5
sum = sum + 5*f(a+i*h);
elseif rem(i, 6) == 3
sum = sum + 6*f(a+i*h);
elseif rem(i, 6) == 0
sum = sum + 2*f(a+i*h);
else
sum = sum + f(a+i*h);
end
end
Area = 3*h*(f(a)+f(b)+sum)/10;
disp("Your area is : " + Area)
####_Bisection_####N
R = 0.0821;
b = 0.02967;
z = 1.0115;
a = 3.654;
T = 300;
f = @(V) R*T/(V-b) - z*a/(V*(V+b)) - 9.38;
l = 0.03;
u = 10;
tol = 1e-9;
max_iter = 100;
[root, iterations] = bisection(f, l, u, tol, max_iter);
fprintf(’Root found: %.8f\n’, root);
fprintf(’Number of iterations: %d\n’, iterations);
fprintf(’Function value at root: %.2e\n’, f(root));
function [root, iterations] = bisection(f, l, u, tol, max_iter)
for i = 1:max_iter
c = (l + u) / 2;
if f(c) == 0 || abs(u - l) < tol
root = c;
iterations = i;
return;
end
if f(c) * f(l) < 0
u = c;
else
l = c;
end
end
error(’Maximum iterations reached without convergence’);
end
####_Regula_Falsi_####N
a = 0.001;
b = 10000;
tol = 1e-6;
max_iter = 100;
[T_root, iter] = regula_falsi(@f, a, b, tol, max_iter);
fprintf(’Root found: T = %.6f, in %d iterations\n’, T_root, iter);
function y = f(T)
term1 = (6 * 10^6 * exp(-17000 / T)) / (1 + 6 * 10^6 * exp(-17000 / T));
term1 = sqrt(term1);
numerator = 16433 + 36.319 * T + 0.0594 * T^2;
denominator = 11.35 * T + 0.0696 * T^2 - 113844;
term2 = 1 / (1 + numerator / denominator);
y = term1 - term2;
end
function [root, iter] = regula_falsi(f, a, b, tol, max_iter)
iter = 0;
while iter < max_iter
c = (a * f(b) - b * f(a))/ (f(b) - f(a));
if abs(f(c)) < tol
root = c;
return;
end
if f(c) * f(a) < 0
b = c;
else
a = c;
end
iter = iter + 1;
end
root = c;
end
####_Newton_Rhapson_####N
f = @(t) 77*exp(-1.5*t) + 20*exp(-0.08*t) - 0.01;
df = @(t) -115.5*exp(-1.5*t) - 1.6*exp(-0.08*t);
x0 = 1;
tol = 1e-6;
max_iter = 100;
[root, iterations] = newtonRaphson(f, df, x0, tol, max_iter);
fprintf(’\n\nRoot found: %.8f\n’, root);
fprintf(’Number of iterations: %d\n’, iterations);
fprintf(’Function value at root: %.2e\n’, f(root));
function [root, iterations] = newtonRaphson(f, df, x0, tol, max_iter)
x = x0;
for i = 1:max_iter
fx = f(x);
if abs(fx) < tol
root = x;
iterations = i;
return;
end
x = x - fx / df(x);
end
error(’Maximum iterations reached without convergence’);
end
####_CW3_Q1####
clc
clear
close all
T0 = 65;
Qgen = 15000;
Airin = 100;
Airacc = 200;
Cv = 50;
R = 1.99;
Cp = Cv + R;
m = Airin;
M = Airacc;
Qlost = @(t, T) 120*(T-T0) + 8530*t;
f = @(t, T) (m*Cp*(65-T) + Qgen-Qlost(t, T))/(M*Cv);
t0 = 0;
h = 0.1;
tf = 8;
n = (tf-t0)/h;
t = t0:h:tf;
T = zeros(size(t));
T(1) = T0;
for j = 1:n
T(j+1) = T(j) + h*f(t(j), T(j));
end
fprintf("When t = %0.2f, temperature is : %0.4f\n", tf, T(j+1))
% Another Way
T1 = zeros(size(t));
T1(1) = T0;
for k = 1:n
Tp = T1(k)+ h*f(t(k), T1(k));
T1(k+1) = T1(k) + h*f(t(k), T1(k)+f(t(k+1), Tp))/2;
end
fprintf("When t = %0.2f, temperature is : %0.4f\n", tf, T1(k+1))
plot(t, T, ’go-’)
hold on
plot(t, T1, ’r*--’)
####_CW3_Q2_####
clc
clear
close all
k1 = 0.001;
k2 = 0.1;
dydt = @(t, y) [k2*y(3) - k1*y(1)*y(2) k2*y(3) - k1*y(1)*y(2) -2*k2*y(3) + 2*k1*y(1)*y(2)];
a0 = 100;
b0 = 200;
c0 = 0;
t0 = 0;
tm = 10;
h = 0.1;
y0 = [a0 b0 c0];
% y = y0;
%
% Y = [y0];
% T = [t0];
y = [a0 b0 c0];
Y = [a0 b0 c0];
T = [t0];
for t = t0:h:tm
m1 = dydt(t, y);
m2 = dydt(t+0.5*h, y+0.5*m1*h);
m3 = dydt(t+0.5*h, y+0.5*m2*h);
m4 = dydt(t+h, y+m3*h);
y1 = y + h*(m1+2*m2+2*m3+m4)/6;
Y = [Y; y1];
T = [T; t];
y = y1;
end
plot(T, Y)
hold on
dydtt = @(t, y) [k2*y(3) - k1*y(1)*y(2); k2*y(3) - k1*y(1)*y(2); -2*k2*y(3) + 2*k1*y(1)*y(2)];
[TT, YY] = ode45(dydtt, [t0, tm], y0);
plot(TT, YY)
####_CW5_####
clc
clear
close all
f = @(a, g) [a^2 a 1 g];
m = [f(2, 8.57); f(2.5, 10); f(3, 12)];
for i = 1:length(m)-1
m(i, :) = m(i, :)/m(i, i);
for j = 1:length(m)-1
if i ~= j
m(j, :) = m(j, :) - m(i, :)*m(j, i);
end
end
end
r1 = m(:, 4);
ff = @(a, g) [a^2 a a^0.5 g];
m = [ff(2, 8.57); ff(2.5, 10); ff(3, 12)];
for i = 1:length(m)-1
m(i, :) = m(i, :)/m(i, i);
for j = 1:length(m)-1
if i ~= j
m(j, :) = m(j, :) - m(i, :)*m(j, i);
end
end
end
r2 = m(:, 4);
x = 4;
y = 17.2;
g1 = r1(1)*x*x + r1(2)*x + r1(3);
g2 = r2(1)*x*x + r2(2)*x + r2(3)*x^0.5;