Documente Academic
Documente Profesional
Documente Cultură
KURSUS
TAJUK
: PERISIAN MATLAB
NAMA
NO MATRIK
%
%
%
%
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
PERISIAN MATHLAB
% 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
% 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)
m = ones(4)
n = ones(5,5)
p = eye(5,5)
p1= eye(5)
p2= eye(4,4)
q = p'
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)
a
t = size(a)
PERISIAN MATHLAB
Example 6:
% clear screen
% clear memory
w = -8 : 0.16 : 8
volt = sin(w)
amp = cos(w)
plot(w,volt)
plot(w,volt,w,amp,volt,amp)
title('chelsea vs arsenal')
xlabel('masa')
ylabel('voltan')
legend('voltan','arus')
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')
y = 5*w + 20
subplot(2,3,2)
plot(w,y)
PERISIAN MATHLAB
Example 7:
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)
PERISIAN MATHLAB
Example 8:
% 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)
g
j = g(2:3)
k = g(2,3)
g
m = g( 2 , : )
mm= g( : , 3 )
g
n = g( : , 2:4)
g
nn= g( : , 1:2:4)
g
p= g(1:2:4,1:3:4)
% 1 3 5x , 1 4 7x
PERISIAN MATHLAB
Example 9:
a(2,3) = 0
size (a)
a(2,6) = 19
a(2,:) = 88
a(:,3) = 77
a(: , 2:4)= 55
u = size(a)
a(2,:)=[]
v=size(a)
PERISIAN MATHLAB
Example 10:
% Name : Kamal Saravanan
% Date : 06/12/2015
% Title: Execise 1
clear
clc
PERISIAN MATHLAB
Example 11:
% Name : Kamal Saravanan
% Date : 06/12/2015
% Title: Execise 2
clear
clc
PERISIAN MATHLAB
1
0
Example 12:
% Name : Kamal Saravanan
% Date : 06/12/2015
% Title: Execise 3
clear
clc
%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