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

Matlab Note: by Abdullah (Student of B S Maths 4 Semester GPG College Timergara Lower Dir) - Date: 9/5/2019

The document provides information about MATLAB commands and programming. It contains 6 sections that cover: 1) Basic MATLAB commands, 2) Programming in MATLAB, 3) Matrices in MATLAB, 4) Graphs in MATLAB, 5) Matrix algebra, and 6) Limits, derivatives and integrations. The document is a note written by a student for their MATLAB class that serves to summarize key MATLAB concepts and commands. Examples are provided throughout to demonstrate how to use various MATLAB functions and operations on matrices.

Uploaded by

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

Matlab Note: by Abdullah (Student of B S Maths 4 Semester GPG College Timergara Lower Dir) - Date: 9/5/2019

The document provides information about MATLAB commands and programming. It contains 6 sections that cover: 1) Basic MATLAB commands, 2) Programming in MATLAB, 3) Matrices in MATLAB, 4) Graphs in MATLAB, 5) Matrix algebra, and 6) Limits, derivatives and integrations. The document is a note written by a student for their MATLAB class that serves to summarize key MATLAB concepts and commands. Examples are provided throughout to demonstrate how to use various MATLAB functions and operations on matrices.

Uploaded by

abullah jan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

-------The------------

MATLAB
NOTE
by Abdullah (Student of B S Maths 4th semester GPG
college Timergara lower dir).

Date: 9/5/2019
[email protected]:

1
Dedicated to my Lovely teachers proffessor Adur Rehman
and Murad khan:

2
TABLE OF CONTENTS
1. Basic commonds of matlab........................................4
2. programing in matlab.................................................8
3. Matrix in matlab........................................................10
4. Graphs in matlab.......................................................27
5. Matrix algebra...........................................................52
6. limits , derivatives and integrations.....................56
7.paper of matlab (2019).............................................58

3
1) Basics commonds of matlab:
>> format long g; % it is use for 15 digits after decimal %

>> a=sqrt(78)

a = 8.83176086632785

>> format short g; % it is use for 4 digit after decimal %

>> sqrt(78)

ans = 8.8318

>> format long e; % it is use for 15 digit after decimal in exp form %

>> sqrt(78)

ans = 8.831760866327848e+00

>> format short e; % it is use for 4 digit after decimal in exp form %

>> sqrt(78)

ans = 8.8318e+00

>> format rat; % give answer in rational form %

>> 1/2

ans = 1/2

>> a=0.5

a = 1/2

>> rats(a) % it give rational out put %

ans =' 1/2 '

>> rats(2^(1/2)*5) % it give rational out put %

ans =' 1393/197 '

4
>> syms a; % it shows that a is arbitrary costant %

>> x=2;

>> y=2*x+a

y=a+4

>> x=0;

>> y=exp((x^2)+2)

y = 7.3891

>> x=4;

>> syms a;

>> y=2*(x^2)+sqrt(x^3)+a

y = a + 40

>> syms x y;

>> f=2*x;

>> inv=finverse(f) % to find inverse of fuction f %

inv = x/2

>> f=2*x^2

>> inv=finverse(f) % to find inverse of fuction f %

inv = (2^(1/2)*x^(1/2))/2

>> sin(pi/2) % it find the sin function value %

ans = 1

>> acos(0) % it find the inverse of cos %

ans = 1.5708

>> a=4.678

>> fix(a) % round toward zero %

5
ans = 4

>> floor(6.77)

ans = 6

>> ceil(78.99)

ans = 79

>> ceil(2.33)

ans = 3

>> abs(-5) % it is use for finding absalute %

ans = 5

>> factor(78) % it find the factor of a number %

ans = 2 3 13

>> gcd(2,4) % it find greatest common deviser %

ans = 2

% find the gcd of 34,56,67,35 %

>> a=gcd(34,56)

a= 2

>> b=gcd(a,67)

b= 1

>> c=gcd(b,35)

c= 1 % gcd of 34,56,67,35 is 1 %

>> lcm(2,4) % it find least common multiple %

ans = 4

>> exp(2) % it find the power of e %

ans = 7.3891e+00

6
>> clc % it clear the commond window %

>>clear % it is use for clearing work space %

>> 3+4 % addition %

ans = 7

>> 9-7 % subtraction %

ans = 2

>> 6*4 % multiplication %

ans = 24

>> 48/4 % division %

ans = 12

>> rem(7,2) % find remiander %

ans = 1

>> 67^2 % it find power %

ans = 4489

>> 8^(1/3)

ans =2

>> factorial(2) % it find factorial %

ans = 2

>> who % it shows all variable in work space %

Your variables are:

a ans

>> conj(2+3i)

ans = 2.0000 - 3.0000i

>> c1=complex(2,4)

7
c1 = 2.0000 + 4.0000i

>> c3=sqrt(-2)

c3 = 0.0000 + 1.4142i

>> c4= (c1+c3)/2+3i

c4 = 1.0000 + 5.7071i

