Tampilkan postingan dengan label MATLAB. Tampilkan semua postingan
Tampilkan postingan dengan label MATLAB. Tampilkan semua postingan

Selasa, 14 Agustus 2018

Belajar Dasar Convolution Dengan Matlab

Berikut ini yaitu script MATLAB untuk convolution tanpa memakai function conv().

x = input('First signal ');
h = input('Second signal ');
a = length(x);
b = length(h);
n = a + b - 1;
y = zeros(1,n);
l = 1:n;
c = 0:a - 1;
d = 0:b - 1;
for i = 0:n
    for j = 0:n
        if((i - j + 1)>0 && (i - j + 1)<=b && (j + 1)<=a)
            y(i+1) = y(i+1) + x(j+1).*h(i - j +1);
        end
    end
end
disp(y);
subplot(3,1,1)
stem(c,x)
grid on
ylabel('magnitude')
title('\bf First Input')
subplot(3,1,2)
stem(d,h)
grid on
ylabel('magnitude')
title('\bf Second Input')
subplot(3,1,3)
stem(l,y)
grid on
ylabel('magnitude')
xlabel('t')
title('\bf Output')





Sumber http://lang8088.blogspot.com

Matlab Script Fft Sederhana

Berikut ini yakni script MATLAB untuk melaksanakan transformasi Fourier signal discrete tanpa memakai function fft().

x=input('Input signal: ');
len=length(x);

y=zeros(1,len);%create an array
yabs=zeros(1,len);%this line is optional, for determining its absolute value
for k=1:len
    for n=1:len
        w=exp(-1i*2*pi*(k-1)*(n-1)/len);
        a=x(n)*w;
        y(k)=y(k)+a;
    end
    yabs(k)=abs(y(k));%this line is optional
end

yt=fft(x);%this one just for comparation

disp(y)
disp(yabs)%output's absolute value
disp(yt)%fft's output
figure(1)
subplot(3,1,1);
stem(y);
subplot(3,1,2);
stem(yabs);
subplot(3,1,3);
stem(yt);


Berikut ini yakni hasil plot signal.


Sumber http://lang8088.blogspot.com

Selasa, 07 Agustus 2018

Matlab: Basic To Control

Berikut ini yakni script MATLAB untuk dasar kendali dengan PID.



clear all, close all, clc;

%% Define the plant

P = tf([1],[1 0 -9]);
% transfer function of the plant
% P =
%      1
%   -------
%   s^2 - 9
pole(P)
% yields s = 3 and s = -3
% thus the plant is not stable

%% The PID controller

Kp = 90;
Ki = 60;
Kd = 30;
% taking into Laplace form
% K = Kd*s + Kp + Ki/s


K = tf([Kd Kp Ki],[1 0]);
% PID controller
% K =
%
%   30 s^2 + 90 s + 60
%   ------------------
%           s
CP = series(K,P);
% the open-loop equals to:
% CP = K*P

CL = feedback(CP,1);
% closed-loop (CL) feedback of the system
pole(CL);
% yields
% s = -27.0919 + 0.0000i
% s =  -1.4540 + 0.3170i
% s =  -1.4540 - 0.3170i

%% Verify the stablility of CL

if isstable(CL,1)
    'Closed-loop is stable'
else
    'Closed-loop is not stable'
end

%% Step response of the system

t = 0:0.1:5;

step(CL,t)
Closed-loop system tersebut stabil. Berikut ini yakni step-response.



Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Minggu, 05 Agustus 2018

Matlab: State Space Observability And Controllability

Berikut ini ialah script MATLAB untuk menguji apakah sistem observable juga controllable.



clear all, close all, clc;

%% Define the matrices

A = [1 2;
    7 2]
% A the state matrix

B = [1; 1]
% B the input vector

C = [1 0]
% C the output vector

D = zeros(size(C,1), size(B,2))
% D the feedforward

eig(A)
% eigenvalues of A
% -2.2749
% 5.2749
% it is not stable

