Sei sulla pagina 1di 6

Assignment Part 2: Transient Response Analysis Assignment

The first order system is given by: G ( s ) = K


Ts +1

Hence a user specifying:den = [2,5] num = 1 should represent:

1 0 .2
G (s) = =
2 s + 5 0 .4 s + 1

By dividing through by 5 (last term in denominator vector) to put it in the standard


format, the T = 0.4, and the gain term is 0.2.

The time domain equation for the step response is given by:

c(t ) = K (1 −e −t T )

Where t would be a user specified time range or a range generate within the
function.

Kwn2
The 2nd order system is given by: G ( s ) =
s 2 + 2ξwn s + wn2

Hence a user specifying: den = [2,5,16], num = 10 should represent:

10 5
G (s) = = 2
2 s + 5s + 16 s + 2.5s + 8
2

By dividing through by 2(first term in denominator vector) to make the coefficient


of the s2 term = 1, we obtain the general form of the expression, the damping ratio,
damped and undamped natural frequency terms can be obtained:

wn2 = 8 ; 2ξwn = 2.5 ; and Kw n2 = 5 Therefore all three terms can be calculated. This
is key to generate the correct second order plot.

The time-domain equations for the three possible response of a second order
system are summarize as follows:

The size of the damping ratio constant of the system determines the type of
response:

i) If the damping ratio is less than 1 then the response is underdamped:


ξwn −2ξwnt
c( t ) = K − Ke −2ξwnt cos ( wd t ) − K e sin ( wd t )
wd
where (damped natural frequency)

1
ii) If the damping ratio is equal to one then the response is critical:

c( t ) = K − K (1 + wn t ) e −ξwnt

iii) If the damping ratio is greater than one then the response is overdamped:

c(t ) = K (1 − Ae −s1t − Be −s2t )


( ) (
where s1 = wn ξ − ξ 2 − 1 and s 2 = wn ξ + ξ 2 − 1 )
3ξ + ξ 2 −1 ξ 2 −1 − 3ξ
B= and A = (1 − B) =
2 ξ 2 −1 2 ξ 2 −1

Your major challenge will be entering the equations correctly so that they work.

Develop a function that will use the basic plot command to generate the
step-response plot of a first or second order system based on the set of
time domain equations provided. The function/script should use as input
arguments the user specified model parameters. The time range can also
be specified as an input argument or be generated internally.

Other useful commands available in Matlab:

y = sin(x) – returns sine of a scalar or the elements of a vector or matrix.

y = exp(x) – returns the exponential of a scalar or the elements of a vector


or matrix.

y = cos(x) – returns the cosine of a scalar or the elements of a vector or


matrix.

2
y = sqrt(x) – returns the square root of a scalar or the elements of a vector
or matrix.

Further useful scripting examples to review


In this assignment you are require to apply the ‘input’ command to request data
from the user. General most basic format:
NameOfVariable = input(‘User Prompt’);
User Prompt – should be enter as shown in quotes and it’s a statement that
instructs the user of what they are required to enter.
NameofVariable – this is the variable to which the user entry is stored. To use the
using entry further in your code, therefore, you would have to refers the named
variable. Examples:
>> rowvect = input('Please enter a row vector of 3 values, use [ and ] to enclose
your entry: ')

Please enter a row vector of 3 values, use [ and ] to enclose your entry: [1,2,3,4]

rowvect = Upon pressing enter the following prompt appears with the cursor one
1 2 3 4 blank space away from the colon so the user entry doesn’t appear to
run into the last word
>>
Please enter a row vector of 3 values, use [ and ] to enclose your
Note that the user didn’t follow instructions and 4 values instead of 3 were enter,
the following is an an example using the input command within a script (function)
and writing the necessary code to ensure the user entry is correct:
function [ydata, coeff] = testinput(xdata)
%This functions plots a quadratic function that the user specifies

userip= input('Please enter a row vector of 3 coefficient, use [ and ] to enclose your entry: ');
% the following code checks if the user enter the data correctly:
n = length(userip); %could have used the 'size' command also

if n > 3
coeff = userip(1:3); %only use the first 3 values of the user vector
elseif n < 3 % prompt user to re-enter input
maxattempt = 5; %this is number of times the user is allow to re-enter value
retry = 1;
while n ~= 3 && retry <= maxattempt % loops if length not three or retrys not exceeded
userip = input('Row vector should be of length 3, please re-enter vector: ');

n = length(userip); %could have used the 'size' command also


retry = retry + 1; %counter keeping check of number of attempts
end
% assume the user has gotten it correct
coeff = userip; % 2nd return argument

else % this branch executes only if n == 3 since other options have been tested

coeff = userip;

end

a = coeff(1); b = coeff(2); c = coeff(3); %extract coefficients of quadratic


dim = size(xdata); % check number of columns and row in xdata

3
rows = dim(1); cols = dim(2);
ydata = a*xdata.^2 + b*xdata + c*ones(rows,cols); % first return argument
plot(ydata);

Things to take note of the following:


- .^2 this was used to avoid an error and to instruct matlab square
the elements within the xdata matrix/vector and not attempt to
multiple the matrix by itself.
- The c coefficient was multiply by ones matrix of the same
dimension as xdata. This is again done to avoid an error as the
three terms in the quadratic expression must be of the same
matrix dimension in order for them to be added.
- Example use of the while command

- Example use of the if-then-else command

- Example use of the length and size command for checking vector
and matrix dimension

- Example use of the input command

The command window testing:

%In this first example the user enters 4 values but only 3 is return and is
used to build the quadratic function

>> xrange = 0:0.5:10;

>> [yrange, quadcoef] = testinput(xrange)

Please enter a row vector of 3 coefficient, use [ and ] to enclose your entry: [1,2,3,4]

yrange =

Columns 1 through 10

3.0000 4.2500 6.0000 8.2500 11.0000 14.2500 18.0000 22.2500 27.0000


32.2500

Columns 11 through 20

38.0000 44.2500 51.0000 58.2500 66.0000 74.2500 83.0000 92.2500 102.0000


112.2500

Column 21

123.0000

4
quadcoef =

1 2 3

>>

140

120

100

80

60

40

20

0
0 5 10 15 20 25

In this second example the user enters 2 values instead of 3 values and so
is re-prompted to re-enter vector:

>> xrange = [0:0.5:10]'; %transpose to make a column vector instead of row

>> [yrange, quadcoef] = testinput(xrange)

Please enter a row vector of 3 coefficient, use [ and ] to enclose your entry: [2,4]

Row vector should be of length 3, please re-enter vector: [2,4,1,5]

Row vector should be of length 3, please re-enter vector: [2,4,1]

yrange =

1.0000
250

3.5000
200

7.0000

11.5000 150

.
100

219.5000 50

5
0 5 10 15 20 25
241.0000

quadcoef =

2 4 1

Note that the return argument vector for the y-values is now a column vector of
the same dimension as the x data values enter.

An alternative to enter a vector would have been to ask the user to separately enter
the coefficients for a, b, and c:

a = input(‘Input coefficient of square term: ‘)

b = input(‘Input coefficient for first order term: ‘)

c = input(‘coefficient of constant term: ‘)

Or finally this information could have been designed to be provided as input arguments to
the function:

function [ydata, coeff] = testinput(xdata,a,b,c)

Potrebbero piacerti anche