>> c4r=real(c4) % it find real part of c4 %

c4r = 1

>> c4im=imag(c4) % it find imagnary part of c4 %

c4im = 5.7071

>> c=2+2i

>> abs(c) % it is use to find magnitude of c %

ans = 2.8284

*************************************************************

2) Programing in matlab:
2.1) Function:
Note: All programes are written in eiditer window and output will be appear in
commond window.

Q:find root of quardatic equation.

function v = matlab1(a,b,c)
a=input('enter a:')
b=input('enter b:')
c=input('enter c:')
v=(-b)+(sqrt(b^2-4*a*c))/2*a;
disp('the root is ')
end
>> matlab1

enter a:5

8
a=5

enter b:6

b=6

enter c:7

c= 7

the root is

ans = -6.0000 +25.4951i

Q:Write a short script/program that compute a number is even or odd in matlab.


(2019 paper annule)

write the following commonds in editer and then run.

x=input('enter a number');
if rem(x,2)==0
disp('even')
else
disp('odd')
end
out put will be appear in commond window i,e

>> matlab1

enter a number 89

odd

Note: For detial study of progrmming in matlab Read the book "numerical method
with matlab" which is present in class no 519.403 of public library timergara.

*************************************************************

9
3) Matrix in matlab:
2.1) one dimentional matrix:
2.1.1) decleration and acssesing one dimentional
matrix:
>> x=[2 3 4 5] % declearing one dimentional mtarix %

x=2 3 4 5

>> y=[ 1+2i 5 6 2-3i] % declearing one dimentional mtarix %

>> y=[3 : 2 : 23] % declearing one dimentional mtarix with difference 2 %

y=3 5 7 9 11 13 15 17 19 21 23

>> z=[0 pi/4 pi/6 pi/2 2*pi 2*pi/3 2*pi]%declearing one dimentional mtarix%

z = 0 0.7854 0.5236 1.5708 6.2832 2.0944 6.2832

>> y=sin(z) % find sin of z matrix %

y = 0 0.7071 0.5000 1.0000 -0.0000 0.8660 -0.0000

>> x=[5 6 7 8 9 4 3];

>> x(2 : end) % it acsses element of x from 2 to end %

ans = 6 7 8 9 4 3

>> x(3 : -1 : 1) % it acsses element of x from 3 to 1 with difference of -1 %

ans = 7 6 5

>> x(3 : -2 : 1) % it acsses element of x from 3 to 1 with difference of -2 %

ans = 7 5

>> linspace(2, 20, 10) % it decler 10 values from 2 to 2o %

ans = 2 4 6 8 10 12 14 16 18 20

10
>> b=[1 2 3 4 5 6 7 8 9];

>> b=[linspace(2,7,4)] % it decler 4 values from 2 to 7 in matrix b %

b = 2.0000 3.6667 5.3333 7.0000

2.1.2) operation of one dimentional matrix:


>> a=[ 1 23 4 5 6 7];

>> a=(1 : 5)' % it find the transpose of the element from 1 to 5 of matrix a %

a=1

>> c= 1

>> b= c' % it find the transpose of matrix c %

b=1 2 3 4 5

>> a=[ 1 2 3 ];

>> b=complex(a , a) % matrix with complex no element %

b = 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i

>> e=b' % transpose of b and conjugate%

e = 1.0000 - 1.0000i

11
2.0000 - 2.0000i

3.0000 - 3.0000i

>> e=b.' % transpose of b %

e = 1.0000 + 1.0000i

2.0000 + 2.0000i

3.0000 + 3.0000i

>> a=[2 3 4]; b=[6 7 8];

>> c=[b a] % two matrix combine or cencatanation %

c= 6 7 8 2 3 4

>> a=[2 3 4];b=[6 7 8];

>> c=a+2

c=4 5 6

>> c=a+b

c =8 10 12

>> d=2*b

d = 12 14 16

>> a=[2 3 4];b=[6 7 8];

>> d=(2*a)+(3*b)

d = 22 27 32

>> d=(2*a)+(3*b)-1

d = 21 26 31

>> a=[2 3 4];b=[6 7 8];

>> a./b % dot mean divide corresponding element of matrix a by b %

12
ans = 0.3333 0.4286 0.5000

>> 2.^a

ans = 4 8 16

>> a .^2

ans = 4 9 16

>> a=[2 3 4];b=[6 7 8];

>> a .*b %dot mean multiply corresponding element of matrix a by b %

ans = 12 21 32

>> a=[2 3 4];b=[6 7 8];

>> max(a)

ans = 4

>> min(b)

ans = 6

2.2) Multi dimentional matrix:


2.2.1) Decleration and accssesing multi dimentional
matrix:
>> ones(3)

ans = 1 1 1

1 1 1

1 1 1

>> zeros(2,3)

ans = 0 0 0

0 0 0

13
>> a=[2,3,4; 3,45,7; 3,4,5; ]

a=2 3 4

3 45 7

3 4 5

>> size(a) % it find order of a %

