MODELLING AND SIMULATION USING MATLAB
1) A=[1 2 3;4 5 6;7 8 9]
>> new
A=
1 2 3
4 5 6
7 8 9
2) rand(3)
>> new
ans =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
3) rand(2,3)
>> new
ans =
0.9649 0.9706 0.4854
0.1576 0.9572 0.8003
Operating with Matrices
4) a=rand(4)
b=rand(4)
c=a+b
>> new
a=
0.0318 0.8235 0.0344 0.7952
0.2769 0.6948 0.4387 0.1869
0.0462 0.3171 0.3816 0.4898
0.0971 0.9502 0.7655 0.4456
b=
0.6463 0.6797 0.4984 0.2238
0.7094 0.6551 0.9597 0.7513
0.7547 0.1626 0.3404 0.2551
0.2760 0.1190 0.5853 0.5060
c=
0.6781 1.5032 0.5328 1.0190
0.9863 1.3499 1.3985 0.9381
0.8009 0.4797 0.7219 0.7449
0.3732 1.0692 1.3508 0.9515
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 =
0.1386 0.1493 0.2575
0.8407 0.2543 0.8143
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 =
1.0000 0 0 1.0000 0.2435 0.1966
0 1.0000 0 1.0000 0.9293 0.2511
0 0 1.0000 1.0000 0.3500 0.6160
9) A=rand(3)
B = [A, zeros(3,2); zeros(2,3), ones(2)]
>> new
A=
0.4733 0.5853 0.2858
0.3517 0.5497 0.7572
0.8308 0.9172 0.7537
B=
0.4733 0.5853 0.2858 0 0
0.3517 0.5497 0.7572 0 0
0.8308 0.9172 0.7537 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=
0 1.5708 3.1416 4.7124 6.2832
b=
0 1.0000 0.0000 -1.0000 -0.0000
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=
0.3804 0.0540 0.9340 0.4694
0.5678 0.5308 0.1299 0.0119
0.0759 0.7792 0.5688 0.3371
ans =
0.1299
ans =
0.0540 0.9340
0.5308 0.1299
ans =
0.4694
ans =
0.3804 0.0540 0.9340 0.4694
ans =
0.9340
0.1299
0.5688
12) a=[1 2 3;4 5 6; 9 8 7]
a(3)
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
Loops: For and While
13) x=10;while x > 1; x = x/2,end
>> new
x=
x=
2.5000
x=
1.2500
x=
0.6250
14) x = []; for i = 1:4, x=[x,iˆ2], end
>> new
x=
1
x=
1 4
x=
1 4 9
x=
1 4 9 16
15) x = []; for i = 4:-1:1, x=[x,i^2], end
>> 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
17) a = rand(5), b = triu(a), a == b
>> new
a=
0.1622 0.6020 0.4505 0.8258 0.1067
0.7943 0.2630 0.0838 0.5383 0.9619
0.3112 0.6541 0.2290 0.9961 0.0046
0.5285 0.6892 0.9133 0.0782 0.7749
0.1656 0.7482 0.1524 0.4427 0.8173
b=
0.1622 0.6020 0.4505 0.8258 0.1067
0 0.2630 0.0838 0.5383 0.9619
0 0 0.2290 0.9961 0.0046
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=
0.1622 0.6020 0.4505 0.8258 0.1067
0.7943 0.2630 0.0838 0.5383 0.9619
0.3112 0.6541 0.2290 0.9961 0.0046
0.5285 0.6892 0.9133 0.0782 0.7749
0.1656 0.7482 0.1524 0.4427 0.8173
ans =
5×5 logical array
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=
0.1622 0.6020 0.4505 0.8258 0.1067
0.7943 0.2630 0.0838 0.5383 0.9619
0.3112 0.6541 0.2290 0.9961 0.0046
0.5285 0.6892 0.9133 0.0782 0.7749
0.1656 0.7482 0.1524 0.4427 0.8173
>> new
a=
0.1622 0.6020 0.4505 0.8258 0.1067
0.7943 0.2630 0.0838 0.5383 0.9619
0.3112 0.6541 0.2290 0.9961 0.0046
0.5285 0.6892 0.9133 0.0782 0.7749
0.1656 0.7482 0.1524 0.4427 0.8173
20)
x = 0:pi/6:.5*pi;
y = sin(x)
>> new
y=
0 0.5000 0.8660 1.0000
21)
a=rand(3,4)
b=sin(a)
c=sqrt(b)
>> new
a=
0.8687 0.2599 0.9106 0.1455
0.0844 0.8001 0.1818 0.1361
0.3998 0.4314 0.2638 0.8693
b=
0.7635 0.2570 0.7899 0.1450
0.0843 0.7174 0.1808 0.1356
0.3892 0.4182 0.2608 0.7639
c=
0.8738 0.5069 0.8888 0.3808
0.2904 0.8470 0.4253 0.3683
0.6239 0.6466 0.5106 0.8740
Matrix Functions
22)
A=rand(3)
y=eig(A)
>> new
A=
0.5797 0.8530 0.5132
0.5499 0.6221 0.4018
0.1450 0.3510 0.0760
y=
1.4447 + 0.0000i
-0.0835 + 0.0407i
-0.0835 - 0.0407i
23)
[V,D]=eig(A)
>> new
V=
-0.7499 + 0.0000i -0.2986 - 0.3058i -0.2986 + 0.3058i
-0.6174 + 0.0000i -0.2571 + 0.2235i -0.2571 - 0.2235i
-0.2377 + 0.0000i 0.8374 + 0.0000i 0.8374 + 0.0000i
D=
1.4447 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -0.0835 + 0.0407i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i -0.0835 - 0.0407i
>> initial_velocity = 0;
acceleration = 4.5;
time = 25;
final velocity = initial velocity ...
+ acceleration * time
final velocity =
112.5000
>> format long
x = 7 + 10/3 + 5 ^ 1.2
x=
17.231981640639408
>> format short
x = 7 + 10/3 + 5 ^ 1.2
x=
17.2320
>> format bank
daily wage = 177.45;
weekly wage = daily wage * 6
weekly wage =
1064.70
>> format short e
4.678 * 4.9
ans =
2.2922e+01
>> format long e
x = pi
x=
3.141592653589793e+00
>> format rat
4.678 * 4.9
Ans =
2063/90
>> format bank
daily wage = 177.45;
weekly wage = daily wage * 6
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 = [7; 8; 9; 10; 11]
c=
10
11
>> m = [1 2 3; 4 5 6; 7 8 9]
m=
1 2 3
4 5 6
7 8 9
Create a script file, and type the following code:
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]
G = [3233; 3432; 3343; 6566]
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 =
struct with fields:
name: 'neeraj'
student =
1×2 struct array with fields:
name
rolnum
student =
1×3 struct array with fields:
name
rolnum
precentage
student =
1×3 struct array with fields:
name
rolnum
precentage
ans =
'neeraj'
ans =
23
ans =
84.4400
>>
q3) write a structure array for a patient details
paitent(1).name= 'rahul'
paitent(2).blood_group= 'B positive'
paitent(3).age= 43
paitent(4).type= 'flue'
paitent
paitent(1).name
paitent(2).blood_group
paitent(3).age
paitent(4).type
>> new
paitent =
struct with fields:
name: 'rahul'
paitent =
1×2 struct array with fields:
name
blood_group
paitent =
1×3 struct array with fields:
name
blood_group
age
paitent =
1×4 struct array with fields:
name
blood_group
age
type
paitent =
1×4 struct array with fields:
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=
0.7011 0.1781 0.5612 0.4607 0.3763
0.6663 0.1280 0.8819 0.9816 0.1909
0.5391 0.9991 0.6692 0.1564 0.4283
0.6981 0.1711 0.1904 0.8555 0.4820
0.6665 0.0326 0.3689 0.6448 0.1206
b=
0.7011 0.1781 0.5612 0.4607 0.3763
0 0.1280 0.8819 0.9816 0.1909
0 0 0.6692 0.1564 0.4283
0 0 0 0.8555 0.4820
0 0 0 0 0.1206
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
>>
2D Linear Plots
Q4) Write a 2d linear plot
x = -2*pi:pi/100:2*pi; y = sin(x); plot(x,y)
Q5) Write a 3d plot
t=.01:.01:20*pi; x=cos(t); y=sin(t); z=t.ˆ3;
plot3(x,y,z)
Chapter 3
Array operations and Linear
Equations
1) Introduction of M file Script with Example ?
Ans = M-File Scripts
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
argumenxts and can produce one or more outputs.
Examples
Here are two simple scripts.
Example 1
Consider the system of equations:
Find the solution x to the system of equations.
Solution:
• Use the MATLAB editor to create a file: File → New → M-file.
• Enter the following statements in the file:
• Save the file, for example, example1.m.
• Run the file, in the command line, by typing:
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
2) Introduction of M File script with example?
Ans = M-File functions
As mentioned earlier, functions are programs (or routines) that accept input arguments
and
return output arguments. Each M-file function (or function or M-file for short) has its own
area of workspace, separated from the MATLAB base workspace.
Example = factorial(5)
f = 120
Q) What is M file function?
M-File functions
MATLAB provides many matrix functions for various matrix/vector manipulations; see
Table 3.3 for some of these functions. Use the online help of MATLAB to find how to use
these functions
Q1) A = [1 2 3; 4 5 6; 7 8 0];
b = [1; 1; 1];
x = inv(A)*b
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
Solving linear equations
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.7778 0.8889 -0.1111
1.5556 -0.7778 0.2222
-0.1111 0.2222 -0.1111
Chapter 4
Introduction to programming in MATLAB
Q) What is M file Function?
Ans: functions are programs (or routines) that accept input arguments and return output
arguments. Each M-file function (or function or M-file for short) has its own area of
workspace, separated from the MATLAB base workspace.
Anatomy of a M-File function
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
The ‘‘if...end’’ structure MATLAB supports the variants of “if” construct.
• if ... end
• if ... else ... end
• if ... elseif ... else ... end
1. discr = b*b - 4*a*c;
if discr < 0
disp(’Warning: discriminant is negative, roots are
imaginary’);
end
2. discr = b*b - 4*a*c;
if discr < 0
disp(’Warning: discriminant is negative, roots are
imaginary’);
else
disp(’Roots are real, but may be repeated’)
end
3. discr = b*b - 4*a*c;
if discr < 0
disp(’Warning: discriminant is negative, roots are
imaginary’);
elseif discr == 0
disp(’Discriminant is zero, roots are repeated’)
else
disp(’Roots are real’)
end
The ‘‘for...end’’ loop
In the for ... end loop, the execution of a command is repeated at a fixed and
predetermined number of times. The syntax is
for variable = expression
statements
end
Usually, expression is a vector of the form i:s:j. A simple example of for loop is
for ii=1:5
x=ii*ii
End
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
The ‘‘while...end’’ loop
This loop is used when the number of passes is not specified. The looping
continues until a
stated condition is satisfied. The while loop has the form:
while expression
statements
end
The statements are executed as long as expression is true.
x=1
while x <= 10
x = 3*x
End
Saving output to a file
In addition to displaying output on the screen, the command fprintf can be used for
writing the output to a file. The saved data can subsequently be used by MATLAB or other
softwares.
To save the results of some computation to a file in a text format requires the
following
steps:
1. Open a file using fopen
2. Write the output using fprintf
3. Close the file using fclose
Here is an example (script) of its use.
% write some variable length strings to a file
op = fopen(’weekdays.txt’,’wt’);
fprintf(op,’Sunday\nMonday\nTuesday\nWednesday\n’);
fprintf(op,’Thursday\nFriday\nSaturday\n’);
fclose(op);