Question

In: Electrical Engineering

Write a MATLAB program to demonstrate the Ziegler-Nichols method of PID loop tuning. Also in no...

Write a MATLAB program to demonstrate the Ziegler-Nichols method of PID loop tuning. Also in no more than one A4 page justify how your MATLAB code demonstrates the PID loop tuning procedure.

Solutions

Expert Solution

  • CLS( Wp,Wc ) :
function sys = CLS( Wp,Wc )
%CLS Closed loop system function

%% Parameters
% Wp : Plant transfer function
% Wc : Controller transfer function
% sys : Closed Loop transfer function with assuming unity feedback.

%% Function implementation
sys=feedback(series(Wp,Wc),1);
end
  • CreatePlant( num,den ):
function [ Wp ] = CreatePlant( num,den )
%CreatePlant Creates plant transfer function.
%   The returned value is the system in numerator/denomerator format
%% Parameters
% num : Numerator vector (starting from highest order of coefficients)
% den : Denomerator vector (starting from highest order of coefficients)
% plant : Plant transfer function 
%% EXAMPLE
%    num=[1];
%    den=[1 0 1];
%    sys=CreatePlant(num,den)

%% Result is        
%             1
% sys= ---------------
%           S^2+1
%% Function implementation
syms s;
Wp=tf(num,den);
end
  • example(varargin):
function varargout = example(varargin)
% EXAMPLE MATLAB code for example.fig
%      EXAMPLE, by itself, creates a new EXAMPLE or raises the existing
%      singleton*.
%
%      H = EXAMPLE returns the handle to a new EXAMPLE or the handle to
%      the existing singleton*.
%
%      EXAMPLE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EXAMPLE.M with the given input arguments.
%
%      EXAMPLE('Property','Value',...) creates a new EXAMPLE or raises
%      the existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before example_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to example_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help example

% Last Modified by GUIDE v2.5 01-Sep-2014 10:35:27

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @example_OpeningFcn, ...
                   'gui_OutputFcn',  @example_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before example is made visible.
function example_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to example (see VARARGIN)

% Choose default command line output for example
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

initialize_gui(hObject, handles, false);

