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

MATLAB Basics

The document provides examples of using basic math functions, trigonometric functions, and polynomials in MATLAB. It demonstrates how to perform simple arithmetic operations, calculate minimum/maximum values, evaluate trigonometric functions at given angles, and represent and manipulate polynomials with MATLAB code. Key functions discussed include addition, subtraction, multiplication, division, sine, cosine, tangent, inverse trigonometric functions, plotting polynomials, and finding polynomial roots and values.

Uploaded by

Udhaya Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

MATLAB Basics

The document provides examples of using basic math functions, trigonometric functions, and polynomials in MATLAB. It demonstrates how to perform simple arithmetic operations, calculate minimum/maximum values, evaluate trigonometric functions at given angles, and represent and manipulate polynomials with MATLAB code. Key functions discussed include addition, subtraction, multiplication, division, sine, cosine, tangent, inverse trigonometric functions, plotting polynomials, and finding polynomial roots and values.

Uploaded by

Udhaya Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

 Type the following in the command window

>> 2+3 < Enter > >> 2-3 < Enter >
ans = ans =
5 -1

>> 5*3 < Enter > >> 5/4 < Enter >
ans = ans =
15 1.2500

>> 5\4 < Enter > >> 2^3 < Enter >
ans = ans =
0.8000 8

>> 1/0 < Enter > >> 0/0 < Enter >
ans = ans =
Inf NaN

>> a=2 < Enter > >> x=5; < Enter >
a= >> y=4; < Enter >
2 >> z=x+y < Enter >
>> b=5 < Enter > z=
b= 9
5 >> z=x-y < Enter >
>> c=a+b < Enter > z=
c= 1
7 >> z=x*y < Enter >
>> d=a*b < Enter > z=
d= 20
10 >> z=x/y < Enter >
z=
1.2500
 Elementary math functions

rats(x) Returns in rational number format >> rats(5.8)


ans =
29/5

max(x) Returns the maximum value >>x=[0.2,-2.5,5.6,3.2];


>> max(x)
ans =
5.6000

min(x) Returns the minimum value >> min(x)


ans =
-2.5000
 Trigonometric math functions

sind(x) Sine of angle x (x in degrees) >> sind(30)


ans =
0.5000

cosd(x) Cosine of angle x (x in degrees) >> cosd(30)


ans =
0.8660

tand(x) Tangent of angle x (x in degrees) >> tand(30)


ans =
0.5774

asin(x) Inverse sine of angle x (x in radians) >> asin(0.5)


ans =
0.5236

asind(x) Inverse sine of angle x (x in degrees) >> asind(0.5)


ans =
30.0000
 Useful commands for managing variables

Command Outcome Example

who Displays a list of >> who


variables currently in
the memory Your variables are:

a ans b c d x y z

whos Displays a list of the >> whos


variables currently in Name Size Bytes Class Attributes
the memory and their
size together with a 1x1 8 double
information about ans 1x1 8 double
their bytes and class b 1x1 8 double
c 1x1 8 double
d 1x1 8 double
x 1x4 32 double
y 1x1 8 double
z 1x1 8 double

clear Removes all >> clear


variables from the
memory

clc Clears the command >> clc


window
 Worksheet

S.No Problem MATLAB code

1. Show that >> tan(pi/5)+sin(pi/5) < Enter >


π  π  ans =
tan  + sin   = 1.3143
5 5 1.3143

2. Show that >> 1+2*sin(pi/5)*cos(pi/5) < Enter >


π  π  ans =
1 + 2 sin   cos  = 1.9511
5 5 1.9511

3. Show that >> tand(48)*tand(23)*tand(42)*tand(67)


tan 48° tan 23° tan 42° tan 67° = 1 ans =
1

4. Show that >> cosd(38)*cosd(52)-sind(38)*sind(52)


cos 38° cos 52° − sin 38° sin 52° = 0 ans =
0

 Polynomials in MATLAB

Polynomial MATLAB representation

p ( x) = 2 x + 3 >> p=[2 3]

p( x) = x 2 + 4 x − 1 >> p=[1 4 -1]

p ( x) = 6 x 3 − 2 x 2 − 3 x + 1 >> p=[6 -2 -3 1]

p( x) = x 2 − 9 >> p=[1 0 -9]

p( x) = x 3 − 3x + 4 >> p=[1 0 -3 4]
Command Outcome Example

p ( x) = 2 x 3 − 5 x 2 − 14 x + 8

polyval(p,x) To find the value >> p=[2 -5 -14 8]; < Enter >
of the polynomial >> polyval(p,2) < Enter >
at a point x ans =
-24

r=roots(p) To find the roots >> r=roots(p) < Enter >


of the polynomial r=
4.0000
-2.0000
0.5000

1 and 3/2 are the roots of the polynomial