ans = 3 3

>> ones(size(a))

ans = 1 1 1

1 1 1

1 1 1

>> eye(4) % identity matrix %

ans = 1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

>> eye(2,4)

ans = 1 0 0 0

0 1 0 0

>> b=1:4

b=1 2 3 4

>> a=[2,3,4; 3,45,7; 3,4,5; ];

>> diag(a)

ans = 2

45

14
5

>> diag(b)

ans = 1 0 0 0

0 2 0 0

0 0 3 0

0 0 0 4

>> diag(b,3)

ans = 0 0 0 1 0 0 0

0 0 0 0 2 0 0

0 0 0 0 0 3 0

0 0 0 0 0 0 4

0 0 0 0 0 0 0

0 0 0 0 0 0 0

0 0 0 0 0 0 0

>> diag(b,-2)

ans = 0 0 0 0 0 0

0 0 0 0 0 0

1 0 0 0 0 0

0 2 0 0 0 0

0 0 3 0 0 0

0 0 0 4 0 0

>> a=[2,3,4; 3,45,7; 3,4,5; ] % declearing matrix %

a= 2 3 4

3 45 7

15
3 4 5

>> b=[2:1:4; 5:1:7; 8:1:10] % declearing matrix, the 1 st row start from 2 upto 4 and
difference 1 in each elements in the row and samilar for 2nd & 3 rd row %

b=2 3 4

5 6 7

8 9 10

>> a(1,2) % accessing element of matrix "a" at row 1 & colomn 2 %

ans = 3

>> a(1) % element of first row & colomn of a %

ans = 2

>> a(3) % element of 3rd row & first column %

ans = 3

2.2.2) operation on multi dimentional matrix:


>> a=[2,3,4;3,4,5;6,7,8]

a =2 3 4

3 4 5

6 7 8

>> b=[2,3,6; 6,7,8; 9,7,2;]

b=2 3 6

6 7 8

9 7 2

>> c=a+b % add a with b %

c=4 6 10

9 11 13

16
15 14 10

>> c=a*b % multiply a with b %

c = 58 55 44

75 72 60

126 123 108

>> inv(b) % find inverse of matrix b %

ans = 1.4 -1.2 0.6

-2 1.6667 -0.66667

0.7 -0.43333 0.13333

>> a=[2 3 4; 5 6 7; 2 3 9 ]

a= 2 3 4

5 6 7

2 3 9

>> 2.^a % each element of a is consider is a power of 2 %

ans = 4 8 16

32 64 128

4 8 512

>> a.^2 % it find the square of each element of a %

ans = 4 9 16

25 36 49

4 9 81

>> a=[2 3 4; 5 6 7; 2 3 9 ]

a= 2 3 4

5 6 7

17
2 3 9

>> a+2 % it add 2 to each element of a %

ans = 4 5 6

7 8 9

4 5 11

>> a*2 % it multiply 2 to each element of a %

ans = 4 6 8

10 12 14

4 6 18

>> a=[2 3 4; 5 6 7; 2 3 9 ]

a= 2 3 4

5 6 7

2 3 9

>> a./2 % each element of a is divided by 2 %

ans = 1.0000 1.5000 2.0000

2.5000 3.0000 3.5000

1.0000 1.5000 4.5000

>> a=[2 3 4; 5 6 7; 2 3 9 ]

a= 2 3 4

5 6 7

2 3 9

>> b=[ 2 33 5; 3 4 5; 7 8 9;]

b = 2 33 5

3 4 5

18
7 8 9

>> a.*b % use for multiplying each element of a with b %

ans = 4 99 20

15 24 35

14 24 81

>> a=[ 1 2 3; 4 5 6; 7 8 9]

a= 1 2 3

4 5 6

7 8 9

>> a(3,3)=0 % set element in 3rd row, 3rd column of a to zero %

a=1 2 3

4 5 6

7 8 0

>> a=[ 1 2 3; 4 5 6; 7 8 9]

a=1 2 3

4 5 6

7 8 9

>> a(2,6)=1 % set element in 2nd row, 6th column of a to 1 %

a= 1 2 3 0 0 0

4 5 6 0 0 1

7 8 9 0 0 0

>> a( : ,4)=4 or =[4 4 4] % set the 4th column of a equal to 4 %

a= 1 2 3 4 0 0

4 5 6 4 0 1

19
7 8 9 4 0 0

>> a(2, :)=3 or =[3 3 3 3 3 3] % set the 2nd row of a equal to 3 %

a= 1 2 3 4 0 0

3 3 3 3 3 3

7 8 9 4 0 0

>> b=a(3:-1:1,1:3)

b=7 8 9

3 3 3

1 2 3

>> a=[ 1 2 3; 4 5 6; 7 8 9]

a= 1 2 3

4 5 6

7 8 9

>> c=[a b(: ,[1 3])]

c=1 2 3 7 9

4 5 6 3 3

7 8 9 1 3

>> c=[ 1 3]

c=1 3

