how can I pretty print a transfer function in matlab?
H(s)=4.25/((0.3s+1)(22.5+1)(40s+1))
And this is how I've implemented it in matlab:
Hf=tf(K,conv(conv([T1 1],[T2 1]),[T3 1]));
But the result is :
Hf = 4.25/(270 s^3 + 918.8 s^2 + 62.8 s + 1)
1) How can I display it in the same format as in the first equation?
2) How can I display a controller in a PID format ?
Kp(1 + Ti/s + Td*s)
Related
I am trying to write a function which takes the value x and y in cartesian coordinates as the input and outputs the polar and trigonometric forms. I would like the output to contain the exponential and sin/cos rather than the actual value. For example, if the cartesian is z=1i, I would like the function to output z=sqrt(2)e^(ipi/4) and z=sqrt(2)(cos(pi/4)+Isin(pi/4). How can I do this?
function coordinates(x,y)
r=sqrt(x.^2+y.^2);
theta=atan(y./x);
polarcoord=rexp(itheta)
trigcoord=r*(cos(theta)+i*sin(theta))
end
This gives me the following output :
polarcoord =
1.0000 + 1.0000i
trigcoord =
1.0000 + 1.0000i
thanks
Welcome to SO! You are using functions (sqrt(x) calculates the actual square root of x, exp(x) the exponential, ... So that is why you get the actual value, not the formula expression.
So, a possible way to output the formula is using strings to print the functions without calling them. Then, what you need to do is calculate the values you want inside the sqrt() and the fraction which * pi and add them to a string array. We can do that using square brackets [ ], the function num2string() and the operator + to add strings in the same string array:
function coordinates(x,y)
[num,dem] = rat(atan(y./x)/pi);
% rat converts theta to fraction
% divide by pi to extract pi from theta
root = ["sqrt(" + num2str(x^2+y^2) + ")"]; % i.e "sqrt(2)"
pi_value = ["(" + num2str(num) + "*pi/" + num2str(dem) + ")"];
% i.e "(1*pi/4)" o "(2*pi/5)"...
polarcoord = ["z = " + root + "e^(i" + pi_value]
trigcoord = ["z = " + root + "(cos" + pi_value + ")+i*sin" + pi_value]
end
Example:
coordinates(1,1)
Outputs:
polarcoord = "z = sqrt(2)e^(i(1*pi/4)"
trigcoord = "z = sqrt(2)(cos(1pi/4))+isin(1*pi/4)"
To calculate the fraction we use rat() function to obtain numerator and denominator from the double output of atan(y./x) and we divide by pi to extract its value from the theta value (we are representing pi already in the string).
I was given a step response graph, and from it, obtained a transfer function. The loop includes a plant and a PID controller, and I know the PID values that produced the output graph. I verified that the transfer function I found was correct by applying a step input, and the output graph matches the one I saw.
My code looks like this:
T = tf([.00248,-.00011,.000163],[1,.01,.00041])%plant and controller(P*C) with feedback
C = pid(2.5,0.5,0.1)%PID values
%T = feedback(C*plant,1)%need to find plant
step(T)
From this, I need to find the plant transfer function so that i can use it to find the optimal PID values instead of the ones it is using now.
If you look at the control loop with unity feedback:
You have for the closed-loop transfer function (that's your T):
Y(s) / U(s) = P*C / (1 + P*C) = T
If you reverse the relationship, you can express P as a function of C and T:
P = T / (C * (1-T))
In MATLAB, I would combine this with the use of the function minreal to obtain a minimum realisation of the transfer function:
>> T = tf([.00248,-.00011,.000163],[1,.01,.00041])
Transfer function 'T' from input 'u1' to output ...
0.00248 s^2 - 0.00011 s + 0.000163
y1: ----------------------------------
s^2 + 0.01 s + 0.00041
Continuous-time model.
>> C = pid(2.5,0.5,0.1)
Transfer function 'C' from input 'u1' to output ...
0.1 s^2 + 2.5 s + 0.5
y1: ---------------------
s
Continuous-time model.
>> P = minreal(T / (C * (1-T)))
Transfer function 'P' from input 'u1' to output ...
0.02486 s^3 - 0.001103 s^2 + 0.001634 s
y1: --------------------------------------------------
s^4 + 25.01 s^3 + 5.254 s^2 + 0.05687 s + 0.001238
Continuous-time model.
I am trying to find the denominator of a given fraction G, but I cannot find a way to use MATLAB's built-in functions without oversimplifying the fraction, and losing important information.
I've tried using MATLAB's built-in commands "numden", "simplify", and "simplifyFraction" but they keep cancelling equal terms from the numerator and denominator. This is normally okay, but for my application, I need to know all values in the denominator which might cause a hole/instability in the function, G.
I've tried looking into the additional constraints within those functions like "IgnoreAnalyticConstraints", but they don't seem to fix the issue. I've simplified my code to isolate my problem below with my current attempts:
syms 's'
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1));
[n,d]=numden(G)
G_simp=simplify(G)
G_simpC=simplify(G,'IgnoreAnalyticConstraints',false)
G_simpF=simplifyFraction(G)
Output:
n = 3
d = s + 1
G_simp = 3/(s + 1)
G_simpC = 3/(s + 1)
G_simpF = 3/(s + 1)
Here is an example fraction input:
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1))
which simplifies to:
G = 3*(s - 1)/((s - 1)*(s + 1)). <-desired result
I am trying to keep the fraction in this simplified form, but the built-in commands will cancel the (s-1) terms resulting in:
G = 3/(s + 1). <-actual result
You can use the Control System Toolbox:
s= tf([1 0],1);
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1))
zpk(G)
The code s= tf([1 0],1); creates a variable s. It contains a Transfer Function object that represents the transfer function f(s)=s. The line
G = 2/(s - 1) + 1/(s + 1) - 4/((s - 1)*(s + 1))
creates a Transfer Function object that contains the corresponding transfer function. And zpk(G) converts this function to the zero/pole/gain form.
The result of the code above is
G =
3 s^3 - 3 s^2 - 3 s + 3
-----------------------
s^4 - 2 s^2 + 1
Continuous-time transfer function.
ans =
3 (s+1) (s-1)^2
---------------
(s+1)^2 (s-1)^2
Continuous-time zero/pole/gain model.
What I Have to do :
Perform the linearization in the vicinity of the operating point. Determine the linearized transfer
This is my non-linear operating point model ('op') with step :
This is my non-linear operating point model ('linmod'), where I have replaced step -> In and to workspace -> Out
Parameter Kn = 948;
For transfer function I did:
[num,den] = linmod('test');
G = tf(num,den);
0.3333 s^2 + 83.33 s + 23.33
--------------------------------------
s^4 + 333.3 s^3 + 2.09e04 s^2 + 5833 s
If I change the value of Kn, and re-run simulation + linmod, I get the exact same transfer function.
Also, I do not quite understand in the mathlab help what is
[A,B,C,D] = linmod['linmod',x,u]
I know that matrixes A,B,C,D are used in
x' = Ax + Bu and
y = Cx + Du
But I do not know what to do from here.
Also a question :
Do I need to use linear or non-linear model for linmod ?
I need to read a PNG file and interpret all the information stored in it and print it in human readable format. While working on PNG, I understood that it uses CRC-32 for generating checksum for each chunk. But I could not understand the following information mentioned on the PNG file specification site:
The polynomial used by PNG is:
x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
Here is the link for reference:
http://www.w3.org/TR/PNG/
Can anyone please help me in understanding this?
http://en.wikipedia.org/wiki/Computation_of_CRC ?
According to list of CRCs in wiki, this polynomial (aka AUTODIN II polynomial ) is one of the most used.
CRC-32-IEEE 802.3 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
Used in (Ethernet, V.42, MPEG-2, PNG, POSIX cksum, Arj, Lha32, Rar, Zip, and more..)
Rewritted with power marked by ^:
x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1.
So you can read source of cksum, e.g. here
http://www.opensource.apple.com/source/file_cmds/file_cmds-188/cksum/crc32.c
The 32-bit AutoDIN-II CRC is built
upon the following shift-register
reference model.
Polynomial: g(x) = 1 + x + x^4 + x^5 + x^7 + x^8 + x^10 + x^11 +
x^12 + x^1 + x^22 + x^23 + x^26 + x^32
Input data bit 0 first
Leading-zero checking is performed by the following procedure:
1. The crc register is initialized to 0xffffffff, not zero.
2. When a crc is appended, the 32 bits of the crc are inverted.
3. When checking a good message with an appended crc, the register
will return to the fixed value of 0xdebb20e3, rather than zero.
That's the CRC-32 algorithm implemented in zlib. Please don't implement your own when you can use that library instead.
[EDIT]: How to use the CRC calculator from zlib (an example in C extracted from the zlib docs).
#include <zlib.h>
uLong crc = crc32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();
If you have the block of data that you want to get the CRC for, you don't need that while loop; you just get the initial value (first assignment to crc above) and then compute the value over the data that you have (second assignment to crc).
Every x term refers to a 1 in the binary representation of 0xedb88320, which is 11101101 10111000 10000011 00100000. The number on the left (least-significant) end, 1, is the (coefficient of the) constant (x^0) term. The next number from the left, 1, is the coefficient of the x term. The next number, 1, is the coefficient of the x^2 term. The next number, 0, is the coefficient of the x^3 term (which is absent because 0*x^3 = 0). And so on. There is an implied 1 to the right of the right (most-significant) end that is the coefficient of the x^32 term.