%% Verify the controllability and observability

Cm = ctrb(A,B)
% Controllability matrix

Om = obsv(A,C)
% Observability matrix

if rank(Cm) == size(A,1)
    'It is controllable'
else
    'It is not controllable'
end

if rank(Om) == size(A,1)
    'It is observable'
else
    'It is not observable'
end

Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Sabtu, 04 Agustus 2018

Matlab: Transform State Space Transfer Function

Berikut ini ialah script MATLAB untuk mengubah dari state space menjadi transfer function.



clear all, close all, clc;

%% State space representation
A = [-11 -18;
    1 0]
% the state matrix

B = [1; 0]
% the input vector

C = [0 1]
% the output vector

D = zeros(size(C,1),size(B,2))
% the feedforward

%% Verify the controllability and observability

Cm = ctrb(A,B)
% Controllability matrix

Om = obsv(A,C)
% Observability matrix

if rank(Cm) == size(A,1)
    'It is controllable'
else
    'It is not controllable'
end

if rank(Om) == size(A,1)
    'It is observable'
else
    'It is not observable'
end

%% Transform into transfer function

[num,den] = ss2tf(A,B,C,D)
sys = tf(num,den)
pole(sys)
% yields
% s = -9
% s = -2

step(sys)
% plot the step response
Berikut ini ialah step response dari sistem tersebut.


Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Matlab: Pole-Placement State Space

Berikut ini yakni script MATLAB untuk state feedback controller.



clear all, close all, clc;

%% Define the system

A = [1 0 2;
    2 3 0;
    1 2 3]
% the state matrix

B = [1; 0; 0]
% the input vector

eig(A)
% the eigenvalues are
% 4.7511 + 0.0000i
% 1.1244 + 1.0251i
% 1.1244 - 1.0251i
% it is not stable

%% The pole-placement

desiredPoles = -0.5*ones(3,1)
% desired poles = [-0.5; -0.5; -0.5]

K = acker(A,B,desiredPoles)
% determine the gain K
% based on the Ackermann's formula
% yields
% K = [8.5000 13.0156 12.7188]

%% State feedback controller

sys = ss(A-B*K, eye(size(A)), eye(size(A)), eye(size(A)))

eig(sys.a)
% the eigenvalues are
% -0.5000 + 0.0000i
% -0.5000 + 0.0000i
% -0.5000 - 0.0000i

x0 = [0.1; 0.1; 0.1]
% initial state

t = 0:0.01:25;
% the time duration

x = initial(sys,x0,t);
% response of the state-space
% with initial condition x0
% and time duration t

x1 = [1 0 0]*x';
x2 = [0 1 0]*x';
x3 = [0 0 1]*x';
% the state variables


%% Plot the state variables

subplot(3,1,1); plot(t,x1), grid
title('Response to initial condition')
ylabel('x1')

subplot(3,1,2); plot(t,x2), grid
ylabel('x2')

subplot(3,1,3); plot(t,x3), grid
ylabel('x3')
xlabel('t (seconds)')

Berikut ini ialah plot response dari state variable.


Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Jumat, 03 Agustus 2018

Matlab: Linear-Quadratic Regulator Design For State Space Systems

Berikut ini yaitu script MATLAB untuk LQR (Linear-Quadratic Regulator) controller.



clear all, close all, clc;

%% Define the system

A = [1 0 2;
    2 3 0;
    1 2 3]
% the state matrix

B = [1; 0; 0]
% the input vector

eig(A)
% the eigenvalues are
% 4.7511 + 0.0000i
% 1.1244 + 1.0251i
% 1.1244 - 1.0251i
% it is not stable

%% LQR design

Q = eye(size(A))

R = 1

K = lqr(A,B,Q,R)
% K =
%    14.5461   24.4004   41.9475

%% State feedback controller

sys = ss(A-B*K, eye(size(A)), eye(size(A)), eye(size(A)))

eig(sys.a)
% the eigenvalues are
% -4.7742 + 0.0000i
% -1.3860 + 1.1477i
% -1.3860 - 1.1477i
% it is stable

