Professional Documents
Culture Documents
Start MATLAB: You can enter MATLAB by double clicking on the MATLAB
shortcut icon on your window desktop. When you start MATLAB, a special
window called the MATLAB desktop appears. The major tools within from the
desktop are:-
>> (4+3)*7
ans =
49
>> S=4+3*7
S=
25
a) Addition:
for example R = 9+5+3
>> R=9+5+3
R=
17
1
b) Subtraction:
>> P=5-9
P=
-4
c) Multiplication:
>> C=15*6
C=
90
d) Division:
>> A=84/6
A=
14
>> (7+5)*9
ans =
108
>> 7+5*9
ans =
52
2
By adding parentheses, these two expression give different results:
108 and 52.
1/5+3+4/9*6/7
In MATLAB, it becomes
ans =
0.5060
>> 1/5+3+4/9*6/7
ans =
3.5810
Then x= 3+4*8-4
>> x=3+4*8-4
x=
31
Overwriting variable:
3
Once a variable has been created, it can be reassigned. In addition, if you do
not wish to see the intermediate results, you can suppress the numerical
output by putting a semicolon(;) at the end of the line.
For example
>>A=153;
>>A=A+2
A=
152
Error message:
>> T=10;
>> 5T
5T
>>format short
>> x=-163.6667
>>format long
>> x= -1.636666666666667e+002
For example,
4
b=
0.2837
c=
74.2099
CHAPTER-2
Creating Arrays and Basic plotting
Mathematical functions:
5
Example:
>> p=5;
>> v=2;
>> s=8;
z=
28.2904
>>log(752)
ans =
6.6227
>>log10(752)
ans =
2.8762
i=
0 + 1.0000i
>>sin(i)
ans =
0 + 1.1752i
>>exp(inf)
ans =
6
Inf
>> 1^inf
ans =
NaN
>> x=1/0
x=
Inf
>> y= cos(1/0)
y=
NaN
>>exp(10)
ans =
2.2026e+04
Basic plotting:
>>v=[2 3 5 7 2 9];
>>plot(v, s)
7
6
-1
2 3 4 5 6 7 8 9
>>y=sin(x);
>>plot (x,y)
8
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.5 1 1.5 2 2.5 3 3.5
>> x=linspace(0,(2*pi),100);
>> y=tan(x);
>> z=cos(x);
>>plot(x,y,'m-')
>> hold
>>plot(x,z,'k--')
9
80
60
40
20
-20
-40
-60
-80
0 1 2 3 4 5 6 7
>> x=linspace(0,(2*pi),100);
>> y=sin(x);
>> z=cos(x);
>> w=tan(x);
>>plot(x,y,'r-',x,z,'g--',x,w,'b:')
10
80
60
40
20
-20
-40
-60
-80
0 1 2 3 4 5 6 7
>> y1=2*cos(x);
>> y2=cos(x);
>> y3=0.5*cos(x);
>>plot(x,y1,'--',x,y2,'-',x,y3,':')
>>ylabel('cosine function')
>>legend('2*cos(x)','cos(x)','0.5*cos(x)')
11
>>axis([0 2*pi -3 3])
12
Entering a vector (One-dimensional array):
>>a= [3 4 5 6 7]
x=
34567
y=
>>x (1:3)
ans =
34 5
To find a particular value of x form first row and forth column in Matlab
>>x(1,4)
ans =
13
Produces a column vector, we have,
>>x(:)
ans =
>>x(1:end)
ans =
3 4 5 6 7
A=
42 5
7 7 9
2 2 3
>>A(3,2)
ans =
14
Matrix indexing:
>>A(3,2)=0
A=
42 5
7 7 9
2 0 3
Transposing a matrix:
A=
4 2 5
7 7 9
2 2 3
>> A'
ans =
4 7 2
27 2
5 9 3
Creating a sub-matrix:
A=
15
4 2 5
7 7 9
2 2 3
B=
7 7
2 2
C=
2 2 3
7 7 9
42 5
>>A(end:-1:1,end)
ans =
Colon operator:
>> 1:13
16
ans =
1 2 3 4 5 6 7 8 9 10 11 12 13
Linear Spacing:
For example,
Y= linspace (a, b)
>>linspace(0,6,10)
>>linspace(0,2*pi,40)
ans =
A=
3 5 7
2 7 7
17
2 9 4
For example,
ans =
2 9 4
The colon operator can also used to extract a submatrix from a matrix A.
>>A(:,1:3)
ans =
3 5 7
2 7 7
2 9 4
To delete a row or column of a matrix A, use the empty vector operator, [].
A=
3 5 7
2 7 7
2 9 4
A=
3 5 7
2 9 4
18
>> A=[A(1,:);[2 7 7];A(3,:)]
A=
3 5 7
2 7 7
2 9 4
Dimension:
For example,
>>size(A)
ans =
3 3
Concatenating matrices:
B=
3 5 7 30 50 70
7 7 9 70 70 90
2 2 3 20 20 30
-3 -5 -7 1 0 0
-7 -7 -9 0 1 0
-2 -2 -3 0 0 1
Matrix generators:
19
MATLAB provides functions that generates elementary matrices. The matrix
of zeros, the matrix of ones, and the identity matrix are returned by the
functions zeros, ones, and eye, respectively
Elementary matrice:
A=
1 0 0
0 1 0
0 0 1
B=
0 0 0 0 0
0 0 0 0 0
3.>> A=ones(2,3)
A=
1 1 1
1 1 1
4.>> A=rand(2,2)
A=
0.8147 0.9134
0.9058 0.6324
20
5.>> s=[1 2 4];
>>diag(s)
ans =
1 0 0
0 2 0
0 0 4
Now create a 5 by 5 matrix in which the middle two rows and middle
two columns are 1s, and the rest of the entries are 0s.
>> x=zeros(5,5)
x=
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
>>x(3:4,:)=ones(2,5)
x=
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
1 1 1 1 1
0 0 0 0 0
>>x(:,3:4)=ones(6,2)
x=
0 0 1 1 0
21
0 0 1 1 0
1 1 1 1 1
1 1 1 1 1
0 0 1 1 0
22
CHAPTER-3
Array Operations and Linear Equations
Array operations:
Using in MATLAB
A=
1 4 5
7 2 1
3 8 7
B=
2 2 6
8 5 2
4 6 2
>>A+B
23
ans =
3 6 11
15 7 3
7 14 9
>> A*B
ans =
54 52 24
34 30 48
98 88 48
>> A^2
ans =
44 52 44
24 40 44
80 84 72
>> A-B
ans =
-1 2 -1
-1 -3 -1
-1 2 5
>> A/B
ans =
24
0.8514 -0.6216 1.5676
A=
5 2 3
7 5 6
7 2 9
B=
10 20 30
40 50 60
70 80 90
>> c=A.*B
c=
50 40 90
>> A.^2
ans =
25
25 4 9
49 25 36
49 4 81
Operation Matrix
Array
Addition +
+
Subtraction -
-
Multiplication *
.*
Division /
./
Left Division \ .\
Exponentiations ^
.^
4x +2y +6z = 8
2x +8y +2z = 4
6x +10y +3z = 0
26
1. x = (use the matrix inverse, inv.)
P=
4 3 6
2 2 2
6 8 3
>> q=[8;4;0]
q=
>> x=inv(P)*q
x=
38.0000
-24.0000
-12.0000
>> q=[8;4;0]
>> x=P\q
x=
27
38.0000
-24.0000
-12.0000
Matrix inverse:
A=
7 8 0
4 5 6
1 3 3
>>inv(A)
ans =
Determinant of A is
>>det(A)
ans =
-69
Matrix functions:
(a) A+B
(b) AB
(c) A2
28
(d) AT
(e) B-1
(f) ATBT
(g)A2 +B2 AB
Solution:-
A=
3 5 1
2 5 4
8 8 7
B=
7 3 4
2 5 6
-3 3 7
>> A+B
ans =
10 8 5
4 10 10
5 11 14
>> A*B
29
ans =
28 37 49
12 43 66
51 85 129
>> A^2
ans =
27 48 30
48 67 50
96 136 89
>> A'
ans =
3 2 8
5 5 8
1 4 7
>>inv(B)
ans =
>> A'*B'
ans =
59 64 53
82 83 56
30
47 64 58
>> A^2+B^2-A*B
ans =
42 59 55
42 73 64
9 78 15
>>det(A)
ans =
75
>>det(B)
ans =
107
>>det(A*B)
ans =
8.0250e+03
Solution:
A=
4 5 -3
31
-1 2 3
2 3 7
>>eig(A)
ans =
2.3714 + 2.6858i
2.3714 - 2.6858i
8.2571
>> v=eig(A)
v=
2.3714 + 2.6858i
2.3714 - 2.6858i
8.2571
>> [v,d]=eig(A)
v=
d=
2.3714 + 2.6858i 0 0
0 2.3714 - 2.6858i 0
0 0 8.2571
B=
1 2 3
32
8 7 6
5 3 1
>>eig(B)
ans =
11.8655
-2.8655
0.0000
>>h=eig(B)
h=
11.8655
-2.8655
0.0000
>> [V,D]=eig(B)
V=
D=
11.8655 0 0
0 -2.8655 0
0 0 0.0000
33
34
CHAPTER-4
Introduction To Programming in MATLAB
Introduction:
All the commands were executed in the Command Window. The problem is
that the commands entered in the Command Window cannot be saved and
executed again for several times. Therefore, a different way of executing
repeatedly commands with MATLAB is:
M-File Scripts:
For example,
x + 2y + 3z = 1
3x + 3y + 4z = 1
2x + 3y + 3z = 2
Solution:
b=[1;1;2] x=A\b Now Save the file. for example, example1.m. Run and
change folder, in the command line, by typing:
A=
1 2 3
35
3 3 4
2 3 3
b=
x=
-0.5000
1.5000
-0.5000
Example
Solution:
x = 0:pi/100:2*pi;
y1 = 2*cos(x);
y2 = cos(x);
y3 = 0.5*cos(x);
plot(x,y1,'--',x,y2,'-',x,y3,':')
ylabel('Cosine functions')
legend('2*cos(x),cos(x),0.5*cos(x)')
36
axis([0 2*pi -3 3])
now save the file name example2, run the file ,in the command window, by
typing:
M-File functions:
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.
function f = factorial(n)
f = prod(1:n);
37
As an example, for n = 12, the result is,
>>factorial(12)
ans =
479001600
Script Functions
Do not accept input arguments Can accept input arguments
or return output arguments. and return output arguments.
Store variables in a workspace Store variables in work space
that is shared with other internal to the function.
scripts. Are useful for extending the
Are useful for automating a Matlab language for your
series of commands. application.
The input arguments are listed inside parentheses following the function
name. The output arguments are listed inside the brackets on the left side.
38
They are used to transfer the output from the function file. The general form
looks like this:
When a script file is executed, the variables that are used in the calculations
within the file must have assigned values. The assignment of a value to a
variable can be done in three ways.
We have already seen the two first cases. Here, we will focus our attention
on the third one. In this case, the variable is defined in the script file. When
the file is executed, the user is prompted to assign a value to the variable in
the command prompt. This is done by using the input command. Here is an
example.
Average = (gameA+gameB+gameC)/3
39
Now in Matlab:
>>average = 7.66667
Output commands:
40
Chapter 5
Control flow and operators
Introduction
MATLAB is also a programming language. Like other computer programming
languages, MATLAB has some decision making structures for control of
command execution. These decision making or control ow structures
include for loops, while loops, and if-else-end constructions. Control ow
structures are often used in script M-files and function M-files. By creating a
file with the extension .m, we can easily write and run programs. We do not
need to compile the program since MATLAB is an interpretative (not
compiled) language. MATLAB has thousand of functions, and you can add
your own using m-files. MATLAB provides several tools that can be used to
control the ow of a program (script or function).
Control flow
MATLAB has four control ow structures: the if statement, the for loop, the
while loop, and the switch statement.
if ... end
if expression
statements
end
41
ifdiscr< 0
disp(Warning: discriminant is negative, roots are imaginary);
end
2. discr = b*b - 4*a*c;
ifdiscr< 0
disp(Warning: discriminant is negative, roots are imaginary);
elsedisp(Roots are real, but may be repeated)
end
3. discr = b*b - 4*a*c;
ifdiscr< 0
disp(Warning: discriminant is negative, roots are imaginary);
elseifdiscr == 0
disp(Discriminant is zero, roots are repeated)
elsedisp(Roots are real)
end
no semicolon (;) is needed at the end of lines containing if, else, end
Operator Description
42
> Greater than
< Less than
>= Greater than or
equal to
<= Less than or equal
to
== Equal to
= Not equal to
& AND operator
| OR operator
NOT operator
In the for ... end loop, the execution of a command is repeated at a fixed and
predetermined number of times. The syntax is:
statements
end
for h=1:7
h*h
end
Now in Matlab:
43
4
16
25
36
49
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
s=1
while s <= 10
s = 5*s
end
>>s =
s=
s=
44
25
It is important to note that if the condition inside the looping is not well
defined, the looping will continue indefinitely. If this happens, we can stop
the execution by pressing Ctrl-C.
Other ow structures:
The break statement. A while loop can be terminated with the break
statement, which passes control to the first statement after the
corresponding end. The break statement can also be used to exit a for loop.
The continue statement can also be used to exit a for loop to pass
immediately to the next iteration of the loop, skipping the remaining
statements in the loop.
Operator precedence:
Precedence Operator
1 Parentheses ()
2 Transpose (.0), power (. ), matrix power ( )
3 Unary plus (+), unary minus (), logical negation ()
4 Multiplication (.), right division (. /), left division (.\), matrix
multiplication (),
matrix right division (/), matrix left division (\)
5 Addition (+), subtraction ()
6 Colon operator (:)
7 Less than (<), less than or equal to (), greater (>), greater than or
equal to (),
equal to (==), not equal to (=)
8 Element-wise AND, (&)
9 Element-wise OR, (|)
45
In addition to displaying output on the screen, the command fprintf can be
used for writing the output to a file. To save the results of some computation
to a file in a text format requires the following steps:
Example:
op = fopen(weekdays.txt,wt);
fprintf(op,Sunday\nMonday\nTuesday\nWednesday\n);
fprintf(op,Thursday\nFriday\nSaturday\n);
fclose(op);
ans=
Assignment-1
Part -1
Q.1
46
4
3+
5
a) Compute , 2 + 3 in command window.
5+
4
Ans:->> 2+(3+4/5)/(5+3/4)
ans =
2.6609
b) Choose the most appropriate word from the options given below
to complete the following sentence:
1) Character string
2) Command line
3) Title of a command
4) All of these
( 3 ).
ans =
Ans:->> r=1609344000;
>> s=3*10^8;
>> t = r/s
47
t=
5.3645
e) Create two (3*4) Matrices and use a Matlab command to add the
matrices element by element.
T=
7 4 3 4
2 6 3 8
9 10 11 12
S=
5 6 7 8
9 1 4 6
6 5 4 2
>> T+S
ans =
12 10 10 12
11 7 7 14
15 15 15 14
Part-2
6 43 2 11 87
Q.2 Create the following matrix A = 12 6 34 0 5 . Use the
34 18 7 41 9
matrix A to:
48
a) Create a five element row vector named va that contains the
elements of the second row of A.
A=
6 43 2 11 87
12 6 34 0 5
34 18 7 41 9
a) >>va=A(2,:)
va =
12 6 34 0 5
b) >>vb=A(4,:)
vb =
11 0 41
c) >>vc=[A(1,:) A(2,:)]
vc =
6 43 2 11 87 12 6 34 0 5
d) >>vd=[A(4:6) A(13:15)]
vd =
43 6 18 87 5 9
x tan x +sin x
cos2 2 = 2 tan x
49
Verify that the identity is correct by calculating each side of the
equation, substituting
x= 5
Ans:->> x=pi/5;
>> LHS=(cos(x/2))^2
LHS =
0.9045
>> RHS=(tan(x)+sin(x))/(2*tan(x))
RHS =
0.9045
Part-3
Q.4
Results from measuring F in six tests are given in the table below.
Determine the coefficient of friction in each test, and the average
from all tests. (In command window)
Test # 1 2 3 4 5 6
Mass m 2 4 5 10 20 50
(kg)
Force F 12.5 23.5 30 61 117 294
(N)
Ans:->> g=9.81;
>>mass=[2 4 5 10 20 50]
50
mass =
2 4 5 10 20 50
force =
>>mv=force./(mass*g)
mv =
>>avg=mean(mv)
avg =
0.6109
4 x 2 y + 6z = 8
2x+8y+2z=4
6 x + 10 y + 3 z = 0
>> B=[8;4;0];
>> X=inv(A)*B
X=
-1.8049
0.2927
2.6341
51
Assignment-2
Part-1
Question1. Start Matlab.
Command Window
Workspace
52
Command history
Current Directory
Help Browser
Start Button
>> 1+2
Ans =
>> x=1+2
x=
>> x=1+2;
>> y=x^2+2*x+8
y=
23
format long e
Pi
You can use the arrow keys and the delete key to recall and edit previous
commands. Press the up arrow key twice to recall the format command and
delete the e and press enter. Then display pi again. Repeat with the
following formats.
format short e
format short
Solution:-
>>format long e
53
>>pi
Ans=
3.141592653589793e+00
>>pi
Ans = 3.141592653589793e+00
>>format short e
>>pi
Ans =
3.1416e+00
>>format short
>>pi
Ans =
3.1416
= /
()^ + ()^
(() + ())
Solution:-
>> = /6
= 0.5236
>>sin()^ 2 + cos()^ 2
Ans = 1
54
Ans = 72.0000
a = [1 2 3]
b = [4 56]
c =a + ib
here is the square root of -1. You can use instead if that is what you are
use to.
Solution:-
>> = [1 2 3]
=1 2 3
>> = [4 5 6]
b= 4 5 6
>>c = +
>>imag(z)
ans=
4 5 6
>>conj(z)
ans=
Solution:-
>>mag = sqrt(y.^2+x.^2)
55
mag =
>> a=angle(z)
a=
Z.
Solution:-
>>z'
ans =
1.0000 - 4.0000i
2.0000 - 5.0000i
3.0000 - 6.0000i
>>z.'
ans=
1.0000 + 4.0000i
2.0000 + 5.0000i
3.0000 + 6.0000i
2 2 5 6
A= 3 4 B= 4 8
Solution: -
>> A= [2 2; 3 4]
56
A=
2 2
3 4
B=
5 6
4 8
Question10. Try the following and ensure you can follow what is happening:
>> A+5
7 7
Ans= 8 9
>> A+B
7 8
Ans = 7 12
>> A-B
3 4
Ans = 1 4
>> A*B
57
18 28
Ans = 31 50
>> A^2
10 12
Ans = 18 22
>> A'
2 3
Ans = 2 4
Solution:-
>> b=[5;11]
b=
11
>> x=A\b
x=
58
>> A*x
Ans =
Solution:-
>> b= (1:100)'
b=
1 13 25 37 49 61 73 85 97
2 14 26 38 50 62 74 86 98
3 15 27 39 51 63 75 87 99
4 16 28 40 52 64 76 88 100
5 17 29 41 53 65 77 89
6 18 30 42 54 66 78 90
7 19 31 43 55 67 79 91
8 20 32 44 56 68 80 92
9 21 33 45 57 69 81 93
10 22 34 46 58 70 82 94
11 23 35 47 59 71 83 95
12 24 36 48 60 72 84 96
59
>> x=A1\b
x=
60
-417.53 325.65 92.042 42.583 58.938 261.85
94 27 6 2 4 66
>> A1*x
Ans=
61
55.00 71.00 79.00 87.00 94.00
00 00 00 00 00
63.000
56.00 0 72.00 80.00 88.00 95.00
00 00 00 00 00
64.00
57.00 00 73.00 81.00 89.00 96.00
00 00 00 00 00
65.00
58.00 00 74.00 82.00 90.00 97.00
00 00 00 00 00
66.00
59.00 00 75.00 83.00 91.00 98.00
00 00 00 00 00
67.00
60.00 00 76.00 84.00 99.00
00 00 00 00
68.00 92.000
61.00 00 0
00
69.00 77.000 85.000 100.00
62.00 00 0 0 0
93.000
00
70.00 78.00 86.00 0
00 00 00
62
Part-2
>> A= [2 2;3 4]
A=
2 2
3 4
>> B= [5 6; 4 8]
B=
5 6
4 8
>> A.*B
Ans =
10 12
12 32
>> A.\B
Ans =
2.5000 3.0000
1.3333 2.0000
>> A.^B
Ans =
32 64
81 65536
>>sin(A*pi/6)
Ans =
0.9511 0.9511
0.9511 0.5878
>> A.^2
ans =
4 4
9 16
>>sqrt(D)
Ans =
2 2
3 4
>>clear
x = linspace(1,1,100); y = 2x.^3 x;
Solution:-
>> x=linspace(-1,1,100);
>> y=2*x.^3-x;
>>plot(x,y)
Question4. Plot the polynomial 4x3 3x using the function polyval. First find
out how to use polyval using the help. doc polyval
p = [ 40 30]
y1 =polyval(p,x);
hold on
plot(x,y1,g)
Solution:-
>>doc polyval
>> p=[4 0 -3 0]
p=
4 0 -3 0
>> hold on
>>plot(x,y1,'g')
Question5. There are many functions that handle polynomials. Look them
up in the help. Enter doc polyval again, then click on polynomials on the path
at the top of the page. What does the function roots do?
Solution:- roots
Polynomial roots
Syntax : = ()
Question6. Plot the roots of the polynomial onto the graph. ry =zeros(3,1)
The plot should still be held from exercise 4. plot(r,ry,rx) . Clear the
figure :clf
Solution:-
>> = ()
r=
0.8660
-0.8660
>>ry= zeros(3,1)
ry =
>> plot(r,ry,'rx')
Question7. Enter the following:
Solution:-
>> a=2:0.5:4
a=
>>a(2)
Ans= 2.5000
>>a([2 4])
Ans =
2.5000 3.5000
>>a(2:4)
Ans =
>>a(2:end)
Ans =
This produces a 5 by 6 matrix. Set the third element on the second row of w
to 100.
Solution:-
>> w=(1:5)'*(5:10)
w=
5 6 7 8 9 10
10 12 14 16 18 20
15 18 21 24 27 30
20 24 28 32 36 40
25 30 35 40 45 50
(2,3)=10
W= 5 6 7 8 9 10
10 12 100 16 18 20
15 18 21 24 27 30
20 24 28 32 36 40
25 30 35 40 45 50
Question 9. Enter the following:
>>w(2:4,2:4)
Ans =
12 14 16
18 21 24
24 28 32
>>w(2,:)
Ans =
10 12 14 16 18 20
>>w(:,5)
Ans =
18
27
36
45
Ans =
6 8
30 40
>>w(:)
Ans =
10
15 50
20 18
25 24 35
6 30 8
12 7 16 18
40 14 24 27
21 32 36
28 40 45
9 10
20
>>w(:)=1:30
w=
1 6 11 16 21 26
2 7 12 17 22 27
3 8 13 18 23 28
4 9 14 19 24 29
5 10 15 20 25 30
who
whos
Save
Clear
All variables should have been saved to matlab.mat . If you cant see this in
the current folder window, right click in the window and select refresh.
Solution:-
>>who
aans p r ry w x y y1
>>whos
a 1x5 40 double
p 1x4 32 double
r 3x1 24 double
ry 3x1 24 double
>>clear
Clear command clear all the variables from workspace.
edit graph
grid
Solution:-
>> x=linspace(-2*pi,2*pi,100);
>> y=sin(x);
>>plot(x,y)
>>grid
Q. 12. Add the following at the end of script created above.
hold on
y1=mysin(x);
plot(x,y1,r:)
axis([-2*pi,2*pi,-2,2])
Solution: x=linspace(-2*pi,2*pi,100);
y =sin(x);
plot(x,y)
grid
holdon
y1=sin(x);
plot(x,y1,'r:d');
axis([-2*pi,-2,2])
degrees = 0:6:360;
rad = degrees*pi/180;
plot (sin(rad))
Close the graph ans repeat the first two commands by double clicking on
them in the Command History window.
Solution:
>>degrees = 0:6:360;
>>rad = degrees*pi/180;
>>plot(sin(rad))
Q.16: Drag the plot command from the Command History into the
command window and change it to:-
plot(degrees,sin(rad))
Solution:
>>degrees = 0:6:360;
>>rad = degrees*pi/180;
>>plot(degrees,sin(rad))
Q.17: Hold down control and select each of the following commands
in the command history window,
degrees = 0:6:360;
rad = degrees*pi/180;
plot(degrees,sin(rad))
Then press the right mouse button and select create script.
Solution:
degrees = 0:6:360;
rad = degrees*pi/180;
plot(degrees,sin(rad))
xlabel ('degrees')
ylabel ('value')
grid
Q.18: Annotate the graph by adding the following lines to the script.
title(sine wave)
xlabel(degrees)
ylabel(value)
grid
Solution:
degrees=0:6:360;
rad=degrees*pi/180;
plot(degrees,sin(rad))
Q.19: Change the title and the plot command so that a cosine wave is
plotted in green. Save as graph2.
Solution:
degrees=0:6:360;
rad=degrees*pi/180;
plot(degrees,cos(rad),'g')
title('cos wave')
xlabel('degrees')
ylabel('value')
grid
Q.20. Change the title and the plot command so that tan is plotted in
red. Change the y limit to -10 to 10. Save as graph3.
Solution:
degrees=0:6:360;
rad=degrees*pi/180;
plot(degrees,tan(rad),'r')
title('tan wave')
xlabel('degrees')
grid
ylabel('value')
Practical Assignment
Q. 1: Find a short MATLAB expression to built the matrix
1 2 3 4 5 6 7
b= 9 7 5 3 1 1 3
4 8 16 32 64 128 256
Solution:
b=
1 2 3 4 5 6 7
9 7 5 3 1 -1 -3
4 8 16 32 64 128 256
Solution:
Ans=
12
-2
320
Solution:
>> [0 0 1]*b
Solution:
>> [1 0 0;0 0 1;0 1 0]*b
ans =
1 2 3 4 5 6 7
4 8 16 32 64 128 256
9 7 5 3 1 -1 -3
1 2 34 5
1 2 34 5
1 2 34 5
Solution:
>> [1 1 1]'*(1:5)
ans =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
b) matrix
Solution:
>> (0:4)'*[1 1 1]
ans =
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
1 2 3 4 5 6 7
A= 11 12 12 13 14 5 6
9 10 16 18 19 20 21
22 23 24 25 26 27 28
a) Create a 3*4 matrix b from the 1st , 3rd and 4th rows and
the 1st , 3rd , 5th and 7th columns of the matrix A.
Solution:
B=
1 3 5 7
9 16 19 21
22 24 26 28
Solution:
u=
9 10 16 18 19 20 21 5 14 19 26 7 6 21 2
Assignment-3
Q.1 Write a user defined Matlab function, with two input or two output
arguments that determines the height in cm. and mass in Kg of a person
from his height in inches and weight in pounds. For the function name and
arguments use [cm, kg] = value (in,lb).The input arguments are the height
in inches and weight in pounds, and the output arguments are the height
in cm. and mass in Kg. use the function in the command window to:
a) Determine in SI units the height and mass of a 5ft. 10 in. person who
weights 175 lb.
b) Determine your own height and weight in SI unit.
cm = in*2.54
kg = lb*0.454
a) >>value(70,173)
cm =
177.8000
kg =
78.5420
ans =
177.8000
b)>>value(58,128)
cm =
147.3200
kg =
58.1120
ans =
147.3200
Q.2 Write a user defined Matlab function for the following math function:
r= 0.9a4-12a2-5a
The input to the function is x and output is r. Write the function such
that x can be a vector.
r = (0.9*a.^4-12*a.^2-5*a);
plot(a, r);
I.
>>opt(-7)
ans =
1.6079e+03
>>opt(17)
ans =
7.1616e+04
>>opt(x)
10
-10
-20
-30
-40
-50
-60
-3 -2 -1 0 1 2 3
Q.3 Write a user defined Matlab function for the following function:
r()= 2(1.1-sin2()). The input to the function (in radians) and the
output is r. write the function such that can be a vector:
1) >> equation(pi/5)
a=
1.5090
ans =
1.5090
>>equation(2*pi/4)
a=
0.2000
ans =
0.2000
2) equation(0:0.08:2*pi)
90
2.5
120 60
2
1.5
150 30
1
0.5
180 0
210 330
240 300
270
Q.4 Write a user defined Matlab function that calculates the local
maximum or minimum of a quadratic function of the form:
f(x) = px2 + qx + r. for the function name and the arguments use [c, d] =
maxmin(p, q, r). The input arguments are the constants p, q and r, and the
output arguments are the coordinates x and y of the maximum or the
minimum. Use the function to determine the maximum or minimum of the
following functions:
I. F(x)= 4x2-7x+22
II. F(x)=5x2-7x+2
>>>>maximin(4,7,22)
c=
0.7000
d=
42.3500
ans =
0.7000
>>maximin(5,-7,2)
c=
-0.7000
d=
9.3500
ans = -0.7000
>>loan(10000,7,30)
p=
7.6123e+04
ans =
7.6123e+04
Q.6 Write a user defined Matlab function that determines the angles of a
triangles when the lens of the size are given. For the function name and
the argument use [al, bet, gam] = triangle(a, b, c). Use the function to
determine the angles in triangles with the following sides:
r = acosd(p.^2+q.^2-r.^2)./(2*p*q)
p = acosd(q.^2+r.^2-p.^2)./(2*q*r)
q = acosd(r.^2+p.^2-q.^2)./(2*r*p)
2) >> tri(200,300,210)
r=
0 + 0.0058i
p=
191.0211
q=
-3.0177e+02 - 8.1863e+01i
3)>>tri(7,9,10)
r=
0 + 1.8617i
p=
6.9143
q=
-9.5579 - 6.9918i
Q.7 Evaluate the following expressions without using Matlab. Check the
answer with Matlab:
1. 5 <= 8-3
2. Y=7<3-1+6>2
3. Y=(7<3)-1+(6>2)
4. Y= 2*4+5==7+20/4
Now , in Matlab:
ans = 1
ans =
0
3.) >> (7<3)-1+(6>2)
ans =
0
Q.8 Given: a=10, b=6. Evaluate the following expressions without using
Matlab. Check the answer with Matlab:
1) Y = a>=b
2) Y = a-b<=b/2
3) Y = a-(b<=b/2)
Within Matlab:
>> a=10;
>> b=6;
>> a>=b
ans =
2)withoutMatlab:
Within Matlab:
>>a-b<=b/2
ans =
3) without Matlab:
10-(6<=3) = 10
[because 6<3 is false i.e.= 0 ]
Within Matlab:
>> a-(b<=b/2)
ans =
10
1) V>=w
2) W~=v
>> w= [0 2 1 -1 0 -2 4 3 2];
1) without Matlab:
v=[4 5 0 1 8 2]
Within Matlab:
>> v>=w
ans =
1 0 0 1 1 1 0 1 1
>>withoutMatlab:
W=[0 2 1 -1 -2 4 3]
Within Matlab:
>> W~=v
ans =
1 1 1 1 0 1 1 1 0
>>Y=w>v
ans =
0 1 1 0 0 0 1 0 0
Q.11 Evaluate the following expressions without using Matlab. Check the
answer with Matlab:
1. 5& -2
2. 8-2|6+5& -2
3. ~(4&0) + 8*~(4|0)
Sol.>> 5&-2
ans =
1
>> 8-2|6+5&-2
ans =
>>~(4&0)+8*~(4|0)
ans =
Q.12 The maximum daily temperature (in 0F) for New York city and
Anchorage, Alaska during the month of January, 2001 are given in the
vector below(data from the U.S. National Oceanic atmospheric
administration).
T = [31 26 30 33 33 39 41 41 34 33 45 42 36 39 37 45 43 36 41 37 32 32 35
42 38 33 40 37 36 51 50]
S = [37 24 28 25 21 28 46 37 36 20 24 31 34 40 43 36 34 41 42 35 38 36 35
33 42 42 37 26 20 25 31]
Sol. T=[31 26 30 33 33 39 41 41 34 33 45 42 36 39 37 45 43 36 41 37 32 32 35 42
38 33 40 37 36 51 50];
S=[37 24 28 25 21 28 46 37 36 20 24 31 34 40 43 36 34 41 42 35 38 36 35 33 42
42 37 26 20 25 31];
avgk = mean(T)
avgl = mean(S)
a = sum(T<avgk)
b = sum(S<avgl)
c = S>T;
d = sum(e)
e = find(e)
f = (T==S);
g = sum(o)
h = find(o)
i = sum((T>32)&(S>32))
j = find((T>32)&(S>32))
>>mine
avgl =
37.6774
avgl =
33.1290
a=
17
b=
13
c=
11
d=
1 7 9 14 15 18 19 21 22 25 26
e=
1
f=
23
g=
16
h=
7 8 9 13 14 15 16 17 18 19 20 23 24 25 26 27