1
$\begingroup$

A while ago I posted a question regarding differential equations. It was supposed to model heat transfer. We've refined our model a bit (using numerical methods now). Our code now looks like this:

    Manipulate[
     tijd = (h*3600 + m*60 + s);
     T0 = (T0max + T0min)/2 + ((T0max - T0min)/2)*
        Sin[(2*\[Pi]/86400) (t + tijd - 86400/4)];
     Sol = NDSolve[{Ta'[t] == -(2*ka + k0)*Ta[t] + ka*Tb[t] + ka*Tc[t] + 
          k0*T0 + f, 
        Tb'[t] == kb*Ta[t] - (2*kb + k0)*Tb[t] + kb*Tc[t] + k0*T0, 
        Tc'[t] == kc*Ta[t] + kc*Tb[t] - (2*kc + k0)*Tc[t] + k0*T0, 
        Ta[0] == Ta0, Tb[0] == Tb0, Tc[0] == Tc0}, {Ta, Tb, Tc}, {t, 0, 
        tmax}, MaxSteps -> 100000];
     Plot[{Evaluate[{Ta[t], Tb[t], Tc[t]} /. Sol], T0}, {t, 0, tmax}, 
      PlotRange -> {{0, tmax}, {Tmin, Tmax}}, Frame -> True, 
      Axes -> False, 
      LabelStyle -> {FontFamily -> "Arial", FontSize -> 12}, 
      FrameLabel -> {"Time (s)", "Temperature (\[Degree]C)"}, 
      ImageSize -> Large], 
     Style["Initial Temperature", 12, Bold], {{Ta0, 20, "Ta"}, 0, 
      50}, {{Tb0, 30, "Tb"}, 0, 50}, {{Tc0, 20, "Tc"}, 0, 
      50}, {{T00, 25, "T0"}, 0, 50}, Delimiter, 
     Style["k-values", 12, Bold], {{ka, 0.000305, "ka"}, 0, 
      0.001}, {{kb, 0.00061, "kb"}, 0, 0.001}, {{kc, 0.00061, "kc"}, 0, 
      0.001}, {{k0, 0.0001525, "k0"}, 0, 0.001}, Delimiter, 
     Style["Axis values", 12, Bold], {{tmax, 5000, "tmax"}, 0, 
      86400}, {{Tmin, 15, "Tmin"}, -10, 40}, {{Tmax, 35, "Tmin"}, 0, 
      100}, Delimiter, 
     Style["Heat Source", 12, Bold], {f, 0.002, 
      ControlType -> InputField}, Delimiter, 
     Style["Time", 12, Bold], {{h, 12, "hours"}, 0, 
      23}, {{m, 0, "minutes"}, 0, 59}, {{s, 0, "seconds"}, 0, 
      59}, Delimiter, 
     Style["Outer temperature", 12, Bold], {{T0max, 25, "Tmax"}, -50, 
              50}, {{T0min, 15, "Tmin"}, -50, 50}]   

The part I'd like to talk about is this:

    Style["Heat Source", 12, Bold], {f, 0.002, 
      ControlType -> InputField}

Right now, our heat source is a constant 0.002. What we would like to do, is make it dependant upon the room temperature, so that it shuts off when the room temperature is above say, 18 degrees (we're using celsius).

I know this should be possible using

    Piecewise[{0.002, Ta < 18}, 0]

But whenever I insert this into the code as follows, I get errors.

    Style["Heat Source", 12, Bold], {f, Piecewise[{0.002, Ta < 18}, 0], 
      ControlType -> InputField}

How should I implement this to get the desired outcome?

Thanks in advance.

$\endgroup$
2
$\begingroup$

It's always a good idea to read the error messages. Your first one says

Piecewise::pairs : The first argument {0.002, Ta < 18} of Piecewise is not a list of pairs.

This is fairly self-explanatory. Checking the documentation for Piecewise we find that the first argument should indeed be a list of pairs, so use this:

Piecewise[{{0.002, Ta < 18}}, 0]

Re-evaluating we get more errors. The first one says:

NDSolve::dvnoarg : The function Ta appears with no arguments.

Again, this is self-explanatory. Ta is a function of t, so use this:

Piecewise[{{0.002, Ta[t] < 18}}, 0]

The code now runs without errors.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.