x0 = [5; 5; 5]
% initial state

t = 0:0.01:5;
% the time duration

x = initial(sys,x0,t);
% response of the state-space
% with initial condition x0
% and time duration t

x1 = [1 0 0]*x';
x2 = [0 1 0]*x';
x3 = [0 0 1]*x';
% the state variables

%% Plot the state variables

subplot(3,1,1); plot(t,x1), grid
title('Response to initial condition')
ylabel('x1')

subplot(3,1,2); plot(t,x2), grid
ylabel('x2')

subplot(3,1,3); plot(t,x3), grid
ylabel('x3')
xlabel('t (seconds)')

Berikut ini ialah plot response dari state variable.


Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Matlab: Bode Plot Diagram

Berikut ini yaitu script MATLAB untuk analisis closed-loop dengan Bode plot diagram.

clear all, close all, clc;

%% Define the system

num = 1;
den = [1 3 5 7];
P = tf(num,den);
% define the plant

Kp = 3;
Ki = 5;
Kd = 7;

K = tf([Kd Kp Ki],[1 0])
% define the PID controller

CL = feedback(series(K,P),1)
% closed-loop system (CL)

%% Bode analysis

[Gm1,Pm1,Wcg1,Wcp1] = margin(P)
% Gain margin and Phase margin of the plant
% Gm1 = 8.0004
% Pm1 = inf

[Gm2,Pm2,Wcg2,Wcp2] = margin(CL)
% Gain margin and Phase margin of CL
% Gm2 = Inf
% Pm2 = 93.3738

bode(P),grid
hold on
bode(CL)
legend('Plant','Closed-loop system')

Berikut ini ialah Bode plot diagram.


Lihat juga tentang.
Kendali state space MATLAB.

Untuk klarifikasi perihal Bode plot sanggup dilihat pada artikel berikut.
Sumber http://lang8088.blogspot.com

Matlab: Bode State Space

Berikut ini yaitu script MATLAB untuk Bode plot diagram state space.

clear all, close all, clc;

%% Define the system

A = [1 0 2;
    2 1 3;
    4 2 1]
% state matrix

B = [1; 0; 0]
% input vector

C = [1 0 0]
% output

D = zeros(size(C,1),size(B,2))
% feedforward

%% Verify the controllability and observability

Cm = rank(ctrb(A,B))
% controllability matrix

Om = rank(obsv(A,C))
% observability matrix

if Cm == size(A,1)
    'It is controllable'
else
    'It is not controllable'
end

if Om == size(A,1)
    'It is observable'
else
    'It is not observable'
end

%% Bode plot diagram

sys = ss(A,B,C,D)
% state space system

margin(sys)

Berikut ini ialah Bode plot diagram.

Lihat juga tentang.
Kendali state space MATLAB.

Untuk klarifikasi wacana Bode plot sanggup dilihat pada artikel berikut.
Sumber http://lang8088.blogspot.com

Kamis, 02 Agustus 2018

Matlab: Control Loop

Catatan singkat perihal control loop di MATLAB. Berikut ini yaitu script MATLAB untuk merancang controller menurut kondisi loop yang diinginkan, loop shaping.

clear all, close all, clc;

%% The transfer function

P = tf([1],[1 0 2 5])
% the plant
% P =
%         1
%   -------------
%   s^3 + 2 s + 5

pole(P)

desiredLoop = tf([10],[1 0])
% desired loop
%   10
%   --
%   s

K = desiredLoop/P
% Gain
% K =
%
%   10 s^3 + 20 s + 50
%   ------------------
%           s

%% Closed-loop system

CL = feedback(series(K,P),1)

t = 0:0.01:5;

step(P,t), grid
hold on;
step(CL,t)
legend('Plant', 'Closed-loop system')

Berikut ini yaitu step response dari plant dan closed-loop.

Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Matlab: State Space Transfer Function