equation p( x) = 2 x 2 − 5 x + 3

p=poly(r) To find the >> r=[1 3/2]; < Enter >


polynomial when >> p=poly(r) < Enter >
the roots are p =
known 1.0000 -2.5000 1.5000

p( x) = 2 x 2 + 3x + 1 , q( x) = x + 2

h=p+q Sum of two >> p=[2 3 1]; < Enter >


polynomials >> q=[0 1 2]; < Enter >
>> h=p+q < Enter >
h=
2 4 3

h=conv(p,q) Multiplication of >> h=conv(p,q) < Enter >


two polynomials h=
0 2 7 7 2
[q r]=deconv(p,q) Division of two >> p=[2 3 1]; < Enter >
polynomials >> q=[1 2]; < Enter >
>> [q r]=deconv(p,q) < Enter >
q=
2 -1
r=
0 0 3

plot(x,y) Plotting a p( x) = 2 x 2 + 3x + 1
polynomial

>> p=[2 3 1]; >> y=polyval(p,x) >> plot(x,y)


6
>> x=[-1:0.1:1]
5
y=
4

x= Columns 1
3

Columns 1 through 9 2

through 9 1

0 -0.0800 0

-1.0000 - -0.1200 -0.1200 -1


-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

0.9000 -0.8000 -0.0800 0


-0.7000 -0.6000 0.1200 0.2800 >> p=[2 3 1]; < Enter >
-0.5000 -0.4000 0.4800 >> x=[-10:0.1:10]; < Enter >
-0.3000 -0.2000 >> y=polyval(p,x); < Enter >
Columns 10 >> plot(x,y) < Enter >
Columns 10 through 18
through 18 250

0.7200 200

150
-0.1000 0 1.0000 1.3200
100

0.1000 0.2000 1.6800 2.0800


50

0.3000 0.4000 2.5200 3.0000


0

0.5000 0.6000 3.5200 4.0800 -50


-10 -8 -6 -4 -2 0 2 4 6 8 10

0.7000
Columns 19
Columns 19 through 21
through 21
4.6800
0.8000 5.3200 6.0000
0.9000 1.0000

 Solving system of linear equations

Equations MATLAB code

x+2y=4 >> [x y]=solve('x+2*y=4','2*x-y=3') < Enter >


2x-y=3 x=
2
(Unique solution) y=
1

2x+3y=46 >> [x y]=solve('2*x+3*y=46','3*x+5*y=74') < Enter >


3x+5y=74 x=
8
(Unique solution) y=
10

2x+3y=9 >> [x y]=solve('2*x+3*y=9','4*x+6*y=18') < Enter >


4x+6y=18 x=
9/2 - (3*z)/2
(Infinitely many y=
solution) z

x+2y=4 >> [x y]=solve('x+2*y=4','2*x+4*y=12') < Enter >


2x+4y=12 Warning: Explicit solution could not be found.
> In solve at 81
(No solution) x=
[ empty sym ]
y=
[]

2x+y+z=2 >> [x y z]=solve('2*x+y+z=2','-x+y-z=3','x+2*y+3*z=-10')


-x+y-z=3 x=
x+2y+3z=-10 3
y=
(Unique solution) 1
z=
-5

 Plotting the functions

f ( x) = sin( x), [0,4π ] f ( x) = cos( x), [0,4π ]

>> ezplot('sin(x)',[0 4*pi]) < Enter > >> ezplot('cos(x)',[0 4*pi]) < Enter >
cos(x)
sin(x)

1
1

0.5
0.5

0
0

-0.5
-0.5

-1
-1

0 2 4 6 8 10 12
0 2 4 6 8 10 12
x
x

f ( x) = tan( x), [0,4π ] p ( x) = 2 x 2 + 3x + 1, [−10,10]

>> ezplot('tan(x)',[0 4*pi]) < Enter > >> ezplot('2*x^2+3*x+1',[-10 10]) < Enter >
tan(x) 2 x 2+3 x+1

200
4

2 150

0
100

-2

50
-4

-6 0

0 2 4 6 8 10 12 -10 -8 -6 -4 -2 0 2 4 6 8 10
x x

x2 + y2 = 4 x 2 + y 2 − 8 x + 10 y − 12 = 0

>> ezplot('x^2+y^2=4') < Enter > >> ezplot('x^2+y^2-4*x-8*y-45=0',[-20 20])


x 2+y 2=4
6
< Enter >
x 2+y 2-4 x-8 y-45=0
4 20

15
2

10
0
y

-2 0
y

-4 -5

-10
-6
-6 -4 -2 0 2 4 6 -15
x

-20
-20 -15 -10 -5 0 5 10 15 20
x

y 2 = 4x x 2 = −9 y