>> b=a(c , c) %b is formed from the first & third rows & first & third columns of a.

b=1 3

7 9

>> b=a(:)

b= 1

20
4

>> c=a(: , :)

c= 1 2 3

4 5 6

7 8 9

>> c(: , 2)=[ ]

c=1 3

4 6

7 9

>> e=reshape(c,2,3) % reshape matrix e into the desired 2 by 3 shape %

e= 1 7 6

4 3 9

>> a=[1 2 3; 1 4 7; 7 8 9]

a= 1 2 3

1 4 7

7 8 9

>> reshape(c,3,4)

21
ans = 1 7 4 0

0 4 8 7

1 0 7 9

>> b=repmat(e(:,2),1,3) % replicate 2nd column into 3 columns %

b= 7 7 7

3 3 3

>> b=repmat(e(:,2),3) % replicate 2nd column into 3 rows and columns %

b= 7 7 7

3 3 3

7 7 7

3 3 3

7 7 7

3 3 3

>> a=[1 2 3; 1 4 7; 7 8 9]

a=1 2 3

1 4 7

7 8 9

>> c=[1 4 7]

c= 1 4 7

>> c(3:4,:)=a(2:3,:)

c=1 4 7

0 0 0

1 4 7

7 8 9

22
>> x=-3:3

x = -3 -2 -1 0 1 2 3

>> abs(x)>1

ans = 1 1 0 0 0 1 1

>> d=x(abs(x)>1)

d = -3 -2 2 3

>> x=[ 7 6 4 3 8 9 3]

>> x=sort(x)

x=3 3 4 6 7 8 9

>> x=sort(x,'descend')

x=9 8 7 6 4 3 3

>> x=sort(x,'ascend')

x=3 3 4 6 7 8 9

>> a=[1 2 3; 1 4 7; 7 8 9];

>> rot90(a) % rotate a at 90 %

ans = 3 7 9

2 4 8

1 1 7

>> rot90(a,2) % rotate a at 2*90 %

ans = 9 8 7

7 4 1

3 2 1

>> x= -3 : 3

x = -3 -2 -1 0 1 2 3

23
>> k=find(x>2) % it find index no of element of x for x>2 %

k= 7

>> x(k) % it find the value of x at index k %

ans = 3

>> a=[1,2,3;1,4,7;7,8,9]

a=1 2 3

1 4 7

7 8 9

>> k=find(a>5) % it find index no of element of a for a>5 %

k= 3

>> a(k) % it find the value of a at index k %

ans = 7

>> a=[ 2 3 4 5; 2 3 6 7; 2 1 3 4 ;]

a= 2 3 4 5

2 3 6 7

2 1 3 4

>> max(a) % it find maximum value in each column of a %

24
ans = 2 3 6 7

>> max(max(a)) % it find max value in ans %

ans = 7

>> min(a) % it find minimum value in each column of a %

ans = 2 1 3 4

>> min(min(a)) % it find min value in ans %

ans = 1

>> a=[ 1 2 3;4 5 6;7 8 9]

a= 1 2 3

4 5 6

7 8 9

>> diag(a)

ans = 1

>> diag(ans) % it form diagnal matrix from ans %

ans = 1 0 0

0 5 0

0 0 9

>> triu(a) % make upper triangular matrix from matrix a %

ans = 1 2 3

0 5 6

0 0 9

25
>> tril(a) % make lower triangular matrix from matrix a %

ans = 1 0 0

4 5 0

7 8 9

>> a=size(a) % find size of a %

a=3 3

>> r=numel(a) % find total no of element of a %

r=2

>> a=[1,0,0;4,5,0;7,8,9]

a= 1 0 0

4 5 0

7 8 9

>> length(a) % return no of rows if maximum, or no of columns of a %

ans = 3

>> c=[1 2 3 4; 1 2 3 4]

c= 1 2 3 4

1 2 3 4

>> length(c)

ans = 4

>> x=[4 5; 6 7;]

x= 4 5

6 7

>> det(x) % it is use for finding determinant of x %

ans = -2.0000

26
>> rank(x) % it is use for finding no of non zero rows in x %

ans = 2

>> y=[4 5; 0 0]

y= 4 5

0 0

>> rank(y)

ans = 1

******************************************************
4) Graphs in matlab:
3.1)two Dimentional graphs:
3.1.1) The plot funtion:
x=linspace(0,2*pi,30)%create 30 values between 0 to 2*pi%
y=sinx
plot(x,y),title('fiager 3.1: sine wave')
fiager 3.1:sine wave
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

*************************************************************
x=-2:0.005:2
y=sin(1./x)
plot(x,y)

27
title('Graph of sin1/x')
text(0,0,'origin')
Graph of sin1/x
1

0.8

0.6

0.4

0.2

0 origin

-0.2

-0.4

-0.6

-0.8

-1
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2

*************************************************************
x=linspace(0,2*pi,30)%create 30 values between 0 to 2*pi%
y=sin(x);
z=cos(x);
plot(x,y,x,z),title('fiager 3.2: sine and cosine')