Berikut ini ialah script MATLAB untuk merancang transfer function suatu plant dari model state space.



clear all, close all, clc;

%% The transfer function

A = [1 2; 0 3]
% state matrix

B = [1; 2]
% input vector

C = [1 0]
% output vector

D = zeros(size(C,1),size(B,2))
% feedforward

%% Verify observability and controllability

Om = obsv(A,C)

Cm = ctrb(A,B)

if rank(Om) == size(A,1)
    'It is observable'
else
    'It is not observable'
end
 
if rank(Cm) == size(A,1)
    'It is controllable'
else
    'It is not controllable'
end

%% The transfer function

[num,den] = ss2tf(A,B,C,D)

sys = tf(num,den)

eigenValues = eig(A)
% The eigen values are 3 and 1

poles = pole(sys)
% 3 and 1

Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Rabu, 01 Agustus 2018

Matlab: Root Locus

, catatan kali ini perihal root locus MATLAB.
Berikut ini yaitu script MATLAB untuk plot root locus.

clear all, close all, clc;

%% Transfer function

num = 1;
den = [1 4 3];
sys = tf(num,den);
% the plant
Kp = 100;
Ki = 50;
Kd = 25;
PID = tf([Kd Kp Ki],[1 0]);
% PID controller

%% Closed-loop system

CL = feedback(PID*sys,1);
% Closed-loop system

t = 0:0.01:5;
% time interval

figure(1)
step(sys);
hold on;
step(CL,t);
legend('Initial plant','Closed-loop system');
stepinfo(CL)
% plot the step response in get the information
%
%        RiseTime: 0.0883
%    SettlingTime: 0.1625
%     SettlingMin: 0.9038
%     SettlingMax: 0.9938
%       Overshoot: 0
%      Undershoot: 0
%            Peak: 0.9938
%        PeakTime: 0.2934

[R,K] = rlocus(CL);
% R complex root locations for the gains K

figure(2)
rlocusplot(CL)
pole(CL)
% root locus plot
%
%  -24.9525
%   -3.4701
%   -0.5775

Berikut yaitu plot root locus dan step response.



Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Rabu, 04 Juli 2018

Belajar Dasar Convolution Dengan Matlab

Berikut ini yaitu script MATLAB untuk convolution tanpa memakai function conv().

x = input('First signal ');
h = input('Second signal ');
a = length(x);
b = length(h);
n = a + b - 1;
y = zeros(1,n);
l = 1:n;
c = 0:a - 1;
d = 0:b - 1;
for i = 0:n
    for j = 0:n
        if((i - j + 1)>0 && (i - j + 1)<=b && (j + 1)<=a)
            y(i+1) = y(i+1) + x(j+1).*h(i - j +1);
        end
    end
end
disp(y);
subplot(3,1,1)
stem(c,x)
grid on
ylabel('magnitude')
title('\bf First Input')
subplot(3,1,2)
stem(d,h)
grid on
ylabel('magnitude')
title('\bf Second Input')
subplot(3,1,3)
stem(l,y)
grid on
ylabel('magnitude')
xlabel('t')
title('\bf Output')





Sumber http://lang8088.blogspot.com

Selasa, 03 Juli 2018

Matlab Script Fft Sederhana

Berikut ini yakni script MATLAB untuk melaksanakan transformasi Fourier signal discrete tanpa memakai function fft().

x=input('Input signal: ');
len=length(x);

y=zeros(1,len);%create an array
yabs=zeros(1,len);%this line is optional, for determining its absolute value
for k=1:len
    for n=1:len
        w=exp(-1i*2*pi*(k-1)*(n-1)/len);
        a=x(n)*w;
        y(k)=y(k)+a;
    end
    yabs(k)=abs(y(k));%this line is optional
end

yt=fft(x);%this one just for comparation

disp(y)
disp(yabs)%output's absolute value
disp(yt)%fft's output
figure(1)
subplot(3,1,1);
stem(y);
subplot(3,1,2);
stem(yabs);
subplot(3,1,3);
stem(yt);


