0

I am working on simulating an AC just like what's been shown in this link. I have managed to convert it into an AC by flipping some equations and re-initializing some values. Now, I need to make it in Matlab to able to let my system work smoothly and faster without having to run simulink for every iteration. I did some reading on ODE45 and decided to use it, firstly, I converted the model in the webpage to a function as the following:

    function dTroom_dt = odes_thermal(Troom,Tout)
    TinIC = 26;
    r2d = 180/pi;
    % -------------------------------
    % Define the house geometry
    % -------------------------------
    % House length = 30 m
    lenHouse = 30;
    % House width = 10 m
    widHouse = 10;
    % House height = 4 m
    htHouse = 4;
    % Roof pitch = 40 deg
    pitRoof = 40/r2d;
    % Number of windows = 6
    numWindows = 6;
    % Height of windows = 1 m
    htWindows = 1;
    % Width of windows = 1 m
    widWindows = 1;
    windowArea = numWindows*htWindows*widWindows;
    wallArea = 2*lenHouse*htHouse + 2*widHouse*htHouse + ...
        2*(1/cos(pitRoof/2))*widHouse*lenHouse + ...
        tan(pitRoof)*widHouse - windowArea;
    % -------------------------------
    % Define the type of insulation used
    % -------------------------------
    % Glass wool in the walls, 0.2 m thick
    % k is in units of J/sec/m/C - convert to J/hr/m/C multiplying by 3600
    kWall = 0.038*3600;   % hour is the time unit
    LWall = .2;
    RWall = LWall/(kWall*wallArea);
    % Glass windows, 0.01 m thick
    kWindow = 0.78*3600;  % hour is the time unit
    LWindow = .01;
    RWindow = LWindow/(kWindow*windowArea);
    % -------------------------------
    % Determine the equivalent thermal resistance for the whole building
    % -------------------------------
    Req = RWall*RWindow/(RWall + RWindow);
    % c = cp of air (273 K) = 1005.4 J/kg-K
    c = 1005.4;
    % -------------------------------
    % Enter the temperature of the heated air
    % -------------------------------
    % The air exiting the heater has a constant temperature which is a heater
    % property. THeater =20 deg C
    THeater = 20;
    % Air flow rate Mdot = 1 kg/sec = 3600 kg/hr
    Mdot = 3600;  % hour is the time unit
    % -------------------------------
    % Determine total internal air mass = M
    % -------------------------------
    % Density of air at sea level = 1.2250 kg/m^3
    densAir = 1.2250;
    M = (lenHouse*widHouse*htHouse+tan(pitRoof)*widHouse*lenHouse)*densAir;
    %%

    dQlosses_dt= (THeater-Troom)*Mdot*c;

    dQlosses_dt=(Troom-Tout)/Req;

    dTroom_dt=(1/(M*c))*(dQlosses_dt);

    end

Then I just called it using the following lines:

    clear;
    t=1:1:1440;
    Troom_0=25;
    [t,Troom] = ode45('odes_thermal',[t(1) t(3)],Troom_0);
    figure(1);
    plot(t,Troom);

when I run it this is what I get: enter image description here

My question is, how do I incorporate the status or on off features in it. In simulink they were using a relay that will produce a status signal that would be 1 or 0 depending on the temperature, this is how they did it:

enter image description here

I know that this can be represented by "dQlosses_dt= (THeater-Troom)Mdotc*status;" but I don't know how to update status for every time sample as there is no way to check Troom at every time slot.Can you guide me in this, or at least from where to start?

Edit: following Pacta's comment, I tried to make it into a multivariant system which has two outputs Troom and Qlosses. Here are the new functions, but I am getting an error (too many input arguments in odes_thermal2)

    clear;
    t=1:1:1440;
    Troom_0=25;Qlosses_0=10;
    X0=[Troom_0;Qlosses_0];
    [t,X] = ode45('odes_thermal2',[t(1) t(end)],X0);
    Troom=X(1);
    Qlosses=X(2);
    figure(1);
    plot(t,Troom);
    ylabel('Troom');
    xlabel('t');

And the ODE function has the following changes:

    function dX_dt = odes_thermal2(X)
    TinIC = 26;
    r2d = 180/pi;

and

    X(2)= (THeater-Troom)*Mdot*c;

    dQlosses_dt=(Troom-Tout)/Req;

    X(1)=(1/(M*c))*(dQlosses_dt);

Thanks

Isra
  • 155
  • 1
  • 12
  • not sure what you are trying to accomplish. Is it a status function which maps Troom onto something else (namely 0 and 1)? IF so, do you have to do it online(while the simulation marches) or can you just postprocess Troom once the solution has been computed? – pacta_sunt_servanda Oct 06 '18 at 11:17
  • the status value actually plays a role in the Troom update, since the heater's next heat flow value depends on whether its on or off " dQlosses_dt= (THeater-Troom)*Mdot*c * status". In the link I referred to, it is represented by one of the figures. I couldn't add that correctly and the status vector is important to me form my application (AC power cost optimization) – Isra Oct 06 '18 at 11:20
  • If understand well, you have something like: dx1_dt = f(x1, x2 , …); dx2_dt = f(x1, …) . Can't you consider a two dimensional state? – pacta_sunt_servanda Oct 06 '18 at 11:35
  • can you explain what you mean? I am a beginner in ODEs – Isra Oct 06 '18 at 11:36
  • 1
    If you have two 'dynamic' equations for the evolution of x1 (T_room) and x2 (Q_losses) you need to create a function which maps (T_room, Q_losses) into your dT_room, dQ_losses. In general it is a multivariate function. In this case you just pass function dX_dt = odes_thermal(X) where X is 2D and so dX_dt. As a side note: your initial conditions will have to be 2D as well (T_room at t_init and Q_loss at t_init). The solver will spit out a 2D solution (t_room and q_loss) – pacta_sunt_servanda Oct 06 '18 at 11:40
  • I have tried to follow what you suggested, but I got an error "to many input arguments", I am editting my question to include my attempt – Isra Oct 06 '18 at 14:44
  • Please try to follow the interface proposed by ode45 (ref to https://uk.mathworks.com/help/matlab/ref/ode45.html ). – pacta_sunt_servanda Oct 06 '18 at 14:56
  • basically what you want is : function dDepVars_dt = myOdeToBeSolved(t,depVars) inside the function you explicitly build your 2D vector; e.g.: depVars(1) = T_room; depVars(2) = Q_losses and then return dDepVars_dt (1) as dT_room and deVars_dt as dQ_losses – pacta_sunt_servanda Oct 06 '18 at 14:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181401/discussion-between-isra-and-pacta-sunt-servanda). – Isra Oct 06 '18 at 15:08

0 Answers0