matlab sheet 5 - solution functions - ahmed...
TRANSCRIPT
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 1 of 5 Matlab Sheet 5 - Solution
Matlab Sheet 5 - Solution
Functions 1. The output of the MATLAB atan2 function is in radians. Write a function called
atan2d that produces an output in degrees.
M-File: function angle_deg = atan2d(y,x)
% Computes atan2(y,x) in degrees.
angle_rad = atan2(y,x);
angle_deg = angle_rad*180/pi;
In command window >>atan2d(1,0))
ans =
90
>>atan2d(1,-1))
ans =
135
2. Write a function that accepts temperature in degrees Fahrenheit (°F) and
computes the corresponding value in degrees Celsius (°C). The relation between
the two is
𝑇 ℃ =5
9(𝑇 ℉ − 32)
Test your function at 32, 50 and 100.
M-File: function y = temp(x)
% Converts from Fahrenheit to Celsius.
y = (5/9)*(x-32);
In command window >> temp([32,212])
ans =
0 100
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 2 of 5 Matlab Sheet 5 - Solution
3. An object thrown vertically with a speed 𝑣0 reaches a height ℎ at time 𝑡, where
ℎ = 𝑣0𝑡 −1
2𝑔𝑡2
Write and test a function that computes the time 𝑡 required to reach a specified
height ℎ, for a given value of 𝑣0. The function’s inputs should be h, 𝑣0, and 𝑔.
Test your function for the case where ℎ = 100 m, 𝑣0 = 50 m/s, and 𝑔 = 9.81 m/s2.
Interpret both answers.
M-File: function t = time(h,v0,g)
% Computes time t to reach a specified height h, with
initial speed v0.
roots([0.5*g,-v0,h])
In command window >> time(100,50,9.81)
ans =
7.4612
2.7324
The smaller value is the time to reach the height while ascending; the larger
value is the time to reach the height while descending.
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 3 of 5 Matlab Sheet 5 - Solution
Writing and reading from command window and files
4. When a belt is wrapped around a cylinder, the relation between the belt forces
on each side of the cylinder is
𝐹1 = 𝐹2𝑒𝜇𝛽
where 𝛽 is the angle of wrap of the belt in radian and 𝜇 is the friction coefficient.
Write a script file that first prompts a user to specify 𝛽 , 𝜇 , and 𝐹2 and then
computes the force 𝐹1. Test your program with the values 𝛽 = 130°,
𝜇 = 0.3, and 𝐹2 = 100 N. (Hint: Be careful with 𝛽!)
M-File: beta_deg = input(‘Enter a value for beta in degrees:’);
% Convert to radians.
beta = (pi/180)*beta_deg;
mu = input(‘Enter a value for mu:’);
F2 = input(‘Enter a value for force F2:’);
F1 = F2*exp(mu*beta);
disp(‘The force F1 is:’)
disp(F1)
In command window Enter a value for beta in degrees:130
Enter a value for mu:0.3
Enter a value for force F2:100
The force F1 is:
197.5217
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 4 of 5 Matlab Sheet 5 - Solution
5. The intrinsic electrical conductivity 𝜎 of a semiconductor can be approximated
by:
𝜎 = 𝑒(𝐶−
𝐸𝑔
2𝑘𝑇)
Where 𝜎 is measured in (Ω − 𝑚)−1, 𝐸𝑔 is the band gap energy, 𝑘 is Boltzmann’s
constant (8.26 × 10-5 ev/K), and 𝑇 is temperature in kelvins. For Germanium, 𝐶
= 13.83 and 𝐸𝑔 = 0.67 ev. Write a program in a script file that calculates the
intrinsic electrical conductivity for Germanium for various temperatures. The
values of the temperature should be read from an xls spreadsheet using the
xlsread command. The output should be presented as a table where the first
column is the temperature and the second column is the intrinsic electrical
conductivity. Use the following values for temperature: 400, 435, 475, 500, 520,
and 545 K.
M-File: clear, clc
C=13.83; Eg=0.67; k=8.62e-5;
T=xlsread('Germanium_data.xlsx');
sigma=exp(C-Eg./(2*k*T));
tbl=[T sigma];
disp(' Intrinsic')
disp(' Temperature Conductivity')
disp(' deg K (ohm-m)^-1')
fprintf(' %4.0f %5.1f\n',tbl')
Excel File
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 5 of 5 Matlab Sheet 5 - Solution
In Command Window
6. The graph of the function 𝑓(𝑥) = 𝑎𝑥3 + 𝑏𝑥2 + 𝑐𝑥 + 𝑑 passes through the points
(–2.6, –68), (0.5, 5.7), (1.5, 4.9), and (3.5, 88). Determine the constants a, b, c,
and d. (Write a system of four equations with four unknowns, and use MATLAB
to solve the equations.)
M-File: clear, clc
x=[-2.6 0.5 1.5 3.5]; y=[-68; 5.7; 4.9; 88]; power=3:-1:0;
X=[x(1).^power; x(2).^power; x(3).^power; x(4).^power];
coefs=X\y;
fprintf('\nThe equation is f(x)=%.3fx^3 + %.3fx^2 + %.3fx +
%.3f\n',coefs)
In Command Window
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 6 of 5 Matlab Sheet 5 - Solution
7. The pressure drop in Pa for a fluid flowing in a pipe with a sudden increase in
diameter is given by:
∆𝑝 =1
2[1 − (
𝑑
𝐷)
2
]
2
𝜌𝑣2
where 𝜌 is the density of the fluid, 𝑣, the velocity of the flow, and 𝑑 and D are
defined in the figure. Write a program in a script file that calculates the
pressure drop ∆𝑝. When the script file is executed it request the user to input
the density in kg/m3, the velocity in m/s, and values of the non-dimensional
ratio as a vector. The program displays the inputted values of and 𝑣 followed
by a table with the values of in the first column and the corresponding values
of ∆𝑝 in the second column.
Execute the program assuming flow of gasoline (𝜌 = 737 kg/m3) at 𝑣 = 5 m/s
and the following ratios of diameters 𝑑/𝐷 = 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.2.
M-File: clear, clc
rho=input('Please input the fluid density in kg/m^3: ');
v=input('Please input the fluid velocity in m/s: ');
d_ratio=input('Please input the pipe diameter ratio as a vector [x
x x]: ');
delP=0.5*(1-d_ratio.^2).^2*rho*v^2;
fprintf('\nFor gasoline with a density of %.0f kg/m^3 and a flow
',rho)
fprintf('velocity of %.1f m/s\n\n',v)
tbl=[d_ratio;delP];
disp(' delta P')
disp(' d/D (Pa)')
fprintf(' %3.1f %6.1f\n',tbl)
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 7 of 5 Matlab Sheet 5 - Solution
In Command Window
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 8 of 5 Matlab Sheet 5 - Solution
8. A truss is a structure made of members joined at their ends. For the truss shown
in the figure
the forces in the nine members are determined by solving the following system
of nine equations:
𝐹2 + cos(48.81°)𝐹1 = 0
𝐹6 + cos(48.81°)𝐹5 − 𝐹2 = 0
sin(48.81°) 𝐹5 + 𝐹3 = 0
−cos(48.81°)𝐹1 + 𝐹4 = 0
−sin(48.81°) 𝐹1 + 𝐹3 = 1800
−𝐹4 − cos(48.81°)𝐹5 = 1200
−𝐹7 − sin(48.81°)𝐹5 − sin(45°)𝐹9 = 0
sin(45°)𝐹9 = 1500
−cos(45°)𝐹9 + 𝐹8 = 0
Write the equations in matrix form and use MATLAB to determine the forces in
the members. A positive force means tensile force and a negative force means
compressive force. Display the results in a table where the first column displays
the member number and the second column displays the corresponding force.
M-File: clear, clc
T=[cosd(48.81) 1 0 0 0 0 0 0 0
0 -1 0 0 cosd(48.81) 1 0 0 0
0 0 1 0 sind(48.81) 0 0 0 0
-cosd(48.81) 0 0 1 0 0 0 0 0
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 9 of 5 Matlab Sheet 5 - Solution
-sind(48.84) 0 -1 0 0 0 0 0 0
0 0 0 -1 -cosd(48.81) 0 0 0 0
0 0 0 0 -sind(48.81) 0 -1 0 -sind(45)
0 0 0 0 0 0 0 0 sind(45)
0 0 0 0 0 0 0 -1 -cosd(45)];
A=[0; 0; 0; 0; 1800; 1200; 0; 1500; 0];
N=1:9;
F=T\A;
tbl=[N;F'];
disp(' ')
disp(' Member Force')
disp(' No. lbf')
fprintf(' %1i %7.1f\n',tbl)
In Command Window
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 10 of 5 Matlab Sheet 5 - Solution
Interpolation
9. It Interpolation is useful when one or more data points are missing. This
situation often occurs with environmental measurements, such as temperature,
because of the difficulty of making measurements around the clock. The
following table of temperature versus time data is missing readings at 5 and 9
hours. Use linear interpolation with MATLAB to estimate the temperature at
those times.
M-File: Temp = [1:4,6:8,10:12];
Time = [10,9,18,24,21,20,18,15,13,11];
Temp_int = [5,9];
interp1(Temp,Time, Temp_int)
In Command Window
ans =
22.5000 16.5000
Thus the estimated temperatures at 5 P.M. and 9 P.M. are 22.5° and 16.5°.
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 11 of 5 Matlab Sheet 5 - Solution
10. Computer-controlled machines are used to cut and to form metal and other
materials when manufacturing products. These machines often use cubic
splines to specify the path to be cut or the contour of the part to be shaped. The
following coordinates specify the shape of a certain car’s front fender. Fit a
series of cubic splines to the coordinates, and plot the splines along with the
coordinate points.
M-File: x = [0,.25,.75,1.25,1.5,1.75,1.875,2,2.125,2.25];
y = [1.2,1.18,1.1,1,.92,.8,.7,.55,.35,0];
x_int = [0:.01:2.25];
y_int = spline(x,y,x_int);
plot(x,y,’o’,x_int,y_int),xlabel(‘x (ft)’),ylabel(‘y (ft)’)
axis([0 2.25 0 1.5]);
The Plot
0 0.5 1 1.5 20
0.5
1
1.5
x (ft)
y (
ft)
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 12 of 5 Matlab Sheet 5 - Solution
11. A The following data are the measured temperature T of water owing from a hot
water faucet after it is turned on at time t = 0.
a. Plot the data, connecting them first with straight lines and then with a
cubic spline.
b. Estimate the temperature values at the following times, using linear
interpolation and then cubic spline interpolation: t = 0.6, 2.5, 4.7, 8.9.
c. Use both the linear and cubic spline interpolations to estimate the time
it will take for the temperature to equal the following values: T = 75, 85,
90, 105.
Part a
M-File: t = [0:10];
T = [72.5, 78.1, 86.4, 92.3, 110.6, 111.5, 109.3, 110.2,
110.5, 109.9, 110.2];
t_int = [0:.05:10];
T_int = spline(t,T,t_int);
plot(t,T,t,T,’*’,t_int,T_int)
xlabel(‘Time (sec)’),ylabel(‘Temperature (deg F)’)
The Plot
0 1 2 3 4 5 6 7 8 9 10
70
75
80
85
90
95
100
105
110
115
Time (sec)
Tem
pera
ture
(deg F
)
Faculty of Engineering Spring 2017 Mechanical Engineering Department
Dr./ Ahmed Nagib Elmekawy 13 of 5 Matlab Sheet 5 - Solution
Part b Insert the following lines at the end of the script file from Part (a). t_est = [0.6,2.5,4.7,9.8];
T_est_spline = interp1(t,T,t_est,0spline0)
T_est_linear = interp1(t,T,t_est)
The results are T_est_spline = [74.7235, 88.2044, 112.6234, 109.9696] and
T_est_linear
= [75.8600, 89.3500, 111.2300, 110.1400].
Part c
Insert the following code after the ylabel command in part (a).
grid,[tlinear,Tlinear] =
ginput(4),[tspline,Tspline] = ginput(4)
and use the mouse to select the points corresponding to T = 75, 85, 90, and
105.
The results are
tlinear = [0.4493, 1.8088, 2.5922, 3.6982] and
tspline = [0.6336, 1.7396,
2.7995, 3.6751].