% UIWAIT makes example wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = example_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function density_CreateFcn(hObject, eventdata, handles)
% hObject    handle to density (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function density_Callback(hObject, eventdata, handles)
% hObject    handle to density (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of density as text
%        str2double(get(hObject,'String')) returns contents of density as a double
density = str2double(get(hObject, 'String'));
if isnan(density)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end

% Save the new density value
handles.metricdata.density = density;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function volume_CreateFcn(hObject, eventdata, handles)
% hObject    handle to volume (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function volume_Callback(hObject, eventdata, handles)
% hObject    handle to volume (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of volume as text
%        str2double(get(hObject,'String')) returns contents of volume as a double
volume = str2double(get(hObject, 'String'));
if isnan(volume)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end

% Save the new volume value
handles.metricdata.volume = volume;
guidata(hObject,handles)

% --- Executes on button press in calculate.
function calculate_Callback(hObject, eventdata, handles)
% hObject    handle to calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

mass = handles.metricdata.density * handles.metricdata.volume;
set(handles.mass, 'String', mass);

% --- Executes on button press in reset.
function reset_Callback(hObject, eventdata, handles)
% hObject    handle to reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

initialize_gui(gcbf, handles, true);

% --- Executes when selected object changed in unitgroup.
function unitgroup_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in unitgroup 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if (hObject == handles.english)
    set(handles.text4, 'String', 'lb/cu.in');
    set(handles.text5, 'String', 'cu.in');
    set(handles.text6, 'String', 'lb');
else
    set(handles.text4, 'String', 'kg/cu.m');
    set(handles.text5, 'String', 'cu.m');
    set(handles.text6, 'String', 'kg');
end

% --------------------------------------------------------------------
function initialize_gui(fig_handle, handles, isreset)
% If the metricdata field is present and the reset flag is false, it means
% we are we are just re-initializing a GUI by calling it from the cmd line
% while it is up. So, bail out as we dont want to reset the data.
if isfield(handles, 'metricdata') && ~isreset
    return;
end

handles.metricdata.density = 0;
handles.metricdata.volume  = 0;

set(handles.density, 'String', handles.metricdata.density);
set(handles.volume,  'String', handles.metricdata.volume);
set(handles.mass, 'String', 0);

set(handles.unitgroup, 'SelectedObject', handles.english);

set(handles.text4, 'String', 'lb/cu.in');
set(handles.text5, 'String', 'cu.in');
set(handles.text6, 'String', 'lb');

% Update handles structure
guidata(handles.figure1, handles);
  • PIDExperiment(varargin):
function varargout = PIDExperiment(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @PIDExperiment_OpeningFcn, ...
                   'gui_OutputFcn',  @PIDExperiment_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

function PIDExperiment_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for PIDExperiment
handles.output = hObject;
handles.tfdata.num=1;
handles.tfdata.den=[1000 300 30 1];
handles.tfdata.t='0:0.01:300';
% Update handles structure
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = PIDExperiment_OutputFcn(hObject, eventdata, handles) 
% Get default command line output from handles structure
varargout{1} = handles.output;



function num_Callback(hObject, eventdata, handles)
num=str2num(get(handles.num,'string'));
handles.tfdata.num=num;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function num_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function den_Callback(hObject, eventdata, handles)
den=str2num(get(handles.den,'string'));
handles.tfdata.den=den;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function den_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in gettf.
function gettf_Callback(hObject, eventdata, handles)
Plant=CreatePlant(handles.tfdata.num,handles.tfdata.den);
handles.tfdata.plant=Plant;
guidata(hObject,handles);
sys=evalc('Plant');
set(handles.tf,'string',sys);


% --- Executes on button press in plotnyquist.
function plotnyquist_Callback(hObject, eventdata, handles)
axes(handles.nyquistaxis);
nyquist(handles.tfdata.plant);


% --- Executes on button press in findkc.
function findkc_Callback(hObject, eventdata, handles)
[Kc,Pm,Wc,Wm]=margin(handles.tfdata.plant);
pu=2*pi/Wc;
set(handles.kc,'string',num2str(Kc));
set(handles.pu,'string',num2str(pu));
handles.tfdata.kc=Kc;
handles.tfdata.pu=pu;
guidata(hObject,handles);


% --- Executes on button press in plotziegler.
function plotziegler_Callback(hObject, eventdata, handles)

%P-Controller
Kpp=handles.tfdata.kc*0.5;
Tip=100000;
Tdp=0;

%PI-Controller
Kppi=handles.tfdata.kc*0.45;
Tipi=handles.tfdata.pu*0.83;
Tdpi=0;

%PID-Controller
Kppid=handles.tfdata.kc*0.59;
Tipid=handles.tfdata.pu*0.5;
Tdpid=handles.tfdata.pu*0.12;

%Closed Loop transfer function
Wcp=ZieglerNicholasPID(Kpp,Tip,Tdp);
Wcpi=ZieglerNicholasPID(Kppi,Tipi,Tdpi);
Wcpid=ZieglerNicholasPID(Kppid,Tipid,Tdpid);

sysp=CLS(handles.tfdata.plant,Wcp);
syspi=CLS(handles.tfdata.plant,Wcpi);
syspid=CLS(handles.tfdata.plant,Wcpid);

zntable=[Kpp Tip Tdp;Kppi Tipi Tdpi;Kppid Tipid Tdpid ];
set(handles.table,'data',zntable);

%plotting
axes(handles.responseaxis);
t=str2num(handles.tfdata.t);
step(sysp,syspi,syspid,t),
legend('P','PI','PID');


function t_Callback(hObject, eventdata, handles)
t=num2str(get(handles.t,'string'));
handles.tfdata.t=t;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function t_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
  • ZieglerNicholasPID( Kc,Ti,Td ):
function Wc  = ZieglerNicholasPID( Kc,Ti,Td )
% ZieglerNicholasPID function to generate the PID controller transfer 
%% Parameters
% Kc : Critical gain
% Ti : Reset time (minutes)
% Td : Derivative time (minutes)
% Wc : The laplace representation of Z-N

%% EXAMPLE
%    Kc=10;
%    Ti=0.83;
%    Td=2.5
%    Wc=ZieglerNicholasPID( Kc,Ti,Td )
%% Result is        
%               1 
% Wc= 10*(1+ -------- + 2.5*s
%             0.83*s 

%% Function implementation 
s=tf('s');
Wc=Kc*(1+(1/(Ti*s))+Td*s);
end

Related Solutions

Write a MATLAB program to demonstrate the Ziegler-Nichols method of PID loop tuning. Also in no...
Write a MATLAB program to demonstrate the Ziegler-Nichols method of PID loop tuning. Also in no more than one A4 page justify how your MATLAB code demonstrates the PID loop tuning procedure. Instructions: • Upload your MATLAB .m file and also the PDF document separately
Compare and contrast the Ziegler-Nichols and the Pessen tuning methods. Explain their advantages and drawbacks.
Compare and contrast the Ziegler-Nichols and the Pessen tuning methods. Explain their advantages and drawbacks.
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
Write a for-loop in MATLAB that generates a list of numbers such that each number is...
Write a for-loop in MATLAB that generates a list of numbers such that each number is the sum of the previous three. Initialize your list of numbers at the values of 0, 0 and 1. In other words, "0" is the first element of the list, "0" is the second element of the list, and "1" is the third element of the list. What is the 20th value in the list?
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
write a program in c++ to design a tuning circuit for a given frequency bond using...
write a program in c++ to design a tuning circuit for a given frequency bond using a variable capacity to determine the value of an inductance required for a given frequency range
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
Write a program with the aim of performing an audiometry test at MATLAB. The program should...
Write a program with the aim of performing an audiometry test at MATLAB. The program should be as interactive as possible. For example, at first, which ear is to be tested should be chosen so that the sound is only given to that channel of the ear. In addition, whether the test frequency increases automatically or manually should be asked as a parameter. The frequencies of the person being tested should be entered by the user as well as whether...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT