Sunteți pe pagina 1din 12

DDPE 3802 PERISIAN KEJURUTERAAN

KURSUS

: DDPE3802 PERISIAN KEJURUTERAAN

TAJUK

: PERISIAN MATLAB

NAMA

NO MATRIK

KAMAL SARAVANAN A/L VENUGOPALDX126118DPKD04

Example 1: Sample of scalar addition and subtraction done it mathlab

% Name : Kamal Saravanan


% Date : 21/11/2015
% Title: My first m file
% Comment Statement
a=5
b=6
cacing=a+b
dol=cacing-4

%
%
%
%

Defining variable a
Defining variable b
Scalar addition
Scalar substraction

Example 2: Calculating the area with the input that is given by the user
% Name : Kamal Saravanan
% Date : 21/11/2015
% Title: My first matlab program
tapak = input('Masukkan nilai tapak->') % Request user to key input
tinggi = input('Masukan nilai tinggi->')
luas= 1/2*tapak*tinggi
perimeter=tapak*2+tinggi*2

% scalar division and multiplication


% Scalar multiplications

PERISIAN MATHLAB

Example 3: To perform matrix functions in matlab program

% Name : Kamal Saravanan


% Date : 21/11/2015
% Title: Matrices and vectors
clc
clear

% clear screen
% clear memory

a = [1 2 3 4]
% row vector 1x4
b=6
% scalar
c = [0.2 sqrt(2) exp(5)/6] % using equations
d = [1,2,3,4]
% row vector 1x4
e = [1;2;3;4]
% column vector 4x1
f = [1
% column vector 4x1
2
3
4]
a
g = a'
% transpose a
j = [1,2,3;4,5,6]
% matric 2x3
k = [1 2 3;4,5,6]
% matric 2x3
m = [1 2 3
% matric 2x3
4 5 6]
n = m'
% transpose m from matric 2x3 to 3x2

PERISIAN MATHLAB

Example 4: To understand the construction of matrix and its functions

% Name : Kamal Saravanan


% Date : 21/11/2015
% Title: Matrix Construction
clc
clear
a = [1 2 3 4]
b=1:4
c = 1 : 100
d = 1 : 0.5 : 4
e = 1 : 0.7 : 4
f = rand(3)
g = rand(2,3)

% clear screen
% clear memory
% start:end
% start:end
% start:increment:end
% start:increment:end but will stop at increment 3.8
% random value from 0-1 and in matric 3x3
% random value from 0-1 and in matric 2x3

h = zeros(4)
j = zeros(4,4)
k = zeros(2,4)

% zeroes matrix and in matric 4x4


% zeroes matrix and in matric 4x4
% zeroes matrix and in matric 2x4

m = ones(4)
n = ones(5,5)

% ones matrix and in matric 4x4


% ones matrix and in matric 5x5

p = eye(5,5)
p1= eye(5)
p2= eye(4,4)

% identity matrix and in matric 4x6


% identity matrix and in matric 5x5
% identity matrix and in matric 2x9

q = p'

% transpose the matrix

PERISIAN MATHLAB

Example 5:
% Name : Kamal Saravanan
% Date : 21/11/2015
% Title: Matrix Operation
clc %clear screen
clear %clear memory
a = [1 2 3 ; 4 5 6]
b = [7 8 9 ; 1 2 3]
c = [4 5 ; 6 7 ; 8 9]
d = [1 2 ; 3 4]

%
%
%
%

matrix
matrix
matrix
matrix

2x3
2x3
3x2
2x2

whos
a
e=a+3

%scalar addition

a
b
f=a+b
g=a-b

% matrix addition
% matrix substraction

a
h = a*2
j = a/5

% scalar multiplication
% scalar division

size(a)
size(c)
whos
k = a*c
m = a/b
n = d\b

% matrix multiplication
% right division
% left division

p = d*d*d*d*d
q = d^5

% multiplication of 5 times
% exponentiation power of 5

r = inv(d)
r = d^-1
s = det(d)

% inverse value of matrix d


% inverse value of matrix d @ 1/d
% determinant of matrix d

a
t = size(a)

% shows the size of the matric

PERISIAN MATHLAB

Example 6:

% Name : Kamal Saravanan


% Date : 21/11/2015
% Title: matlab plotting
clc
clear

% clear screen
% clear memory

w = -8 : 0.16 : 8
volt = sin(w)
amp = cos(w)

% sinusoidal function for volt

plot(w,volt)

% function to plot graph (x,y) or w vs volt

plot(w,volt,w,amp,volt,amp)
title('chelsea vs arsenal')
xlabel('masa')
ylabel('voltan')
legend('voltan','arus')

% displays title for the graph


% displays the name of x-axis
% displays the name of y-axis
% displays the legend of the graph

subplot(2,3,1)
plot(w,volt)
subplot(2,3,4)
plot(w,amp)
mu = w.*w
subplot(2,3,6)
plot(w,mu)
xlabel('masa')
ylabel('voltan')

% plot with graphs at 2x3 and graph no 1


%123
%456
% plot with graphs at 2x3 and graph no 4

% plot with graphs at 2x3 and graph no 6


% displays the name of x-axis
% displays the name of y-axis

y = 5*w + 20
subplot(2,3,2)
plot(w,y)

PERISIAN MATHLAB

Example 7:

% Name : Kamal Saravanan


% Date : 06/12/2015
% Title: Array constructions
clear
clc

% to clear the all the data and variables


% to clear the screen but keep variables

a=1:5
b=1:1:5
c = 1 : 0.7 : 5
d = linspace(1,5,5)
e = linspace(1,5,6)
f = 1 : 0.8 : 5
g = logspace (-1,2,10)

