Matlab File
Matlab File
Matlab File
>> new
A=
1 2 3
4 5 6
7 8 9
2) rand(3)
>> new
ans =
3) rand(2,3)
>> new
ans =
4) a=rand(4)
b=rand(4)
c=a+b
>> new
a=
b=
c=
5) d=rand(4,1)
e=a*d
>> new
d=
0.6991
0.8909
0.9593
0.5472
e=
1.2241
1.3358
0.9488
1.8926
6) a=rand(3,2)
a'
>> new
a=
0.1386 0.8407
0.1493 0.2543
0.2575 0.8143
ans =
7) a=3
b=a*3
eye(3)
>> new
a=
b=
ans =
1 0 0
0 1 0
0 0 1
8) [eye(3),diag(eye(3)),rand(3,2)
>> new
ans =
9) A=rand(3)
>> new
A=
B=
0.4733 0.5853 0.2858 0 0
0 0 0 1.0000 1.0000
0 0 0 1.0000 1.0000
10) x=0:pi/2:2*pi
b=sin(x)
[x' b' ]
>> new
x=
b=
ans =
0 0
1.5708 1.0000
3.1416 0.0000
4.7124 -1.0000
6.2832 -0.0000
11) a=rand(3,4)
a(2,3)
a(1:2,2:3)
a(1,end)
a(1,:)
a(:,3)
>>
>> new
a=
ans =
0.1299
ans =
0.0540 0.9340
0.5308 0.1299
ans =
0.4694
ans =
ans =
0.9340
0.1299
0.5688
a(7)
a([1 2 3 4])
a(:)
>> new
a=
1 2 3
4 5 6
9 8 7
ans =
ans =
ans =
1 4 9 2
ans =
6
7
>> new
x=
x=
2.5000
x=
1.2500
x=
0.6250
>> new
x=
1
x=
1 4
x=
1 4 9
x=
1 4 9 16
>> new
x=
16
x=
16 9
x=
16 9 4
x=
16 9 4 1
16) 3<5,3>5,3==5
>> new
ans =
logical
1
ans =
logical
ans =
logical
>> new
a=
b=
0 0 0 0.0782 0.7749
0 0 0 0 0.8173
ans =
5×5 logical array
1 1 1 1 1
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
Logical Indexing
18)
a>2
>> new
a=
ans =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
19) a(ans)=20
>> new
a=
>> new
a=
20)
x = 0:pi/6:.5*pi;
y = sin(x)
>> new
y=
a=rand(3,4)
b=sin(a)
c=sqrt(b)
>> new
a=
b=
c=
Matrix Functions
22)
A=rand(3)
y=eig(A)
>> new
A=
y=
1.4447 + 0.0000i
-0.0835 + 0.0407i
-0.0835 - 0.0407i
23)
[V,D]=eig(A)
>> new
V=
D=
acceleration = 4.5;
time = 25;
+ acceleration * time
final velocity =
112.5000
x = 7 + 10/3 + 5 ^ 1.2
x=
17.231981640639408
x = 7 + 10/3 + 5 ^ 1.2
x=
17.2320
weekly wage =
1064.70
4.678 * 4.9
ans =
2.2922e+01
x = pi
x=
3.141592653589793e+00
4.678 * 4.9
Ans =
2063/90
weekly wage =
1064.70
format short e
4.678 * 4.9
Ans =
2.2922e+01
Creating vectors
>> r = [7 8 9 10 11]
r=
7 8 9 10 11
>> r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
res =
9 11 13 15 17
c=
10
11
>> m = [1 2 3; 4 5 6; 7 8 9]
m=
1 2 3
4 5 6
7 8 9
a = 5; b = 7;
c=a+b
d = c + sin(b)
e=5*d
f = exp(-d)
c=
12
d=
1.2657e+01
e=
6.3285e+01
f=
3.1852e-06
Q1) write a 3x3 cell array and call all the data type
A = ones (4)
B = 'this is a string'
C = rand(4)
D = [1 3 4 5]
E = 'my name is neeraj'
F = [2 4 5; 4 4 4; 5 5 4]
H = rand(3)
I = [3; 3; 4; 4]
cell = {A, B, C; D, E, F; G, H, I}
cell{1,1}
cell{1,2}
cell{1,3}
cell{2,1}
cell{2,2}
cell{2,3}
cell{3,1}
cell{3,2}
cell{3,3}
q2) Write a structure array in matlab and call all the datatype
student(1).name= 'neeraj'
student(2).rolnum= 23
student(3).precentage= 84.44
student
student(1).name
student(2).rolnum
student(3).precentage
>> new
student =
name: 'neeraj'
student =
name
rolnum
student =
name
rolnum
precentage
student =
name
rolnum
precentage
ans =
'neeraj'
ans =
23
ans =
84.4400
>>
paitent(1).name= 'rahul'
paitent(3).age= 43
paitent(4).type= 'flue'
paitent
paitent(1).name
paitent(2).blood_group
paitent(3).age
paitent(4).type
>> new
paitent =
paitent =
name
blood_group
paitent =
name
blood_group
age
paitent =
name
blood_group
age
type
paitent =
name
blood_group
age
type
ans =
'rahul'
ans =
'B positive'
ans =
43
ans =
'flue'
>>
q3) write a matrix in matlab and chack the condition
a = rand(5), b = triu(a), a == b
ans)
a=
b=
0 0 0 0.8555 0.4820
0 0 0 0 0.1206
ans =
1 1 1 1 1
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
>>
2D Linear Plots
A script file is an external file that contains a sequence of MATLAB statements. Script
files have a filename extension .m and are often called M-files. M-files can be scripts that
simply execute a series of MATLAB statements, or they can be functions that can accept
Examples
Example 1
Solution:
example1
A = [1 2 3; 3 3 4; 2 3 3];
b = [1; 1; 2];
x = A\b
x=
-0.5000
1.5000
-0.5000
Example = factorial(5)
f = 120
x = -1.0000
1.0000
-0.0000
Q2) A = [1 2 3; 4 5 6; 7 8 0];
b = [1; 1; 1];
x = A\b
x = -1.0000
1.0000
-0.0000
Q1) A = [1 2 3; 4 5 6; 7 8 0];
b = [1; 1; 1];
x = inv(A)*b
Ans: x = -1.0000
1.0000
-0.0000
Q2) A = [1 2 3; 4 5 6; 7 8 0];
b = [1; 1; 1];
x = A\b
Ans: x = -1.0000
1.0000
-0.0000
Matrix inverse
Q1) A = [1 2 3; 4 5 6; 7 8 0];
inv(A)
Ans:
1. function f = factorial(n)
2. % FACTORIAL(N) returns the factorial of N.
3. % Compute a factorial value.
4. f = prod(1:n);
Q1) f = factorial(4)
Ans:
f = 24
Q2) f = prod(1:6)
Ans:
f = 720
Q3) game1 = input('Enter the points scored in the first game' );
game2 = input('Enter the points scored in the second game' );
game3 = input('Enter the points scored in the third game' );
average = (game1+game2+game3)/3
Ans:
Enter the points scored in the first game
15
Enter the points scored in the second game
23
Enter the points scored in the third game
10
average =
16
Chapter 5
Control flow and operators
Control flow MATLAB has four control flow structures: the if statement, the for loop, the
while loop, and the switch statemen t
n = 5; A = eye(n);
for j=2:n
for i=1:j-1
A(i,j)=i/j;
A(j,i)=i/j;
end
End
while expression
statements
end
The statements are executed as long as expression is true.
x=1
while x <= 10
x = 3*x
End