28
fiager 3.2: sine and cosine
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

*************************************************************
* * x=linspace(0,2*pi,30)%create 30 values between 0 to 2*pi%
y=sin(x);
z=cos(x);
plot(y,x,z,x),title('fiager 3.2: sine and cosine')

fiager 3.2: sine and cosine


7

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

29
*************************************************************
x=-6:6
y1=x.^2
plot(x,y1)
title('parabola')
parabola
40

35

30

25

20

15

10

0
-6 -4 -2 0 2 4 6

*************************************************************

t=0:pi/100:2*pi
x=sin(t)
y=cos(t)
plot(x,y)
text(0,0,'origin')
title('circle')

30
circle
1

0.8

0.6

0.4

0.2

0 origin

-0.2

-0.4

-0.6

-0.8

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

*************************************************************

3.1.2) linestyles,markers and colours:


table of basic commonds for linestyles,markerand colours:
color symbol marker symbol linestyle symbol

blue b point . solid line -


green g circle o dotted line :
red r cross x dash-dot line -.
cyan c plus sign + dashed line --
magenta m asterisk *

yellow y square s

black k diamond d

white w triangle down

31
triangle up ^

triangle left <

triangle right >

pentagram p

hexagram h

*************************************************************
x=linspace(0,2*pi,30);
y=sin(x);
z=cos(x);
plot(x,y,'b p',x,z,'c-',x,1.2*z,'m+')
title('fiager 3.3: linestyles and markers')
%the first plot x,y has blue color and pentagram marker
the 2nd plot has cyan color and solid line the 3 rd plot
has magenta color plus singn marker %
fiager 3.3: linestyles and markers
1.5

0.5

-0.5

-1

-1.5
0 1 2 3 4 5 6 7

***********************************************************
x=linspace(0,2*pi,30);
y=sin(x);

32
z=cos(x);
plot(x,y,x,z)
box off %turn off the axis box%
xlabel('independent variable x') %label horizantal axis%
ylabel('dependent variable y and z')%label verticle axis%
title('fiager 3.4: sine and cos curve,no box')
fiager 3.4: sine and cos curve,no box
1

0.8

0.6
dependent variable y and z

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
independent variable x

*************************************************************
x=linspace(0,2*pi,30);
y=sin(x);
z=cos(x);
plot(x,y,x,z)
box on,grid on %grid mean horizantal & verticle line%
xlabel('independent variable x')
ylabel('dependent variable y and z')
title('fiager 3.4: sine and cos curve,added label')
text(2.5,0.7,'sin(x)')%text function use for label a string
at location 2.5,0.7%

33
fiager 3.4: sine and cos curve,added label
1

0.8
sin(x)
0.6
dependent variable y and z

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
independent variable x

*************************************************************

3.1.3) Multiple plots:


x=linspace(0,2*pi,30);
y=sin(x);
z=cos(x);
plot(x,y,'b-.',x,z,'k-',x,1.2*z,'g+',x,1.2*y,'r--')
xlabel('independent variable x')
ylabel('dependent variable y and z')
title('fiager 3.4: sine and cos curve,multiple plots')

34
fiager 3.5: sine and cos curve,multiple plots
1.5

1
dependent variable y and z

0.5

-0.5

-1

-1.5
0 1 2 3 4 5 6 7
independent variable x

*************************************************************
x=linspace(0,2*pi,30);
y=sin(x);
z=cos(x);
plot(x,y,'b-',x,z,'k-',x,1.2*z,'g+',x,1.2*y,'r-')
xlabel('independent variable x')
ylabel('dependent variable y and z')
title('fiager 3.5: sine and cos curve,multiple plots')
legend('sin(x)','cos(x)','1.2z','1.2y')

35
fiager 3.5: sine and cos curve,multiple plots
1.5
sin(x)
cos(x)
1 1. 2z
1. 2y
dependent variable y and z

0.5

-0.5

-1

-1.5
0 1 2 3 4 5 6 7
independent variable x

*************************************************************
x=1:30
y=11:40
bar(x,y)
title('bar plot')
xlabel('x-axis')
ylabel('y-axis')

36
bar plot
40

35

30

25
y-axis

20

15

10

0
0 5 10 15 20 25 30
x-axis

*************************************************************
x=[4 5 6 7]
y=[2 6 8 12]
pie(x,y)
title('pie plot')

37
pie plot
18%

32%

23%

27%

*************************************************************

3.1.4) Sub plots :


x=linspace(0,2*pi,30);
y=sin(x);
z=cos(x);
a=(2*sin(x)).*cos(x); %dot mean every element multiply%
b=2*sin(x)
subplot(2,2,1) %pick the upper left of 2by2 grid subplot%
plot(x,y), title('fiager 3.6 : sin(x)')
grid %horizantal & verticle lines of the graph%
subplot(2,2,2) %pick the upper right of the 4 subplots%
plot(x,z), title('fiager 3.7: cos(x)')
grid
subplot(2,2,3) %pick the lower left of the 4 subplots%
plot(x,a),title('fiager 3.8 : 2sin(x)cos(x)')
grid
subplot(2,2,4)%pick the lower right of 2by2 grid subplot%
plot(x,b),title('faiger 3.9 : 2sin(x)')
grid

