제어공학기초 설계실습 #5 Programming in MATLAB -1- 제어공학기초 설계실습 실험실습 #5 안내서 실험실습 번호 : #5 실험실습 일시 : 2007. 담당교수 : 담당조교 : , . . 1. 제목 : Programming in Matlab 2. 목적 : Matlab에서 프로그래밍하는 방법과 그의 활용 법에 대해 익힘 3. 실습에 필요한 기초지식 : 슬라이드 유인물 참조 4. 실습절차 : 슬라이드 유인물 참조 -2- 제어공학기초 설계실습 Programming in MATLAB Programming in MATLAB Relational and Logical Operators < less than <= less than or equal == equal > greater than >= greater than or equal ~= not equal -3- 제어공학기초 설계실습 Programming in MATLAB Loops and Conditional Structures “for”, “while” “if-else” commands (i) FOR >> for variable=expression statement; statement; ... end -4- 제어공학기초 설계실습 Programming in MATLAB (Example 1) compute the step response of a second order system for values of ranging from 0.1 to 1 >> n=1; y=zeros(200,1); i=1; >> for zeta=0.1:0.1:1 d=[1, 2*zeta, 1]; t=[0:0.1:19.9]'; y(:,i)=step(n,d,t); i=i+1; end >> mesh(fliplr(y),[-120 30]) -5- 제어공학기초 설계실습 Programming in MATLAB -6- 제어공학기초 설계실습 Programming in MATLAB (ii) WHILE >> while expression statement; statement; ... end (iii) IF, ELSE, ELSEIF >> if expression, statement, ... , statement, ... elseif expression2, statement, ... , statement, ... , else statement, ... . end -7- 제어공학기초 설계실습 Programming in MATLAB M-files: Scripts and Function Files Scripts : operate globally on the data in the workspace Functions : - allow parameter passing by values - use local variables and do not operate globally in the workspace - the first line of a function file must contain the word “function” function [out1,out2, ...]=filename(in1,in2, ...) % optional comment lines for documentation Matlab commands -8- 제어공학기초 설계실습 Programming in MATLAB (Example 2) This program is a function that will compute the step response characteristics of a system. It computes percent overshoot (POS), rise time (Tr), peak time (Tp) and 2% settling time (Ts). function [pos,tr,ts,tp]=stepchar(t,y) % A function program for computing percent overshoot % (POS), rise time (Tr), peak time (Tp) and 2% settling % time (Ts) dimt=length(t); [mp,ind]=max(y); yss=y(dimt); pos=100*(mp-yss)/yss; tp=t(ind); -9- 제어공학기초 설계실습 Programming in MATLAB for i=1:dimt if y(i) > 1.02*yss, ts=t(i); elseif y(i) < 0.98*yss, ts=t(i); end end for i=1:dimt if y(i) < 0.1*yss, t1=t(i); elseif y(i)==mp, break; end end for i=1:dimt; if y(i) < 0.9*yss, t2=t(i); elseif y(i)==mp, break; end end tr = t2 -t1; - 10 - 제어공학기초 설계실습 Matlab 실험·실습과제5 1) 앞에 설명된 MATLAB example 들을 실행해 보시오. 2) 앞의 Example 1과 Example 2를 결합하는 프로그램을 작성하시오. 3) Example 2에서 for 문 대신 while 문을 사용하여 프로그램을 작성하시오. 작성된 프로그램을 Example 1과 결합하여 그 결과를 확인하시오. - 11 - 제어공학기초 설계실습