0

I'm trying to avoid the function tf() from Matlab since it requires specific toolboxes to run.

The transfer function that I'm using is quite simple. Is the model for a heatsink temperature.

                         H(s) = (Rth/Tau)/(s + 1/Tau)

In order to avoid the tf() function, I've tried to substitute the transfer function with a state space model coded in Matlab.

I've used the function ss() to get te values of A,B,C and D. And I've tried to compare the results from tf() and my function.

Here's the code that I've used:

Rth = 8.3220e-04; % ºC/W
Tau = 0.0025; % s

P = rand(1,10)*1000; % Losses = input
t = 0:1:length(P)-1; % Time array

%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Transfer function %%%
%%%%%%%%%%%%%%%%%%%%%%%%%

H = tf([0 Rth/Tau],[1 1/Tau]);
Transfer_func = lsim(H,P,t);

figure, plot(Transfer_func),grid on,grid minor, title('Transfer func')

%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   My función ss   %%%
%%%%%%%%%%%%%%%%%%%%%%%%%

% Preallocate for speed
x(1:length(P)) = 0;
y(1:length(P)) = 0;
u = P;

sys = ss(H);
A = sys.A;
B = sys.B;
C = sys.C;
D = sys.D;

for k = 1:length(u)
     x(k+1) = A*x(k) + B*u(k);
     y(k) = C*x(k) + D*u(k);
end

figure, plot(y), grid on,grid minor, title('With my función')

I know that the values from A,B,C and D are ok, since I've checked them using

H = tf([0 Rth/Tau],[1 1/Tau]);
sys = ss(H);
state_space_sys = ss(sys.A,sys.B,sys.C,sys.D);
state_space = lsim(state_space_sys,P,t);

figure, plot(state_space),grid on,grid minor, title('State space')

As you can see, the results obtained from my funtion and the function tf() are very different.

Is there any mistakes on the approach?

If it's not possible to avoid the tf() function in this way, is there any other way?

D.García
  • 1
  • 2
  • I don't get it, you want to avoid using the `tf` function, but you're using the output of the `tf` function ? – obchardon Oct 10 '19 at 15:19
  • Is just to compare results. I'm using the trial of the function in order to get my function working. – D.García Oct 11 '19 at 07:00
  • I guess it's because your transfer function and your state-space models are **continuous**. However, in your function, you use your state-space model as a **discrete** one, so you're going to get different results. – am304 Oct 11 '19 at 09:49
  • Thanks for your comment @am304 . Do you know how the equations for the continous model would be? Or any info that could help me getting them? All the information that I have found is for discrete code – D.García Oct 11 '19 at 11:03
  • You would probably need to solve them with an ode solver. – am304 Oct 11 '19 at 11:25
  • I've checked more posts online, but they say that you can't use ode solver with **u** as an input. Is there a way that I'm not aware of? – D.García Oct 11 '19 at 12:45
  • Well, the correct way to simulate a system response to a transient input would be to use `lsim` but that's part of the control system toolbox, so I assume that if you don't want to use `tf`, you probably don't want use `lsim` either. You can maybe try to generate a discrete state-space system with a defined sample time instead (see https://uk.mathworks.com/help/control/ref/ss.html for details). – am304 Oct 11 '19 at 15:55

1 Answers1

0

At the end, I found another solution. I'm posting this here, so if someone has the same problem, can use this approach.

If you take the transfer function, and develop it, we reach to the following expresion

H(s) = deltaT(s)/P(s) = (Rth/Tau)/(s + 1/Tau)

deltaT(s) * (s + 1/Tau) = (Rth/Tau) * P(s)

deltaT(s) * s = (Rth/Tau) * P(s) - deltaT(s)/Tau

Now, we know that 1/s is equal to integrate. So in the end, we have to integrate the right side of the equation. The code would be like this.

Cth = Tau/Rth;
deltaT = zeros(size(P));

for i = 2:length(P)
    deltaT(i) = (1/Cth * (P(i)-deltaT(i-1)/Rth))*(time(i)-time(i-1)) + deltaT(i-1);
end

This integral has the same output as the function tf().

D.García
  • 1
  • 2