Berikut ini yakni hasil plot signal.


Sumber http://lang8088.blogspot.com

Rabu, 27 Juni 2018

Matlab: Basic To Control

Berikut ini yakni script MATLAB untuk dasar kendali dengan PID.



clear all, close all, clc;

%% Define the plant

P = tf([1],[1 0 -9]);
% transfer function of the plant
% P =
%      1
%   -------
%   s^2 - 9
pole(P)
% yields s = 3 and s = -3
% thus the plant is not stable

%% The PID controller

Kp = 90;
Ki = 60;
Kd = 30;
% taking into Laplace form
% K = Kd*s + Kp + Ki/s


K = tf([Kd Kp Ki],[1 0]);
% PID controller
% K =
%
%   30 s^2 + 90 s + 60
%   ------------------
%           s
CP = series(K,P);
% the open-loop equals to:
% CP = K*P

CL = feedback(CP,1);
% closed-loop (CL) feedback of the system
pole(CL);
% yields
% s = -27.0919 + 0.0000i
% s =  -1.4540 + 0.3170i
% s =  -1.4540 - 0.3170i

%% Verify the stablility of CL

if isstable(CL,1)
    'Closed-loop is stable'
else
    'Closed-loop is not stable'
end

%% Step response of the system

t = 0:0.1:5;

step(CL,t)
Closed-loop system tersebut stabil. Berikut ini yakni step-response.



Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Minggu, 24 Juni 2018

Matlab: State Space Observability And Controllability

Berikut ini ialah script MATLAB untuk menguji apakah sistem observable juga controllable.



clear all, close all, clc;

%% Define the matrices

A = [1 2;
    7 2]
% A the state matrix

B = [1; 1]
% B the input vector

C = [1 0]
% C the output vector

D = zeros(size(C,1), size(B,2))
% D the feedforward

eig(A)
% eigenvalues of A
% -2.2749
% 5.2749
% it is not stable

%% Verify the controllability and observability

Cm = ctrb(A,B)
% Controllability matrix

Om = obsv(A,C)
% Observability matrix

if rank(Cm) == size(A,1)
    'It is controllable'
else
    'It is not controllable'
end

if rank(Om) == size(A,1)
    'It is observable'
else
    'It is not observable'
end

Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Matlab: Transform State Space Transfer Function

Berikut ini ialah script MATLAB untuk mengubah dari state space menjadi transfer function.



clear all, close all, clc;

%% State space representation
A = [-11 -18;
    1 0]
% the state matrix

B = [1; 0]
% the input vector

C = [0 1]
% the output vector

D = zeros(size(C,1),size(B,2))
% the feedforward

%% Verify the controllability and observability

Cm = ctrb(A,B)
% Controllability matrix

Om = obsv(A,C)
% Observability matrix

if rank(Cm) == size(A,1)
    'It is controllable'
else
    'It is not controllable'
end

if rank(Om) == size(A,1)
    'It is observable'
else
    'It is not observable'
end

%% Transform into transfer function

[num,den] = ss2tf(A,B,C,D)
sys = tf(num,den)
pole(sys)
% yields
% s = -9
% s = -2

step(sys)
% plot the step response
Berikut ini ialah step response dari sistem tersebut.


Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Matlab: Pole-Placement State Space

Berikut ini yakni script MATLAB untuk state feedback controller.



clear all, close all, clc;

%% Define the system

A = [1 0 2;
    2 3 0;
    1 2 3]
% the state matrix

B = [1; 0; 0]
% the input vector

eig(A)
% the eigenvalues are
% 4.7511 + 0.0000i
% 1.1244 + 1.0251i
% 1.1244 - 1.0251i
% it is not stable

%% The pole-placement

desiredPoles = -0.5*ones(3,1)
% desired poles = [-0.5; -0.5; -0.5]

K = acker(A,B,desiredPoles)
% determine the gain K
% based on the Ackermann's formula
% yields
% K = [8.5000 13.0156 12.7188]

