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

MATLAB beginner tutorial

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

MATLAB beginner tutorial

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

TUTORIAL –1

a. SUMMATION: adds different types of variable respective to their


properties
1. Scalar + scalar
Code : a=5;
b=7;
c=a+b
Output : c=12

2.Vector + Scalar
Code : a=5;
b=[1 2 2 1];
c=a+b
Output : c=[6 7 7 6]

3. row vector + column vector


Code : a=[4,5,6];
b=[1;2;3];
c=a+b
Output : c =[ 5 6 7
6 7 8
7 8 9 ]
4. vector + Matrix
Code : a=[1 2 3];
b=[1 2 3];
c=a+b
Output: c=[2 4 6]
b. Subtract the variable from another variable using their
properties
1. Scalar - scalar
Code : a=5;
b=7;
c=a-b
Output : c=-2

2.Vector - Scalar
Code : a=2;
b=[4 4 3 3];
c=a-b
Output : c=[2 2 1 1]

3. row vector - column vector


Code : a=[4,5,6];
b=[1;2;3];
c=a-b
Output : c =[3 4 5
2 3 4
1 2 3]
4. vector + Matrix
Code : a=[1 2 3];
b=[1 2 3];
c=a-b
Output: c=[0 0 0]
c. Multiplies different variables respected to their properties

1. Scalar - scalar
Code : a=5;
b=7;
c=a*b
Output : c=35

2.Vector - Scalar
Code : a=2;
b=[4 4 3 3];
c=a*b
Output : c=[8 8 6 6]

3. row vector - column vector


Code : a=[4,5,6];
b=[1;2;3];
c=a*b
Output: c=32

4. vector .* vector
Code : a=[4,5,6];
b=[1 2 3];
c=a.*b
Output: c=[8 10 18]
5. vector * matrix: (vector should be of P dim and matrix
should be of PxY dim)
Code: a=[4,5,6];
b=[1 2 3; 1 2 3; 1 2 3];
c=a*b
Output: c=[15 30 45]

6. matrix .^ vector: (one dimension must match)


Code: a=[4,5,6];
b=[1 2 3; 1 2 3; 1 2 3];
c=a.^b
Output: c=[4 25 216
4 25 216
4 25 216]

d. DIVISION (/ or \): Divides variable with respect to their


property(/ and \ is same for scalars) but a\b means (a^-1)*b
and b/a means b*(a^-1), which gives different answers
according to dimensions

1. Scalar/Scalar and Scalar\Scalar


Code: a=6;
b=4;
c=a\b;
d=a/b
Output: c=0.6667
D=1.5000

2. vector and scalar


Code: a=[4 5 6];
b=4;
Output: a/b=[1.0000 1.2500 1.5000]
b/a= Error using /
Matrix dimensions must agree.
3. row vector and row vector
Code: a=[4 6 8]
b=[2 1 2]
c=a+b
Output: c = [2.0000 6.0000 4.0000]
4. Vector and matrix
Code: a=[4 5];
b=[1 2; 3 4];
c=a/b
Output: c=[-0.5000 1.5000]

e. EXPONENT: power(a,b)

1. Scalar ^ scalar
Code: a=6;
b=2;
c=power(a,b)
Output: c=36
2. vector ^ scalar: (rather vector.^scalar)
Code: a=[2 2 3 3];
b=2;
c=a.^b
Output: c=[4 4 9 9]

3. scalar .^ vector
Code: b=[2 2 3 3];
a=2;
c=a.^b
Output: c=[4 4 8 8]

4. row vector and column vector


Code: a=[4 6 8];
b=[2 ;1 ;2];
c=a^b
Output: Error using ^
Incorrect dimensions for raising a matrix to a power. Check
that the matrix is square and the power is a scalar. To
operate on each element of the matrix
individually, use POWER (.^) for elementwise power.

Q.2 Implement the following MATLAB operators


1. zero(x[,y])
Code: a=zeros(4)
Output: a=[0 0 0 0]
2. ones(x[,y])
Code: a=ones(5)
Output: a=[1 1 1 1 1]

3. eye(x[,y])
Code: a=eye(2)
Output: a=[1 0
0 1]
4. min(variable), max(variable)
Code: a=[1 3 5 7 9 11 13]
b=min(a);
c=max(a)
Output: b=1
C=13
5. sqrt(x)
Code: a=sqrt(25)
Output: a=5
6. length(x)
Code: a=[2 3 4 5 6 7];
b=length(a)
Output: b=7
7. sin, cos, tan, asin, acos, atan: this will give the
trigonometric value of scalar as well as of vectors
Code: x=pi/3;
y=0.5;
a=sin(x)
b=cos(x)
c=tan(x)
d=asin(y)
e=acos(y)
f=atan(y)
Output: a=0.8660
b=0.5000
c=1.7321
d=0.5236
e=1.0472
f=0.4636

You might also like