% start : end
% start : inc : end
% start : inc : end
% generate liner spaced vectors
% (start,end,no of element)
% (start,end,no of element)

%Tittle: array addressing


h=3:8
increment is by one
g = h(2)
j = h(3) + h(1)
h
k = h(2:4)
h
m = h(1:3:5)
mm= h(2:3:6)
mmm=h(2:2:4)
h
n = h(5:-2:1)
h
p = 2:5
q = h(2:5)
r = h([ 6 2 5 1 2 ])

% if no increment is stated, default


% to call 2nd value from array h
% addition of values from array h
% to call the element 2 - 4 of array h
% element 1 1+3=4 4+3=7x
% element 2 2+3=5 5+3=8x
% start : inc : end
% 2 4 x6 8 10
%5 3 1 -1
% value 2 to 5
% element 2 to 5 from h

PERISIAN MATHLAB

Example 8:

% Name : Kamal Saravanan


% Date : 06/12/2015
% Title: Two dimensional array
clear
clc

% to clear the all the data and variables


% to clear the screen but keep variables

% Concatenation
a = [1 2 ; 3 4]
b = rand(2,3)
c = [a b]
d = [a' ones(2,1)]

% matrics 2x2
% random matrics 2x3 with no from 0 -1
% combines the matrics a & b
% combines transpose of matric a with 2x1 matric ones

e = 3:8
% row vector of e from 3 to 8
f = [e(3:6) ; e(1:4)]
% combine sub set e 3-6 and 1-4
% Two dimensional array addressing
g = magic(4)
coloumns
h = g(14)
hh= g(12)

% returns the 14th element by coloumns


% returns the 12th element by coloumns

g
j = g(2:3)
k = g(2,3)

% returns the 2 & 2 element


% returns the element from row 2 and coloum 3

g
m = g( 2 , : )
mm= g( : , 3 )

% return all the values from row no 2


% return all the values from coloumn no 3

g
n = g( : , 2:4)

% returns all row from coloumn 2 to 4

g
nn= g( : , 1:2:4)

% n num matrics with equal sum of row and

% returns values from coloumn 1 and 3 (all, 1 3 5x)

g
p= g(1:2:4,1:3:4)
% 1 3 5x , 1 4 7x

PERISIAN MATHLAB

Example 9:

% Name : Kamal Saravanan


% Date : 06/12/2015
% Title: Manipulationg array
clear
clc
a = magic(3)
b = a(2,3)

% to clear the all the data and variables


% to clear the screen but keep variables
% 3x3 matrics with equal sum of row and coloumns
% assign value from row 2 and columns to of a to b

a(2,3) = 0

% assign value from row 2 and columns to of a with 0

size (a)

% returns the size of matrics a

a(2,6) = 19

% assign value 19 to row 2 and coloumn 6. New row


% and coloumns are with 0

a(2,:) = 88

% all of row 2 replaced with value 88

a(:,3) = 77

% all of coloumn 3 replaced with value 88

a(: , 2:4)= 55

% all row , col 2 3 4

u = size(a)
a(2,:)=[]
v=size(a)

PERISIAN MATHLAB

Example 10:
% Name : Kamal Saravanan
% Date : 06/12/2015
% Title: Execise 1
clear
clc

% to clear the all the data and variables


% to clear the screen but keep variables

% Generate the following arrays x, y & z


x = [ -2 0 0.3 4 -10 6]
y = [1 -4.5 0 7 ]
z = [ 2 -3 0.6 5 0]
z = z'
% Problem 1a
% Obtain the size of arrays x,y and z
size(x)
size(y)
size(z)
% Problem 1b
% Extract the third element of x
x
c = x(3)
% Problem 1c
z
d = z(4)
% Problem 1d
y
e = y(2:4)
% Problem 1e
x
f = x(1:2:5)
% Problem 1f
y
g = y ([3 2 4])
% Problem 1g
z
g = z ([5 4 3])

PERISIAN MATHLAB

Example 11:
% Name : Kamal Saravanan
% Date : 06/12/2015
% Title: Execise 2
clear
clc

% to clear the all the data and variables


% to clear the screen but keep variables

% Generate the following arrays x & y


x = [ -2.5 0.1 0 10 -5]
y = [ 20 -5 6 8 ]
% Problem 2a
% Define z = y'
y
z = y'
% Problem 2b
% Define p = [x y]
x
y
p = [x y]
% Problem 2c
% Delete the third element of x
x
x(3)=[]
% Problem 2d
% Replace the second element of y with -2
y
y(4)=4
% Problem 2e
% Define q = [x' y']
x
y
q = [x' y']

PERISIAN MATHLAB

1
0

Example 12:
% Name : Kamal Saravanan
% Date : 06/12/2015
% Title: Execise 3
clear
clc

% to clear the all the data and variables


% to clear the screen but keep variables

%Exercise 3a
a = [ -3 5 ; 4 8]
b = [ 10 ; 5 ]
x = inv(a)*b
%Exercise 3b
a = [ -2 10 3 ; 4 -5 6 ; 5 2 -1]
b = [ 24 ; -5 ; 10 ]
x = inv(a)*b

%Exercise 3c
a = [ 4 2 -3 ; -8 -4 6 ; 5 2 -10]
b = [ 0 ; 5 ; 0]
x = inv(a)*b
%Exercise 3d
a = [ -4 1 3 0 ; 1 -2 5 -3 ; 6 1 0 -1 ; 0 5 3 -4]
b = [ 0 ; 3 ; 5 ; 0]
x = inv(a)*b

PERISIAN MATHLAB

1
1

S-ar putea să vă placă și