This document contains sample MATLAB code to demonstrate various vector, matrix, and function operations. It includes examples of initializing vectors and matrices, performing element-wise operations, matrix multiplication, calculating norms and angles between vectors, generating temperature conversion tables using user inputs, defining a function to find the roots of a quadratic equation, and defining a function to calculate the volume of a spherical tank. Sample code is provided to solve each problem, along with commentary on the results.
1. 1/3Smee
Matlab (TP1) 25%pt
1. Vectors and Matrices Initialize the following variables:
A=[
5 … 5
⋮ ⋱ ⋮
5 … 5
]
9×9
(use ones,zeros) ,B= (A 9x9 matrix of all 0 but with the values [1 2 3 4 5 4 3 2 1] on
diagonal, use zeros, diag) C= (A 3x4 matrix, use NaN) ,D=
2. Define the vector V = [0 1 2 ... 39 40]. What is the size of this vector? Define the vector W containing the first five and the last
five elements of V then define the vector Z = [0 2 4 ... 38 40] from V.
3. Define the matrix What are its dimensions m x n? Extract from this matrix
and .. Make a matrix Q obtained by taking the matrix M intersected between all the 3rd
rows
and one column out of 2. Calculate the matrix products NP = N x P, NtQ = N 'x Q and NQ = N x Q, then comment the results.
4. Define the variable 𝑥 = [
𝜋
6
,
𝜋
4
,
𝜋
3
] and calculate y1=sin(x) and y2=cos(x) Then calculate tan(x) using only the previous y1 and y2.
5. The following vectors are considered:
𝑢 = (
1
2
3
) , 𝑣 = (
−5
2
1
) 𝑎𝑛𝑑 𝑤 = (
−1
−3
7
)
a. Calculate t=u+3v-5w
b. Calculate ‖𝑢‖, ‖𝑣‖, ‖𝑤‖ and the cosine of the angle α formed by the vectors u and v, and α in degrees.
6. Application:This problem requires you to generate temperature conversion tables. Use the following equations, which describe
the relationships between tempera-tures in degrees Fahrenheit(TF) , degrees Celsius(TC), kelvins( TK ) , and degrees Rankine
(TR ) , respectively: 𝑇𝐹 = 𝑇𝑅 − 459.67 𝑜
𝑅, 𝑇𝐹 =
9
5
𝑇𝐶 + 32 𝑜
𝐹, 𝑇𝑅 =
9
5
𝑇𝑘 You will need to rearrange these expressions to solve
some of the problems.
(a) Generate a table of conversions from Fahrenheit to Kelvin for values from 0°F to 200°F. Allow the user to enter the
increments in degrees F between lines. Use disp and fprintf to create a table with a title, column headings, and appropriate
spacing.
(b) Generate a table of conversions from Celsius to Rankine. Allow the user to enter the starting temperature and the increment
between lines. Print 25 lines in the table. Use disp and fprintf to create a table with a title, column headings, and appropriate
spacing.
(c) Generate a table of conversions from Celsius to Fahrenheit. Allow the user to enter the starting temperature, the increment
between lines, and the number of lines for the table. Use disp and fprintf to create a table with a title, column headings, and
appropriate spacing.
7. Defined function Write in Matlab to find roots of quadratic equation 𝑦 = 𝑎𝑥2
+ 𝑏𝑥 + 𝑐 by contained the roots 𝑥 = [𝑥1 𝑥2].
Now, apply your program for the following cases:
a. a=1,b=7,c=12 b. a=1,b=-4,c=4 c. a=1,b=2,c=3
8. Using a function to calculate the volume of liquid (V) in a spherical tank,
given the depth of the liquid (h) and the radius (R).
V=SphereTank(R,h)
Now, apply your program for the following cases:
a. R=2,h=1 b. R=h=5 𝑉 =
𝜋ℎ2(3𝑅−ℎ)
3
2. 2/3Smee
Solution
1.
A=5*ones(9) or A=5.+zeros(5,5)
B. a=[1:5,4:-1:1]
b=diag(a)
c=zeros(9,9)
D=b+c
or D=diag([1:5,4:-1:1])+zeros(9,9)
C=NaN(3,4)
D=([1:10;11:20;21:30;31:40;41:50;51:60;61:70;71:80;81:90;91:100])'
2.
V=[0:40]
size(V)
W=[V(:,1:5) V(:,37:41)]
Z=V(:,[1:2:40])
3.
M=[1:10;11:20;21:30]
size(M)
N=M(:,1:2)
P=M([1 3],[3 7])
Q=M(:,[1:2:10])
NP=N*P
NtQ=N'*Q
NQ=
%Error because both its size is not available for multiple matric
4.
x=[pi/6 pi/4 pi/3]
y1=sin(x)
y2=cos(x)
y=y1./y2
5.
u=[1;2;3]
v=[-5;2;1]
w=[-1;-3;7]
t=u+3*v-5*w
U=norm(u)
V=norm(v)
W=norm(w)
z=dot(u,v);
a=z/[U*V]
b=acosd(a)
6.
a.
incr=input('put the value of the incressing temperature')
F=0:incr:200;
K=(5.*(F+459.67)./9);
table=[F;K];
disp('Conversions from Fahrenheit to kelvin')
disp('In_F conversion to Kelvin')
fprintf('%3.4f %3.4frn',table);
b.
S=input('put the value of the starting temperature')
C=linspace(S,200,25);
R=(C+273.15).*1.8;
table=[C;R];
disp('Conversions from C to Rekin')
3. 3/3Smee
disp('In_C conversion to Rekin')
fprintf('%3.4f %3.4frn',table);
c.
S=input('put the value of the starting temperature')
incr=input('put the value of the incressing temperature')
C=linspace(S,200,incr);
F=1.8.*C+32
table=[C;F];
disp('Conversions from C to F')
disp('In_C conversion to F')
fprintf('%3.4f %3.4frn',table);
7. Defined Function
function x = quadratic(a,b,c)
delta = 4*a*c;
denom = 2*a;
rootdisc = sqrt(b.^2 - delta); % Square root of the discriminant
x1 = (-b + rootdisc)./denom;
x2 = (-b - rootdisc)./denom;
x = [x1 x2];
end
Comment Window
quadratic(1,7,12)
quadratic(1,-4,4)
quadratic(1,2,3)
8.
function volume=SphereTank(radius,depth)
volume= pi*depth.^2*(3.*radius-depth)/3;
end
Comment Window
SphereTank(2,1)
SphereTank(5,5)
______________________________________________________________________________________________________________________
8/13/2017
5:32PM
Corrected by
Mr.Smee Kaem Chann
Tel : +885965235960