4. LAB SESSION 02
Question 01: (Script)
%FAIQ ALI ME-22150
% Find constant k from knowing half-life T
% f(T)=0.5f(0) = f(0)exp(k*T) as half of the element decay with time
% 0.5 = exp(k*T)
% k*T = log(0.5)
T = 5730; % half-life in years
k = log(0.5)/T
% Find age of the sample t
% f(t) = 0.7745f(0) & f(t) = f(0)exp(k*t)
% 0.7745 = exp(k*t) % t
= log(0.7745)/k
t = round(log(0.7745)/k)
Command Window:
5. Question 02: (Script File)
%FAIQ ALI ME-22150
c=300E6; %speed of light
x_p=530E9;
x_s=630E9;
v=c*((1-(x_p/x_s)^2)/((x_p/x_s)^2+1))
Command Window:
6. Question 03: (Script File)
%FAIQ ALI ME-22150
k=401; %thermal conductivity in watts/degree Celsius/meter
L=300; %length in cm
r1=3; %inner radius in cm
r2=5; %outer radius in cm
T1=100; %internal temperature in Celsius
T2=20; %external temperature in Celsius
q=2*pi*L*k*(T1-T2)/log(r2/r1)
Command Window:
12. Question 04: (Script File)
%FAIQ ALI ME-22150
O=[0 0 0];
A=[3 3 -2];
F_1=[-20 10 30];
r_OA=A-O;
Mo=cross(r_OA,F_1);
Magnitude_M=sqrt(sum(Mo.^2));
fprintf('The Magnitude Of Moment ''Mo'' Produced By The Force ''F1''
About Point ''O'' Is %.4f kN-m.n',Magnitude_M)
Command Window:
13. Question 05: (Script File)
%FAIQ ALI ME-22150
mi=[0.5 0.8 0.2 1.1 0.4 0.9];
xi=[-10 -18 -7 5 0 25];
yi=[8 6 11 12 -8 -20];
zi=[32 19 2 -9 -6 8];
X=mi.*xi./mi;
Y=mi.*yi./mi; Z=mi.*zi./mi; fprintf(['The Centre Of Mass OfParticle
''A'' Is At A(%i, %i, %i).n' 'The Centre Of Mass Of Particle ''B'' Is
At B(%i, %.0f,%i).n' 'The Centre Of Mass Of Particle ''C'' Is At C(%i,
%i,%i).n' 'The Centre Of Mass Of Particle ''D'' Is At D(%i, %i,%i).n'
'The Centre Of Mass Of Particle ''E'' Is At E(%i, %i,%.0f).n' 'The
Centre Of Mass Of Particle ''F'' Is At F(%i, %i,%i).n'],X(1),Y(1),Z(1),
X(2),Y(2),Z(2),X(3),Y(3),Z(3),X(4),Y(4),Z(4),X(5),Y(5),Z(5),X(6),Y(6),Z
(6))
X=sum(mi.*xi)./sum(mi);
Y=sum(mi.*yi)./sum(mi); Z=sum(mi.*zi)./sum(mi); disp(' ') fprintf('The
Centre Of Mass Of The Whole System Is Located At G(%.4f, %. 4f,
%.4f).n',X,Y,Z)
14. LAB SESSION 05
Question 01: (Script File)
%FAIQ ALI ME-22150
Running_Speed=8.6 %ft/s;
Swimming_Speed=3.9 %ft/s;
x=[0:5000];
Time=((10000-x)/Running_Speed)+(sqrt(x.^2+3000^2)/Swimming_Speed);
[Minimum_Time Position]=min(Time)
fprintf('The Minimum Distance That Can Be Covered The Person To Reach
the Drowning Child Is %.4f ft.n',x(Position))
Command Window:
16. %FAIQ ALI ME-22150
Vo1=560;
Theta_1=43; Vo2=680; Theta_2=50; g=9.81;
Tf_1=2*Vo1*sind(Theta_1)/g; Tf_2=2*Vo2*sind(Theta_2)/g;
fprintf('The First Projectile Will Hit The Ground First in %.4f Seconds.
n',Tf_1)
t1=linspace(0,Tf_1,11);
t2=linspace(0,Tf_2,11);
Vx1=Vo1*cosd(Theta_1)*t1;
Vy1=Vo1*sind(Theta_1)*Tf_10.5*g*t1.^2;
Vx2=Vo2*cosd(Theta_2)*t2;
Vy2=Vo2*sind(Theta_2)*Tf_1-
0.5*g*t2.^2; r_1=sqrt(Vx1.^2+Vy1.^2);
r_2=sqrt(Vx2.^2+Vy2.^2);
R_AB=r_2-r_1; disp(' ') ;
disp(' Time R_AB')
disp(' ')
disp([t1'R_AB'])
Command Window:
17. Question 02: (Script File)
%FAIQ ALI ME-22150
h=input('Enter Height In Meters : '); g=9.81;
Bounces=input('Enter the number of bounces as an array in increasing order
: ');
Rebound_V=sqrt(2*g*h);
V=0.85*Rebound_V./Bounces;
Time=[1:length(Bounces)];
Time(1)=0;
Time(2:length(Bounces)+1)=V./g; disp(' ')
disp(' Bounce Number Time') disp(' ')
disp([Bounces' Time([1: length(Bounces)])'])
Command Window:
19. Question 02: (Script File)
%FAIQ ALI ME-22150
P=0:200;
Q=1020*sqrt(P).*(1-0.01*sqrt(P));
plot(P,Q)
title('Small Community Firefighting Water Needs')
xlabel('Population,Thousands')
ylabel('WaterDemand,gal/min')
Command Window:
20. LAB SESSION 07
Question 01: (Script File)
%FAIQ ALI ME-22150
clc,clear
for k=1:3
disp('For the Equation ax^2+bx+c') a=input('Enter a: ');
b=input('Enter b: ');
c=input('Enter c: ');
D=b^2-4*a*c;
if D<0
fprintf('n The equation has no real root.nn')
elseif D==0
root=-b/(2*a);
fprintf('n The Equation has One root, n')
fprintf(' %.3fnn',root)
else
r1=(-b+sqrt(D))/(2*a);
r2=(-b-sqrt(D))/(2*a);
fprintf('n The Equation has two roots, n')
fprintf(' %.3f and %.3fnn',r1,r2)
end
end
21. Question 02: (Script File)
%FAIQ ALI ME-22150
for k=1:2
gender=input('Please input your gender (male or female): ','s');
age=input('Please input your age: ');
RHR=input('Please enter your resting heart rate: ');
fit=input('Please enter your fitness level (low, medium, or high:
','s');
gender = lower(gender);
fit = lower(fit);
switch fit
case 'low'
INTEN=0.55;
case 'medium'
INTEN=0.65;
case 'high'
INTEN=0.8;
end
switch gender
case 'male'
THR=((220-age)-RHR)*INTEN+RHR;
case 'female'
THR=((206-0.88*age)-RHR)*INTEN+RHR;
end
fprintf('nThe recommended training heart rate is %.0fnn',THR);
end
Command Window:
22. Question 03: (Script)
%FAIQ ALI ME-22150
for j=1:2
W=input('Please input your weight in lb: ');
h=input('Please input your height in in: ');
BMI=703*W/h^2;
if BMI<18.5
fprintf('nYour BMI value is %.1f, which classifies you as underweightnn',BMI)
elseif BMI<25
fprintf('nYour BMI value is %.1f, which classifies you as normalnn', BMI)
elseif BMI<30
fprintf('nYour BMI value is %.1f, which classifies you as overweightnn',BMI)
else
fprintf('nYour BMI value is %.1f, which classifies you as obesenn', BMI)
end
end
Command Window:
23. Question 04: (Script File)
%FAIQ ALI ME-22150
for j=1:3
n(1:8)=0;
cost=randi([1 5000],1,1)/100;
fprintf('The total charge is $%.2fn',cost)
pay=input('Please enter payment (1, 5, 10, 20, or 50): ');
if pay<cost
fprintf('Insufficient Paymentnn')
continue
else change=pay-cost;
if change>=20
n(1)=1;
change=change-20;
end
if change>=10
n(2)=1;
change=change-10;
end
if change>=5
n(3)=1
change=change-5;
end
while change>=1
n(4)=n(4)+1;
change=change-1;
end
while change>=.25
n(5)=n(5)+1;
change=change-.25
end
while change>=.10
n(6)=n(6)+1;
change=change-.10;
end
if change>=.05;
n(7)=1;
change=change-.05;
end
change+.000001;
25. Question 05: (Script File)
%FAIQ ALI ME-22150
for j=1:3
p=input('Please enter the pressure: ');
old=input('Please enter the units (Pa, psi, atm, or torr): ','s');
new=input('Please enter the desired units (Pa, psi, atm, or torr):
','s');
switch old
case 'Pa'
temp=p;
case 'psi'
temp=6.894757e03*p;
case 'atm'
temp=1.01325e05*p;
case 'torr'
temp=1.333224e02*p;
end
switch new
case 'Pa'
pnew=temp;
case 'psi'
pnew=temp/6.894757e03;
case 'atm'
pnew=temp/1.01325e05;
case 'torr'
pnew=temp/1.333224e02;
end
fprintf('The converted pressure is %.1f%snn',pnew,pnew)
27. Question 06: (Script File)
%FAIQ ALI ME-22150
time=input('Enter the time of the call (day,evening,or night):','s');
duration=input('Enter the duration of the call in minutes: ');
duration=ceil(duration);
rate_day=0.10;
rate_evening=0.04;
rate_night=0.02;
switch time
case 'day'
cost=duration*rate_day;
case'evening'
cost=duration*rate_evening;
case 'night'
cost=duration*rate_night;
otherwise
error('invalid time sepecified');
end
if duration > 10
if duration <=30
cost=cost+1.00+(duration-10)*0.08;
else
cost=cost+1.00+20*0.08+(duration-30)*0.06;
end
end
fprintf('The cost of the call is %.2fn',cost);
Command Window:
28. LAB SESSION 08
Question 01: (Script File)
%FAIQ ALI ME-22150
V = [5, 17, -3, 8, 0, -7, 12, 15, 20, -6, 6, 4, -7, 16];
for i = 1:length(V)
if V(i) > 0 && (mod(V(i), 3) == 0 || mod(V(i), 5) == 0)
V(i) = V(i) * 2;
end
end
j = 1;
while j <= length(V)
if V(j) < 0 && V(j) > -5
V(j) = V(j) ^ 3;
j = j + 1; % Skip to next element
end
j = j + 1;
end disp(V);
Command Window:
29. Question 04: (Script File)
% FAIQ ALI ME-22150
x = 2;
term = 1;
sum = 1; n = 1;
while abs(term) >= 0.0001 && n <= 30
term = term * x / n;
sum = sum + term;
n = n + 1; end
if n > 30 && abs(term) >= 0.0001
disp('More than 30 terms are needed.');
else
disp(['e^', num2str(x), ' = ', num2str(sum)]);
end
Command Window:
30. Question 03 (Script File)
% FAIQ ALI ME-22150
% Input parameters
n = 4; % or n = 6
firstElement = 1;
secondElement = 2;
% Construct the matrix
fibMatrix = zeros(n,n);
fibMatrix(1, 1) = firstElement;
fibMatrix(1, 2) = secondElement;
for i = 2:n for j = 1:n-i+1
fibMatrix(i, j) = fibMatrix(i-1, j) + fibMatrix(i-1,j+1);
end
end
disp(['Matrix of order ', num2str(n), ' with firsttwo elements:'])
num2str(firstElement), ', ', num2str(secondElement)]);
disp(fibMatrix);
% Verify determinant is zero
determinant = det(fibMatrix);
disp(['Determinant of the matrix: ', num2str(determinant)]);
Command Window
31. Question 04: (Script File)
%FAIQ ALI ME-22150
m_values = [5, 10, 20];
for m = m_values
sum = 0;
n = 0;
while true
term = (-1/3)^n / (2*n + 1);
sum = sum + term;
n = n + 1;
if abs(term) < 1e-15
break;
end
end
% Display the result and compare with ?
disp(['For m = ', num2str(m)]);
disp(['Result: ', num2str(sqrt(12)*sum)]);
disp(['Comparison with pi: ', num2str(pi)]);
disp(' ');
end
Command Window
32. Question 05: (Script File)
%FAIQ ALI ME-22150
m_values = [100, 100000, 1000000];
for m = m_values
product = 1;
n = 1; while true
term = (2^(2*n))/(2^(2*n) - 1);
product = product * term;
n = n + 1;
if n > m
break;
end
end
disp(['For m = ', num2str(m)]);
disp(['Result: ', num2str(2*product)]);
disp(['Comparison with pi: ', num2str(pi)]);
disp('')
end
33. Question 06: (Script File)
%FAIQ ALI ME-22150
scores = [73, 91, 37, 81, 63, 66, 50, 90, 75, 43, 88, 80, 79, 69, 26,
82, 89, 99, 71, 59];
sorted_scores = sort(scores, 'descend');
top_scores = sorted_scores(1:8);
average = mean(top_scores); %
Display the average
disp(['Average of the top 8 scores: ', num2str(average)]);
Command Window
34. Question 07: (Script File)
%FAIQ ALI ME-22150
x_degrees = input('Enter the angle in degrees: ');
x_radians = deg2rad(x_degrees);
term = x_radians;
sum = term;
n = 1;
estimated_error = abs(term/sum);
while estimated_error > 1e-5
term = (-1)^n * (x_radians^(2*n+1)) / factorial(2*n +1);
sum = sum + term;
n = n + 1;
estimated_error = abs(term / sum);
end
disp(['sin(', num2str(x_degrees), ') = ', num2str(sum)]);
disp(['Comparison with calculator: ',num2str(sin(x_radians))]);
Command Window
35. LAB SESSION 09
Question 01: (Script)
% FAIQ ALI ME-22150
function [Smax,Smin] = princstress(Sxx, Syy, Sxy) %Calculates
princstress, Pricipal Stress, using the 3 stress components
Smax=((Sxx+Syy)/2)+(sqrt(((Sxx-Syy)/2)^2+(Sxy^2)));
Smin=((Sxx+Syy)/2)-(sqrt(((Sxx-Syy)/2)^2+(Sxy^2)));
fprintf('Maximum Principal Stress: %.4fn', Smax);
fprintf('Minimum Principal Stress: %.4fn', Smin);
end
Command Window:
a) Maximum Principal Stress: 177.8902
Minimum Principal Stress: -222.8902
ans = 177.8902
b) Maximum Principal Stress: 16.0605
Minimum Principal Stress: -17.0605
ans = 16.0605
36. Question 02: (Script)
%FAIQ ALI ME-22150
function ellipseplot(xc,yc,a,b)
%ellipseplot calculates and plots the ellipse with xc center
and yc center
theta = linspace(0, 2*pi, 100);
x = xc + a * cos(theta);
y = yc + b * sin(theta); plot(x, y);
xlabel('x');
ylabel('y');
title('Ellipse Plot');
end
Command Window:
37. Question 03: (Script)
%FAIQ ALI ME-22150
function [HI] = HeatIn(T,R)
%HeatIn calcualtes Heat index using Temperature and R
humidity
C1=-41.379;
C2=2.04901523;
C3=10.14333127;
C4=-0.22475541;
C5=-6.83783*10^-3;
C6=-5.481717*10^-2;
C7=1.22874*10^-3;
C8=8.5282*10^-4;
C9=-1.99*10^6;
HIx=C1+C2*T+C3*R+C4*T*R+C5*(T^2)+C6*(R^2)+C7*(T^2)*R+C8*T*( R^ 2
)+C9*(T^2)*(R^2);
HI=round(HIx);
end
Command Window:
38. Question 04: (Script)
%FAIQ ALI ME-22150
function [y] = fifthroot(P)
% Function calculates the fifth root of the number
P i = 1;
E = 1;
X(1) = P;
while E > 0.00001
i = i + 1;
X(i) = X(i-1) - ((X(i-1)^5 - P) / (5*(X(i-1))^4));
E = abs((X(i) - X(i-1)) / X(i-1)); e
end
y = X(i);
end
Command Window:
43. Question 05: (Script)
%FAIQ ALI ME-22150
% Find the cube root of 13,440
cube_root = nthroot(13440, 3);
% Round the cube root to the nearest integer
integer_root = round(cube_root);
% Calculate the four consecutive integers
integers = integer_root - 1 : integer_root + 2;
% Display the consecutive integers
disp(integers);
COMMAND WINDOW:
44. LAB SESSION 10
Question 01: (Script)
% FAIQ ALI ME-22150
T = [-20, 0, 20, 40, 60, 80, 100, 120];
d = [4, 0.38, 0.095, 0.032, 0.015, 0.0078, 0.0045, 0.0032];
% Fit a quadratic equation using polyfit
pf=polyfit(T,log(d),4);
% Estimate viscosity at 150 degrees Celsius
T_150=exp(polyval(pf,150));
% Plotting
x_cal=-20:.01:120;
y_cal=exp(polyval(pf,x_cal));
plot(T,d,'o',x_cal,y_cal,'r-', 'LineWidth', 1.5)
xlabel('Temperature (K)');
ylabel('Viscosity (Ns/m^2)');
title('Viscosity of Oil');
legend('Data Points', 'Fitted Equation');
grid on
fprintf('Estimated viscosity at 150 °C: %.4f *10Nsm^2n',T_150)
COMMAND WINDOW:
45. Question 02: (Script)
h_ft = [-1000, 0, 3000, 8000, 15000, 22000, 28000]; % Altitude in feet
T_F = [213.9, 212, 206.2, 196.2, 184.4, 172.6, 163.1]; % Boiling
temperature in Fahrenheit
% Convert altitude from feet to meters
h_m = h_ft * 0.3048;
% Fit a linear equation using polyfit
coeff = polyfit(h_m, T_F,2);
% Calculate boiling temperature at 5,000 m
h_5000 = 5000;
T_5000_F = polyval(coeff,h_5000);
% Plotting
figure;
h_plot = linspace(min(h_m), max(h_m), 100);
T_plot = polyval(coeff,h_plot);
plot(h_m, T_F, 'bo',h_plot, T_plot, 'r-', 'LineWidth', 1.5); % Data
points
xlabel('Altitude (m)');
ylabel('Boiling Temperature (°C)');
title('Boiling Temperature of Water at Different Altitudes');
legend('Data Points', 'Fitted Equation');
fprintf('Boiling temperature at 5,000 m is %.2f °Fn',T_5000_F);
COMMAND WINDOW:
46. Question 03: (Script)
%FAIQ ALI ME-22150
t = [0, 1, 3, 4, 6, 7, 9]; % Time in hours
NB = [500, 600, 1000, 1400, 2100, 2700, 4100];
% Fit an exponential function using the polyfit
coeff = polyfit(t, log(NB),6);
% Estimate the number of bacteria after 5 hours
t_5 = 5;
NB_5 = exp(polyval(coeff,t_5));
% Plotting
t_plot = linspace(min(t), max(t), 1000);
NB_plot = exp(polyval(coeff,t_plot));
plot(t, NB, 'bo',t_plot, NB_plot, 'r-', 'LineWidth',1.5);
xlabel('Time (hours)');
ylabel('Number of Bacteria');
title('Growth of Bacteria over Time'); legend('Data
Points', 'Fitted Equation');
% Display estimated number of bacteria after 5 hours
fprintf('Estimated number of bacteria after 5 hours: %.2fn', NB_5);
COMMAND WINDOW: