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