0% found this document useful (0 votes)
128 views

Lab 2

This document discusses solving linear equations and operations on matrices in MATLAB. It contains examples of using MATLAB to find the volume of a cylinder, calculate trigonometric functions as rational numbers, solve sets of linear equations, and add and remove rows and columns from matrices. Various MATLAB commands are also explained, including inverse, eye, magic, and rank.

Uploaded by

Kulsoom Jahan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Lab 2

This document discusses solving linear equations and operations on matrices in MATLAB. It contains examples of using MATLAB to find the volume of a cylinder, calculate trigonometric functions as rational numbers, solve sets of linear equations, and add and remove rows and columns from matrices. Various MATLAB commands are also explained, including inverse, eye, magic, and rank.

Uploaded by

Kulsoom Jahan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB NO # 02

Question 1
If y has not been assigned a value, Will MATLAB allow you to define the equation x = y ^2 to store in memory for later use?
1. >> x=y^2 ANS: No! MATLAB will not display the value of x until or unless we give the value to y.

2. If the volume of a cylinder of height h and radius r is given by V = r2h, use MATLAB to find the volume enclosed by a cylinder that is 12 cm high with a diameter of 4 cm? ANS:
>>r=2; >> h=12; >> v=pi*r^2*h v= 150.7964

3. Display the results of sin (/4), sin (/3), and sin (/2) as rational numbers. Also consider the phase as degrees? ANS:>>x1=sind (45) x1= 43/3137 >>x2=sin (pi/4) x2= 43/3137 >>y1=sind (60) y1 = 194/10615 >>y2=sin (pi/3) y2 = 194/10615 >>z1=sind (90) z1= 202/7369 >>z2=sin (pi/2)

Page 1 of 7

LAB NO # 02 z2= 202/7369

4. Compute ex for a few values of x. You must take minimum 20 values and take the input as vectors. ANS:>>t1=[1:20]
t1= Columns 1 through 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Columns 16 through 20 16 17 18 19 20 >> t=exp(t1) t= 1.0e+008 * Columns 1 through 9 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 Columns 10 through 18 0.0002 0.0006 0.0016 0.0044 0.0120 0.0327 0.0889 0.2415 0.6566 Columns 19 through 20 1.7848 4.8517

Question 2:
Solve the following set of linear equations; also find the determinant of each of the following? 3x 2y= 5 6x 2y= 2
>>a1=[3 2;6 2]; >> b1=[5;2]; >> c1=inv(a1); >> I1=c1*b1' I1= -1.0000 4.0000

x 2y 3z = 1 x 4y 3z = 2 2x 8y z = 3
Page 2 of 7

LAB NO # 02 >>a2=[1 2 3;1 4 3;2 8 1]; >> b2=[1;2;3]; >> c2=inv(a); >> I2=c2*b2 I2 = -0.6000 0.5000 0.2000

x 7y 9z = 12 x y 4z = 16 x y 7z = 16
>>a3=[1 7 9;2 1 4;1 1 7]; >> b3=[12;16;16]; >> c3=inv(a); >> I3=c3*b3' I3 = 5.1724 -1.2414 1.7241

5x 2y 9z =44 9x 2y 2z =11 6x 7y 3z =44


>>a4=[5 2 9;9 2 2;6 7 3]; >> b4=[44;11;44]; >> c4=inv(a); >> I4=c4*b4 I4= -0.8766 5.2314 4.2134

x 2y 3z = 12 4x y 2z = 13 9y 8z = 1
>>a5=[1 2 3;4 2;0 9 8]; >>b5=[12;13;1];

Page 3 of 7

LAB NO # 02 >>c5=inv(a5);

The inverse of this matrix is not possible. Then we cant find the values of variables x, y and z . 2w + 4x - 3y + 29z =20 12w + 8y + 2z =10 -5w + x + 10z =40 w +x + y +z =20
>>a6= [2 4 -3 29; 12 0 8 2;-5 1 0 10; 1 1 1 1]; >>b6= [20; 10; 40; 20]; >>c6=inv (a6);

The inverse of this matrix is not possible. Then we cant find the values of variables w, x, y and z. x 2y 3z = 12 4x y 2z = 13 9y 8z = 1
>>a7= [1 2 3; 4 1 2; 0 9 8]; >>b7= [12; 13; 1]; >>c7=inv (a7);

The inverse of this matrix is not possible. Then we cant find the values of variables x, y and z. 3x 2y z =07 4y z =02
>>a8= [3 2 1;0 4 1]; >>b8= [7; 2]; >>c8=inv (a8);

The inverse of this matrix is not possible. Then we cant find the values of variables x, y and z.

Question 3:
Page 4 of 7

LAB NO # 02

Discuss the different format types used in MATLAB. Please write in your own words and it must not exceed more than 5-lines?
Format function is use to control the output format of the numeric values displayed in the Command Window. The format function affects only how numbers are displayed, not how MATLAB computes or saves them. The specified format applies only to the current session. It changes the output format to the default type, short, which is a 5-digit scaled, fixed-point value.

Syntax:
format type format('type')

Question 4:
Explain the purpose of following commands?
Diary, eye, magic, rank Diary is used to save the whole task done in matlab. EYE command is use to create the identity matrix of different order e.g.

T=eye (2) T= 1 0 0 1

MAGIC command returns a matrix constructed from the integers 1 through n^2 with equal row and column sums. The order must be a scalar greater than or equal to 3 e.g. T2=magi(3) T2= 2 9 6 8 4 5 7 4 6

Page 5 of 7

LAB NO # 02 Rank determines the number of non-zeros rows or columns in echelon form of any matrix.

Syntax:
k = rank(A) k = rank(A, tol)

Question 5:
Explain with examples how to add rows and columns in a defined matrix?
>>A=[2 2 1; 0 4 0; 1 8 0] A= 2 0 1 2 4 8 1 0 0

B=[1 2 1;0 8 1;0 4 0] B= 1 0 0 2 8 4 1 1 0

>>A1= A(3,:); >> C=(B; A1] C= 1 2 1 0 8 0 4 1 0 0

1 8

>>B1=B(3, :); Page 6 of 7

LAB NO # 02 D=[A1,B1] D= 1 8 0 0 4 0

Page 7 of 7

You might also like