I'm looking for code source in C, C ++ or C # to evaluate a transfer function provided in "s" space and generate similar graphics like the "step" and "impulse" functions in Matlab.
I already have the model in the transfer function format.
See below Matlab code:
s = tf('s')
sis = (2*s+10) / (1*s^2+2*s+10)
step(sis)
I want the code source of a program or algorithm that uses a transfer function, as modeled, to make the graph in the same way that makes Matlab using the "step" but I do not want to use matlab, scilab or other software like.
What I need is to make an independent program, stand alone, that performs these calculations for the graph I need.
Hi. It's relatively simple. For the step response, you basically integrate the system's equations (which you, I hope, can derive) excited by a step function numerically.
Read, for instance, http://stackoverflow.com/questions/14603874/numerical-integration-with-c and http://rosettacode.org/wiki/Numerical_integration#C.
Graphing is a separate question, but it has been highlihted many-many times. I hope you'll be able to find examples. E. g. http://koolplot.codecutter.org/
One of possible routes would be to convert transfer function (alone for impulse response or multiplied with step for step response) to ordinary differential equation (ODE). Or better to state-space equations (set of first order ODE).
Next you can use any numerical integration method for getting output.
Numerical integration algorithms in C you may find in book:
Numerical Recipes in C: The Art of Scientific Computing
For test (getting a feeling of process) you may simply substitute dx/dt with (x((k+1)T)-x(kT))/T (Euler's method) and as result you would get set of recursive algebraic equations. Iterations would start with given initial conditions.
Second, but not the last, one would be to obtain approximate discrete z-model, e.x. by using Tustin transformation and then solving it's equivalent discrete equation iteratively.
How to Convert a transfer function in a ODE ( generic algorithm ).
Or getting the model in state space, how can I generate the data for the graph of output x time?
In your second positioning, believe that, after obtaining the converted model in z-model, I need generate a difference equation and use it for the N elements of graphic output x time considering t = K * T (T = period) .
but doubt is now: how to convert, generally, a s-model to a Z-model automatically?
That is, how to programmatically accomplish the transformation of Tustin or Euler in a s-model transfer function?
But extracting a system of linear ordinary DE's out of a transfer function is simple (basically converting from TF to state-space). But remember, it's not unique. You can choose a particular form.
As others mentioned, you can also convert to a discrete representation, but be careful: numeric problems are often inevitable, unless you use a clever technique, say, splitting your filter into sections.
Generic algorithm for converting any transfer function (TF) to state-space equations (set of first order-ODE) can be found in many linear control books. I am attaching some pages from "Modern Control Systems" by Dorf and Bishop. Any TF, Equ. 3.41, can be converted ODEs, Equ. 3.50. Output will be calculated from Equ. 3.51. This form of equations results when phase variables are adopted for state vector x.
Other forms of state equations will result for other types of state variables, but upper one needs no calculations.
If system is already in state-space form you can skip this step.
Step 2.
Numerical solution of ODEs (Equ. 3.50), i.e. integration, with any numerical method from the book in previous post.
For this step you can find many codes in the Internet. See for ex. odeint implementation for C++