%% State feedback controller

sys = ss(A-B*K, eye(size(A)), eye(size(A)), eye(size(A)))

eig(sys.a)
% the eigenvalues are
% -0.5000 + 0.0000i
% -0.5000 + 0.0000i
% -0.5000 - 0.0000i

x0 = [0.1; 0.1; 0.1]
% initial state

t = 0:0.01:25;
% the time duration

x = initial(sys,x0,t);
% response of the state-space
% with initial condition x0
% and time duration t

x1 = [1 0 0]*x';
x2 = [0 1 0]*x';
x3 = [0 0 1]*x';
% the state variables


%% Plot the state variables

subplot(3,1,1); plot(t,x1), grid
title('Response to initial condition')
ylabel('x1')

subplot(3,1,2); plot(t,x2), grid
ylabel('x2')

subplot(3,1,3); plot(t,x3), grid
ylabel('x3')
xlabel('t (seconds)')

Berikut ini ialah plot response dari state variable.


Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Sabtu, 23 Juni 2018

Matlab: Linear-Quadratic Regulator Design For State Space Systems

Berikut ini yaitu script MATLAB untuk LQR (Linear-Quadratic Regulator) controller.



clear all, close all, clc;

%% Define the system

A = [1 0 2;
    2 3 0;
    1 2 3]
% the state matrix

B = [1; 0; 0]
% the input vector

eig(A)
% the eigenvalues are
% 4.7511 + 0.0000i
% 1.1244 + 1.0251i
% 1.1244 - 1.0251i
% it is not stable

%% LQR design

Q = eye(size(A))

R = 1

K = lqr(A,B,Q,R)
% K =
%    14.5461   24.4004   41.9475

%% State feedback controller

sys = ss(A-B*K, eye(size(A)), eye(size(A)), eye(size(A)))

eig(sys.a)
% the eigenvalues are
% -4.7742 + 0.0000i
% -1.3860 + 1.1477i
% -1.3860 - 1.1477i
% it is stable

x0 = [5; 5; 5]
% initial state

t = 0:0.01:5;
% the time duration

x = initial(sys,x0,t);
% response of the state-space
% with initial condition x0
% and time duration t

x1 = [1 0 0]*x';
x2 = [0 1 0]*x';
x3 = [0 0 1]*x';
% the state variables

%% Plot the state variables

subplot(3,1,1); plot(t,x1), grid
title('Response to initial condition')
ylabel('x1')

subplot(3,1,2); plot(t,x2), grid
ylabel('x2')

subplot(3,1,3); plot(t,x3), grid
ylabel('x3')
xlabel('t (seconds)')

Berikut ini ialah plot response dari state variable.


Lihat juga tentang.
Kendali state space MATLAB.

Sumber http://lang8088.blogspot.com

Matlab: Bode Plot Diagram

Berikut ini yaitu script MATLAB untuk analisis closed-loop dengan Bode plot diagram.

clear all, close all, clc;

%% Define the system

num = 1;
den = [1 3 5 7];
P = tf(num,den);
% define the plant

Kp = 3;
Ki = 5;
Kd = 7;

K = tf([Kd Kp Ki],[1 0])
% define the PID controller

CL = feedback(series(K,P),1)
% closed-loop system (CL)

%% Bode analysis

[Gm1,Pm1,Wcg1,Wcp1] = margin(P)
% Gain margin and Phase margin of the plant
% Gm1 = 8.0004
% Pm1 = inf

[Gm2,Pm2,Wcg2,Wcp2] = margin(CL)
% Gain margin and Phase margin of CL
% Gm2 = Inf
% Pm2 = 93.3738

bode(P),grid
hold on
bode(CL)
legend('Plant','Closed-loop system')

Berikut ini ialah Bode plot diagram.


Lihat juga tentang.
Kendali state space MATLAB.

Untuk klarifikasi perihal Bode plot sanggup dilihat pada artikel berikut.
Sumber http://lang8088.blogspot.com