38
fiager 3.6 : sin(x) fiager 3.7: cos(x)
1 1

0.5 0.5

0 0

-0.5 -0.5

-1 -1
0 2 4 6 0 2 4 6

fiager 3.8 : 2sin(x)cos(x) faiger 3.9 : 2sin(x)


1 2

0.5 1

0 0

-0.5 -1

-1 -2
0 2 4 6 0 2 4 6

*************************************************************
3.2) Three dimentional graphs:
3.2.1) Line 3D plots:
t=linspace(0,10*pi)
plot3(sin(t),cos(t),t)
xlabel('sin(t)'),ylabel('cos(t)'),zlabel('t')
text(0,0,0,'0rigin')%fix origin at (0,0,0)%
title('fiager 3.10: helix')

39
fiager 3.10: helix

30

25

20

15
t

10

0
1 0rigin
0.5 1
0 0.5
0
-0.5
-0.5
cos(t) -1 -1 sin(t)

*************************************************************
t=linspace(0,10*pi)
plot3(sin(t),cos(t),t,2*sin(t),2*cos(t),2*t)
xlabel('sin(t)'),ylabel('cos(t)'),zlabel('t')
text(0,0,0'0rigin')
title('fiager 3.10: helix')
grid

40
fiager 3.10: helix

60

50

40

30
t

20

10

0
2 0rigin
1 2
0 1
0
-1
-1
cos(t) -2 -2 sin(t)

*************************************************************
x=-9:9
y=-9:9
z=exp(x.^2+y.^2) %graph of z=f(x,y)%
plot3(x,y,z)
title('fiager 3.11: e^(x^2+y^2)')
grid

41
fiager 3.11: e (x 2+y2 )

10 70
2.5

1.5

0.5

0
10
5 10
0 5
0
-5
-5
-10 -10

*************************************************************
3.2.2) Mesh plots:
mesh(X,Y,Z) draws a wireframe mesh with color determined by Z, so color is
proportional to surface height. If X and Y are vectors, length(X) = n and length(Y) =
m, where [m,n] = size(Z). In this case, (X(j), Y(i), Z(i,j)) are the intersections of the
wireframe grid lines; X and Y correspond to the columns and rows of Z, respectively.
If X and Y are matrices, (X(i,j), Y(i,j), Z(i,j)) are the intersections of the wireframe
grid lines. The values in X, Y, or Z can be numeric, datetime, duration, or categorical
values.
[X,Y]= meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2);
Z = sin(R)./R;
mesh(X,Y,Z)

42
1

0.5

-0.5
10
5 10
0 5
0
-5
-5
-10 -10

*************************************************************
***
x = -2:0.25:2;
y = x;
[X,Y] = meshgrid(x,y);
F = X.*exp(-X.^2-Y.^2);
mesh(X,Y,F)

43
0.5

-0.5
2
1 2
0 1
0
-1
-1
-2 -2

*************************************************************
x=-9:9
y=-9:9
z=exp(x.^2+y.^2)%graph of z=f(x,y)%
[x,y,z]=peaks(12)%peaks(12) mean 12 horizantal and verticle
line which make the surface z%
mesh(x,y,z)
title('fiager 3.11: e^(x^2+y^2)')
grid on

44
fiager 3.11: e (x 2+y2 )

-2

-4

4
2 4
0 2
0
-2
-2
-4 -4

rotation of 3.11

3
3
2
2
1
1
0
0
-1
-1
-2 -2
-3 -3

% it is the rotation of the above fiager and shows peaks(12) clearly %

*************************************************************
x=-9:9

45
y=-9:9
z=-9:9
[x,y,z]=peaks(30)
mesh(x,y,z)
title('fiager 3.12: meshplot')
fiager 3.12: meshplot

10

-5

-10
4
2 4
0 2
0
-2
-2
-4 -4

*************************************************************
x=[1.5:0.125:1.5];
y=[-0.6:0.125:2.8];
[x,y,z]=peaks(51);
z=100.*(y-x).*x.^2+(1-x).^2;
meshz(x,y,z)% mesh plot with zero plane%
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
grid on
title('mesh plot with zero plane')

46
mesh plot with zero plane

6000

4000

2000
z-axis

-2000

-4000

-6000
4
2 4
0 2
0
-2
-2
y-axis -4 -4 x-axis
by Abdullah:
*************************************************************
x=[1.5:0.125:1.5];
y=[-0.6:0.125:2.8];
[x,y,z]=peaks(51);
z=100.*(y-x).*x.^2+(1-x).^2;
meshc(x,y,z)
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
grid on
title('mesh plot with contours')

47
mesh plot with contours

4000

2000
z-axis

-2000