>> ezplot('y^2=4*x') < Enter > >> ezplot('x^2=-9*y') < Enter >
y 2=4 x x 2=-9 y
6 6

4 4

2 2

0 0
y

-2 -2

-4 -4

-6 -6
-6 -4 -2 0 2 4 6 -6 -4 -2 0 2 4 6
x x
x2 y2 36 x 2 + 4 y 2 = 144
+ =1
16 36

>> ezplot('36*(x^2)+4*(y^2)=144') < Enter >


>> ezplot('(x^2)/16+(y^2)/36=1') 36 (x 2)+4 (y 2)=144
6

< Enter >


4

(x 2)/16+(y 2)/36=1
6 2

4 0

y
2 -2

0
y

-4

-2 -6
-6 -4 -2 0 2 4 6
x
-4

-6
-6 -4 -2 0 2 4 6
x

36 x 2 − 4 y 2 = 144

>> ezplot('36*(x^2)-4*(y^2)=144')
< Enter >
36 (x 2)-4 (y 2)=144
6

0
y

-2

-4

-6
-6 -4 -2 0 2 4 6
x
 Differentiation and integration in MATLAB

Function Differentiation Integration

f ( x) = x 2 >> syms x < Enter > >> syms x < Enter >
>> f=x^2 >> f=x^2
f= f=
x^2 x^2
>> diff(f) >> int(f)
ans = ans =
2*x x^3/3

f ( x) = sin x >> syms x < Enter > >> syms x < Enter >
>> f=sin(x) >> f=sin(x)
f= f=
sin(x) sin(x)
>> diff(f) >> int(f)
ans = ans =
cos(x) -cos(x)

f ( x) = x 3 − 27 >> syms x < Enter > >> syms x < Enter >
>> f=x^3-27 < Enter > >> f=x^3-27 < Enter >
f= f=
x^3 - 27 x^3 - 27
>> diff(f) < Enter > >> int(f) < Enter >
ans = ans =
3*x^2 (x*(x^3 - 108))/4

x +1 >> syms x < Enter > >> syms x < Enter >
f ( x) =
x >> f=(x+1)/x < Enter > >> f=(x+1)/x <Enter>
f= f=
(x + 1)/x (x + 1)/x
>> diff(f) < Enter > >> int(f)
ans = ans =
1/x - (x + 1)/x^2 x + log(x)
>> simplify(ans) <Enter>
ans =
-1/x^2

f ( x) = sin 2 ( x) >> syms x < Enter > >> syms x < Enter >
>> f=sin(x)^2 < Enter > >> f=sin(x)^2 <Enter>
f= f=
sin(x)^2 sin(x)^2
>> diff(f) < Enter > >> int (f) < Enter >
ans = ans =
2*cos(x)*sin(x) x/2 - sin(2*x)/4

f ( x) = 5 sec x + 4 cos x >> syms x < Enter > >> syms x < Enter >
>> f=5*sec(x)+4*cos(x) >> f=5*sec(x)+4*cos(x)
< Enter > <Enter>
f= f=
4*cos(x) + 5/cos(x) 4*cos(x) + 5/cos(x)
>> diff(f) < Enter > >> int (f) < Enter >
ans = ans =
(5*sin(x))/cos(x)^2 - 4*sin(x) *sin(x) - 10*atan(tan(x/2)*i)*i

 Matrix addition, multiplication, inverse and determinant

Matrices MATLAB code

1 2 3 >> A=[1 2 3;2 3 1] < Enter >


A= 
2 3 1  A=

3 − 1 3 1 2 3
B=
− 1 0 2 2 3 1
Find A+B and A-B. >> B=[3 -1 3;-1 0 2] < Enter >
B=
3 -1 3
-1 0 2
>> A+B < Enter >
ans =
4 1 6
1 3 3
>> A-B < Enter >
ans =
-2 3 0
3 3 -1

1 − 2 3 >> A=[1 -2 3;-4 2 5] < Enter >


A=
− 4 2 5 A=

2 3 1 -2 3
B = 4 5 -4 2 5
2 1  >> B=[2 3;4 5;2 1] < Enter >
Find AB and BA. B=
2 3
4 5
2 1
>> A*B < Enter >
ans =
0 -4
10 3
>> B*A < Enter >
ans =
-10 2 21
-16 2 37
-2 -2 11
1 2 4 >> A=[1 2 4;-1 3 0;4 1 0] < Enter >
A = − 1 3 0 

A=
4 1 0  1 2 4
Find the transpose of A, the determinant of -1 3 0
A and the inverse of A. 4 1 0
>> A' < Enter >
ans =
1 -1 4
2 3 1
4 0 0
>> det(A) < Enter >
ans =
-52
>> inv(A)
ans =
0 -0.0769 0.2308
0 0.3077 0.0769
0.2500 -0.1346 -0.0962

You might also like