Aim - CSE B

advertisement
MATLAB RECORD
BATCH: 2014 -- 2018
Programming using MATLAB
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SRM UNIVERSITY
SRM Nagar, Kattankulathur – 603 203
Kancheepuram (Tamilnadu)
Basic Matlab Commands
Ex.No: 1
Date:
Aim:
Commands:
•
Help
Purpose = to get the list of help topics
•
Clock
Purpose = to show date in vector form
Ans =
1.0e+003*
2.0130
•
0.0070
0.0290
0.0140
0.0330
Date
Purpose = to show date in string form
Ans=
29-Oct-2014
•
Ver
Purpose = to show current version of matlab
Ans =
MATLAB Version 7.7.0.471 (R2008b)
•
PWD
Purpose = to show the present working directory
Ans =
C:\Documents and Settings\User\My Documents\MATLAB\Tushar
•
CD
Purpose = to change the directory
0.0184
•
Dir
Purpose = to display the name of all files and folder in the present directory
Ans =
Explore the basics of matlab.mat
•
ls
Purpose = to display the name of all files and folder in the current directory
Ans =
Explore the basics of matlab.mat
•
path
Purpose = to show the path of the present directory
•
edit path
Purpose = to edit the current path directory
•
mkdir
Purpose = to create a new directory
•
Who
Purpose = to list all the variables in workspace
Ans =
Your variables are:
Ans
•
Whos
Purpose = to display the information of all the variables
Ans =
•
Name
Size
Bytes
Class
Ans
1X58
116
char
What
Purpose = to display m-file, mex-file and mat-file.
Ans =
Attributes
MAT-files in the current directory C:\Documents and Settings\User\My
Documents\MATLAB\Tushar
Explore the basics of matlab
•
Clear all
Purpose = to clear all the commands in the workspace
•
Clc
Purpose = to clear the screen
•
Clf
Purpose = to clear the figure window
•
Ctrl+C
Purpose = to termination the execution
•
Quit
Purpose = to exit the matlab
•
Exit
Purpose = to exit the matlab
Result:
Evaluating the Expressions
Ex.No.02
Date:
Aim:
1. x3+5x2-2.67x-52 , when x=13.5
2. ((14x3)1/2)/e3x , when x=13.5
3. log|x2-x3| , when x=13.5
4. [37 log(76)/(73+546)] + (910)1/3
5. Prove LHS=RHS
when x=∏/5
a. Cos2(x/2) = tanx+sinx/2tanx
Solution:
•
x3+5x2-2.67x-52
>>x=13.5
>>x^3+5*(x^2)-2.67*x-52
Output = 3.2836e+003
•
((14x3)1/2)/e3x
>>x=13.5
>>(sqrt(14*(x^3)))/exp(3*x)
Output = 4.7823e-016
•
log|x2-y3|
>>x=13.5
>>log(abs((x^2)-(x^3)))
Output = 7.7311
•
>>[(37 log(76))/(73+546)] + (910)1/3
(((3^7)*log(76))/((7^3)+546))+(nthroot(910,3))
Output = 20.3444
•
Prove LHS=RHS
Cos2(x/2) = tanx+sinx/2tanx
LHS
>>Cos2(x/2)
(Cos(x/2))^2
Output = 0.9045
RHS
>>tanx+sinx/2tanx
(tan(x)+sin(x))/2*tan(x))
Output = 0.9045
Thus, LHS=RHS
Result:
when x=∏/5
Programming using control structures
Ex.No. 03
Date:
Aim:
Q1) Display the grade of a student using if-else statement
a>80 - Distinction
a< 80 and a> 60 - First Class
a< 60 and a> 45 - Second Class
a< 45 - Fail
Q2) Find if the given number is odd or even using if-else structure.
Q3) To find the body mass index (BMI) by using height and weight.
BMI = weight/(height)2
Weight (KG)
Height (Meters)
BMI <18.5 = under weight
BMI > 18.5 and <24.9 = normal
BMI > 25 and <29.9 = overweight
BMI >30 = obese
Solution:
Ans 1)
Code:
disp (‘Display the grade of the student’);
a= input (‘Enter Marks :’);
if a>=80
disp (‘Distinction’);
elseif (a>=60 && a<80)
disp (‘First Class’);
elseif (a>=45 && a<60’)
disp (‘Second Class’);
else
disp (‘Fail’);
end;
Output:
Display the grade of the student
Enter Marks : 70
First Class
Display the grade of the student
Enter Marks : 39
Fail
Ans 2)
Code:
disp (‘Check whether the inputted number is odd or even’);
a = input (‘Enter a number :’);
if (mod(a,2)==0)
disp (‘Even’);
else
disp (‘Odd’);
end;
Output:
Enter a number : 56’
Even
Enter a number : 3
Odd
Ans 3)
Code:
disp (‘Calculating BMI’);
w = input (‘Enter weight’);
h = input (‘Enter height’);
r=w/(h^2);
disp (‘The BMI is :’);
if (r<18.5)
disp (‘Underweight’);
elseif (r > 18.5 && r < 24.9)
disp (‘Normal’);
elseif (r > 25 && r < 29.5)
disp (‘Overweight’);
else
disp (‘Obese’);
end;
Output:
Calculating BMI
Enter weight : 50
Enter height : 1.5
The BMI is :
22.222
Normal
Calculating BMI
Enter weight : 75
Enter height : 1.1
The BMI is :
61.9835
Obese
Result:
For, While and Switch statements
Ex.No. 04
Date:
Aim:
Q1) Find the sum of ‘N’ natural numbers using FOR loop.
Q2) Find the sum of odd numbers using WHILE loop.
Q3) implement string functions such as length and compare to upper and lower case using switch case.
Solution:
Ans 1)
Code:
sum = 0;
for i=1:1:35
sum=sum+i;
end;
fprintf (‘The sum of the first 35 natural number;%d’,Sum);
Output:
The sum of the first 35 natural number; 630
Ans 2)
Code:
i=1;
while i<100
sum=sum+i;
i=i+2;
end;
fprintf (‘The sum of odd natural numbers upto 100 is;%d’,Sum);
Output:
The sum of odd natural numbers upto 100 is; 13130
Ans 3)
Code:
a = ‘HELLO’;
b = 'HELLO’;
disp('1 for length'/n'2 to compare'/n'3 to apply lower case'/n'4 to apply upper case');
ch = input (‘Enter any number : ‘);
switchch
case 1
disp (length(a));
disp (length(b));
break;
case 2
disp (strcmp(a,b));
break;
case 3
disp (lower(a));
disp (lower(b));
break;
case 4
disp (upper(a));
disp (upper(b));
break;
otherwise
disp (‘Wrong Choice’);
end;
Output:
Enter any number : 2
1
Enter any number : 1
5
5
Enter any number : 4
HELLO
HELLO
Enter any number : 3
hello
hello
Result:
File Operations
Ex.No. 05
Date:
Aim:
Q1) Open a text file in MAT lab.
Q2) Writing information ‘WELCOME’ into a file and read that file.
Q3) Copy and close the file.
Solutions:
Ans 1)
Code:
fid = fopen (‘csei.txt’, ‘w’)
Output:
Fid =
3
Ans 2)
Code:
fid = fopen (‘csei.txt’, ‘w’)
fprintf (3, ’%S’, ‘WELCOME’)
fscanf (3, ‘%S’)
Output:
fid =
3
Ans =
7
Ans =
WELCOME
Ans 3)
Code:
copyfile ‘csei.txt’ ‘Tush.txt’
status = fclose (3)
fid = fopen (‘Tush.txt’, ‘r’)
fscanf (3, ‘%S’)
Output:
status =
0
fid =
3
ans =
WELCOME
Result:
Matrix Arithmetic Operations
Ex.No. 06
Date:
Aim:
1) Perform the arithmetic operations for the given 3X3 matrix
A+B, A-B, A*B, det(A), det(B)
A=
3
11
6
4
7
10
13 9
0
B=
4
6
8
3
5
7
6
9
12
2) A =
1 2 3 4 5 6
7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
i.
Form a matrix B from the first, third and fourth rows and the first, third, fourth and seventh column of
the matrix A.
ii.
Create a matrix C with 17 elements long row vector from matrix A from the elements of the third
row, the fifth & seventh column.
Solutions:
Ans 1)
Code:
>>A = [ 3 11 6 ; 4 7 10 ; 13 9 0 ]
>>B = [ 4 6 8 ; 3 5 7 ; 6 9 12 ]
>>C = A+B
>>D = A-B
>>E = A*B
>>F = det (A)
>>G = det (B)
Output:
A=
3 11
6
4 7
10
13 9
0
B=
4 6 8
3 5 7
6 9 12
C=
7 17
14
7 12 17
19 18 12
D=
-1 5 -2
1 2 -3
7 0 -12
E=
81
97
79
F = 830
G=0
127
149
123
173
201
167
Ans 2)
Code:
A = [ 1:7 ; 2:2:14 ; 21:-3:3 ; 5:5:35 ]
a = A (1, ;)
b = A (3:4, :)
c = [a;b]
d = c (:,1)
e = c (:,3:4)
f = c (:,7)
B=[def]
g = A (3,:)
h = A (:,5)
I = A (:,7)
h=h’
i=I’
U = [ g h i]
Output:
A=
a=
1 2 3 4 5 6 7
b=
21
5
18 15 12 9 6 3
10 15 20 25 30 35
c=
21 18 15 12 9 6 3
5 10 15 20 25 30 35
d=
1
21
5
e=
3 4
15 12
15 20
f=7
3
35
B=
1 3 4 7
21 15 12 3
5 15 20 25
g = 21 18 15 12 9 6 3
h= 5
10
9
25
I=7
14
3
35
h = 5 10 9 25
I = 7 14 3 35
U = 21 18 15 12 9 6 3 5 10 9 25 7 143 35
Result:
Rule of matrix
Ex.No 7
Date:
Aim:
From the matrix A,B and C
A=
5 2 4
1 7 -3
6 -10 0
B=
11 5 -3
0 -12 4
2 6 1
C=
7 14 1
10 3 -2
8 -5 9
Q1) Calculate A+B and B+A, to show that addition of matrix is commutative.
Q2) Calculate A(A+B) and (A+B)+C, to show that addition of matrix is associative.
Q3) Calculate A*(B+C) and A*B+A*C, to show that multiplication of matrix is multiplication.
Solutions:
A = [ 5 2 4 ; 1 7 -3 ; 6 -10 0]
B = [ 11 5 -3 ; 0 -12 4 ; 2 6 1]
C = [ 7 14 1 ; 10 3 -2 ; 8 -5 9]
Ans 1)
Code:
X=A+B
Y=B+A
Output:
X=
Y=
Hence, A+B = B+A
Ans 2)
Code:
X=A+(B+C)
X=A+(B+C)
Output:
X=
Y=
Hence, A(A+B) = (A+B)+C
Ans 3)
Code:
X=A*(B+C)
Y=A*B+A*C
Output:
X=
Y=
Hence, A*(B+C) = A*B+A*C
Result:
Polynomial evaluation
Ex.No. 8
Date:
Aim:
Q1) f(x) = x5-12.1 x4+40.59 x3-17.015 x2-71.95 x+35.88, Calculate f(9) and roots of this equation.
Q2) Perform the polynomial multiplication and addition for the given two polynomial
f1(x) = 3 x6+15 x4-10 x3-3 x2+15 x-40
f2(x) = 3 x3-2 x-6
Q3) Divide the polynomial 4 x4+6 x3-2 x2-5 x-3 by x2+4x+2
Solutions:
Ans 1)
Code:
>>P = [ 1 -12.1 40.59
>>val = polyval (P,9)
>>r = roots (P)
-17.015
-71.95
35.88 ];
Output:
P=
1.0000 -12.1000
val =
7.2611e+003
r=
6.5000
4.0000
2.3000
-1.2000
0.5000
Ans 2)
Code:
>>P = [ 3 15 -10
>>Q = [ 0 0
0
>>P+Q
>>M = conv (P,Q)
40.5900
-3 15 -40 ]
3 -2
-6 ]
Output:
P=
3 15 -10
Q=
0
0
0
ans =
3 15 -10
-17.0150
-3
15
-40
3
-2
-6
0
13
-46
-71.9500
35.8800
M=
0
0
0
9
39
-78
-79
111
-132
-10
Ans 3)
Code:
>>P = [ 4
6 -2
-5
>>Q = [ 1
4 2]
>>[D,R] = deconv (P,Q
3]
Output:
P=
4
Q=
1
D=
4
R=
0
6
-2
4
2
-10
0
-5
3
30
0
-105
-57
Result:
2D plots
240
Ex.No.09
Date:
Aim:
Year
2008
Placement 65
2009
2010
2011
2012
72
74
76
84
Q1) Plot the graph for the table with dashed dotted red lines, marker type diamond, marker edge color
green, marker size = 12 and label the graph along with grid and box. Also plot the bar chart and pie diagram
for the data.
Solutions:
Ans 1)
Code:
>>year = [ 2008,2009,2010,2011,2012 ]
>>placement = [ 65,72,74,76,84 ]
>>plot (year,placement,'rd-','markeredgecolor','g','markersize',12);
>>title('2D PLOTS and PRINTING LABELS');
>>box on;
>>grid on;
Output:
Result:
Bar chart and pie chart
Exp.No.10
Date:
Aim:
Q1) Write a matlab program to get 5 subjects for 5 members of student and calculate the average and draw
the bar chart and pie chart for individual student’s performance.
Solution:
Ans 1)
Code:
S1 = [ 30 35 48 36 40 ];
S2 = [ 32 33 25 12 27 ];
S3 = [ 4 0 16 5 1 ];
S4 = [ 47 49 48 45 50 ];
S5 = [ 27 18 39 34 30 ];
a = sum(S1)/5;
b = sum(S2)/5;
c = sum(S3)/5;
d = sum(S4)/5;
e = sum(S5)/5;
S = [ a b c d e ];
bar (S,'y');
pie (S);
Output:
Result:
Mini Project
Title:
Objectives:
To Build a system to enter student’s details for name, date of birth, marks for five subjects, total and
average.
i. You have to produce matrix for the above data
ii. Generate the report card for individuals using the above details
iii. Produce the 3D graph for results
Expected outcomes:
Algorithms Used:
Procedure:
Output / graphs:
Result(s):
References:
Download