-4000

-6000

2
3
2
0 1
0
-2 -1
-2
y-axis -3 x-axis

*************************************************************
3.2.3) Surface plots:
% In surface there is no space beween the gride line which form it or shaded%
x=-9:9
y=-9:9
z=exp(x.^2+y.^2)%graph of z=f(x,y)%
[x,y,z]=peaks(30)
surf(x,y,z)
text(0,0,0,'origen')
title('fiager 3.11: e^(x^2+y^2)')

48
fiager 3.11: e (x 2+y2 )

10

0
origen

-5

-10
4
2 4
0 2
0
-2
-2
-4 -4

*************************************************************
x=-9:9
y=-9:9
z=exp(x.^2+y.^2)%graph of z=f(x,y)%
[x,y,z]=sphere(30)
surf(x,y,z)
text(0,0,0,'origen')
title('fiager 3.11: e^(x^2+y^2)')

49
fiager 3.11: e (x 2+y2 )

0.5

0
origen

-0.5

-1
1
0.5 1
0 0.5
0
-0.5
-0.5
-1 -1

*************************************************************
x=[1.5:0.125:1.5];
y=[-0.6:0.125:2.8];
[x,y,z]=peaks(51);
z=100.*(y-x).*x.^2+(1-x).^2;
surf(x,y,z)
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
grid on
title('surface like banana')

50
surface like banana

6000

4000

2000
z-axis

-2000

-4000

-6000
4
2 4
0 2
0
-2
-2
y-axis -4 -4 x-axis

*************************************************************
x=[1.5:0.125:1.5];
y=[-0.6:0.125:2.8];
[x,y,z]=peaks(51);
z=100.*(y-x).*x.^2+(1-x).^2;
surfnorm(x,y,z) %norm means normals%
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
grid on
title('surface like banana with normals')

51
surface like banana with normals

6000

4000

2000
z-axis

-2000

-4000

-6000
4
2 4
0 2
0
-2
-2
y-axis -4 -4 x-axis
by Abdullah:
*************************************************************

5) Matrix algebra:
4.1) Basic commond of Algebra:
syms x

>> 3*x + 7*x

ans = 10*x

>> x^2+x^2

ans =2*x^2

>> syms x y

>> p=3*x^2;

>> q=2*x^3;

>> o=p*q % multiply p with q %

o = 6*x^5

52
>> z=p/q % division of p by q %

z = 3/(2*x)

>>syms x

>> factor(6*x^2+18*x-24) % it find factor of ploynomial & numbers %

ans = [ 6, x + 4, x - 1]

>> syms x y

>> F = factor(y^2*x^2,x)

F =[ y^2, x, x]

>> syms x

>>p = x^3 - 2*x + 5;

>>[q, r] = quorem(x^5, p) % it shows quotient & remiender %

q = x^2 + 2

r =- 5*x^2 + 4*x - 10

>> lcm(2*x , 2*x^2) % find least common multiple %

ans =2*x^2

>> gcd(2*x , 2*x^2) % find greatest common deviser %

ans =2*x

4.2) Roots:
% roots of a polynomial p(x) is those values of x which give 0 by putting in p(x) %

Note:
In this chapter ploynomial is represented by row matrix of its cofficients in
decending order e,g 2x2+2x-7 can be written is a=[ 2 2 -7];

Q:Find the root of x+2.

>> a=[1 2]

53
a= 1 2

>> r=roots(a)

r = -2 % root=-2 %

>> x=-2

x = -2

>> x+2

ans= 0

Q:find polynomial p(x) whose root is -2.

>> pp=poly(-2)

pp = 1 2 % p(x)= x+2 %

Q:find the roots of x4-12x3+0x2+25x+116.

>> p=[1 -12 0 25 116]

p = 1 -12 0 25 116

>> r=roots(p)

r = 11.7473 + 0.0000i % roots %

2.7028 + 0.0000i

-1.2251 + 1.4672i

-1.2251 - 1.4672i

Q:find the polynomial whose root is 11.7473+0.0000i ,

2.7028+0.0000i , -1.2251+1.4672i , -1.2251-1.4672i .

>> r=[11.7473+0.0000i 2.7028+0.0000i -1.2251+1.4672i -1.2251-1.4672i]

r = 11.7473 + 0.0000i 2.7028 + 0.0000i -1.2251 + 1.4672i -1.2251 - 1.4672i

>> pp=poly(r)

pp = 1.0000 -11.9999 -0.0015 25.0012 116.0023

54
p(x)=x4-11x3+0x2+25x+166

*************************************************************

4.3)Solution of linear equation using matrix:


Q: solve the equation 2x+3y=2 & 3x+6y=5 in matlab.

>> a=[2,3; 3,6]

a=2 3

3 6

>> b=[2; 5]

b=2

>> x=inv(a)*b

x = -1 % x= -1 & y=1.3333 %

1.3333

*************************************************************

Q: solve the equation 2x+3y+4z=2 ,4x+5y+6z=4 ,8x+9y=5 in matlab.

>> a=[ 2 3 4; 4 5 6; 8 9 0]

a=2 3 4

4 5 6

8 9 0

>> b=[2; 4; 5]

b=2

>> x=inv(a)*b

55
x = 1.3 % x=1.3, y=-0.6, z=0.3 %

-0.6

0.3

By abdullah.
*************************************************************

6) limits , derivatives and integrations:


5.1) limits:
>> syms x h;

>> f=sin(x)/x;

>> limit(f,x,0) % it find the limit of f when x approches to 0 %

ans = 1

>> syms x;

>> f=1/x;

>> limit(f,x,0,'right') % it find the limit of f when x approches to 0 from the right %

ans = Inf

>> limit(f,x,0,'left') % it find the limit of f when x approches to 0 from the left %

ans = -Inf

>> syms x;

>> f=(abs(x))/x;

>> f=limit(f,x,1)

f=1

>> syms y;

>> f=abs(y)/sin(y);

>> f=limit(f,y,1)

f = 1/sin(1)

56
>> syms n;

>> f=(factorial(n))/n^(n);

>> f=limit(f,n,inf)

f=0

5.2) derivatives:
>> syms x;

>> f=sin(x^2);

>> f = diff(f , x) % find derivative of f(x) %

f = 2*x*cos(x^2)

>> f2=f(2) % find the derivative of f(x) at x=2 %

f2 = 4*cos(4)

>> syms x t

>> diff(sin(x*t^2)) % find derivative of sin(xt 2) w r t x %

ans = t^2*cos(t^2*x)

>> diff(sin(x*t^2),t) % find derivative of sin(xt 2) w r t t %

ans = 2*t*x*cos(t^2*x)

>> syms t

>> d4=diff(t^6,4) % it find the 4th derivative of t^6 %

d4 = 360*t^2

>> syms x y

>> diff(x*cos(x*y),y,2) % it find the 2nd derivative of xcos(xy) w r t y %

ans = -x^3*cos(x*y)

5.3) integrations:
>> syms x; % it use for symbol %

>> f=sin(x);

57
>> F=int(f,x) % it find integration of f w.r.t x %

F = -cos(x)

>> F=int(f,x,2,4) % it find definite integration of f w.r.t x from 2 to 4 %

F = cos(2) - cos(4)

>> F=int(f,x,0,2*pi) % it find definite integration of f w.r.t x from 0 to 2pi %

F=0
*************************************************************

paper of Matlab (2019) University of


Malakand:
part 'II'
Q2. Write commands for performing the following in Matlab/Maple/Mathematica.

a) Define two 2 by 2 matrices.

b) take the transpose of the first and multiply it with the other element wise.

Q3. How the following function are performed in Matlab/Maple/Mathematica.

a) Derivative of 6x2+18x-24.

b) Factorization of 6x2+18x-24.

c) lim x to 0 x/|x|=1.

Q4. list Five built in functions 0f Matlab/Maple/Mathematica and explian thier

with examples.

Q5. Write a short script/program that compute a number is even or odd in matlab.

write the following commonds in editer and then run.

x=input('enter a number');
if rem(x,2)==0
disp('even')
else
disp('odd')

58
end
out put will be appear in commond window i,e

>> matlab1

enter a number 89

odd

*************************************************************

The end
by Abdullah (Student of B S Maths 4th semester GPG college
Timergara lower dir).

Refrences:
1)Book name "Mastering with matlab"
Author name "Duane Hanselman"
2)Book name "Numerical methods with Matlab"
the above books is persent in 'English section' of public
library timergara at class no '519.403'.
date: 9/5/2019
[email protected]:

59
*hj****hggjkghjhjhjkhjkjkhjkhjkhkghghjghjghjjjjjjjjtrianglejjjkjkjkjljl

mncvbncbcbcvnxbvhjhknmbnmcbnbnb>> x=linespace(0,2*pi,30)

Undefined fhjfjkshunction or variable 'linespace'.

Didghghgjkggggggh you mean:

>> x=linspace(0,2*pi,30)

x=

Columns 1 through 10

0 0.2167 0.4333 0.6500 0.8666 1.0833 1.3000 1.5166 1.7333


1.9500

Columns 11 through 20

2.1666 2.3833 2.5999 2.8166 3.0333 3.2499 3.4666 3.6832 3.8999


4.1166

Columns 21 through 30

4.3332 4.5499 4.7666 4.9832 5.1999 5.4165 5.6332 5.8499 6.0665


6.2832

60
>> x=linspace(0,2*pi,30);

>> y=sin(x);

>> plot(x,y),title(fiager 3.1:sine wave )

plot(x,y),title(fiager 3.1:sine wave )

Error: Invalid expression. Check for missing multiplication operator, missing or


unbalanced

delimiters, or other syntax error. To construct matrices, use brackets instead of


parentheses.

>> plot(x,y),title('fiager 3.1:sine wave' )

>>

>>

>

61
62
63
64
65
66

You might also like