2
$\begingroup$
T1[z_, t_] = (A1 Cos[z] + B1 Sin[z]) Exp[t];(*from z=0 to z=a=1*)
T2[z_, t_] = (A2 Cos[z] + B2 Sin[z]) Exp[t];(*from z=a=1 to z=b=2*)
T3[z_, t_] = (A3 Cos[z] + B3 Sin[z]) Exp[t];(*from z=b=2 to z=c=3*)
Interval1 = [0, a];
Interval2 = [a, b];
Interval3 = [b, c];
T[z_,t_]=...

This is basic definition of my problem. Is have temperature function depending on time t and location z.

I have 1D problem (z) where i have three different layers and in each layer, temperature has its own function T1,T2,T3. A1,A2,A3,B1,B2,B3 are known (random) constants (you can make them up for this example) Graphic presentation of the problem

For each area/interval temperature changes by its presented function T1,T2,T3.

Is it possible to define globally mora general temperature function T[z_,t_] where you can define that on certain/given interval takes form of the predefined Ti[z,t] function, for instance when z=[0,a], T[z,t]=T1[z,t], when z is in [a,b],then T[z,t]=T2[z,t] and so on...

$\endgroup$
1
  • 6
    $\begingroup$ Sounds like you might want Piecewise. $\endgroup$
    – N.J.Evans
    Commented Jun 10, 2022 at 13:01

1 Answer 1

7
$\begingroup$

Something like this?

T[z_,t_]:=
  Which[
    z<0,BadT[z,t],
    z<a,T1[z,t],
    z<b,T2[z,t],
    z<c,T3[z,t],
    True,BadT[z,t]]

Alternatively, you can add conditions:

T[z_, t_] := (A1 Cos[z] + B1 Sin[z]) Exp[t] /; 0 <= z < a;
T[z_, t_] := (A2 Cos[z] + B2 Sin[z]) Exp[t] /; a <= z < b;
T[z_, t_] := (A3 Cos[z] + B3 Sin[z]) Exp[t] /; b <= z < c;

Or another option with Piecewise:

T = 
  Piecewise[
    {{T1[z, t], 0 <= z < a}, 
     {T2[z, t], a <= z < b},
     {T3[z, t], b <= z < c}, 
     {BadT[z, t], True}}]

In this latter case, you would use it something like this:

Plot3D[T, {z, 0, c}, {t, 0, 1}]

(For all of these, I'm assuming that a, b, c, A1, B1, etc have all been defined)

$\endgroup$
5
  • 1
    $\begingroup$ Which[] certainly works, but I would upvote your answer if you show the Piecewise[] version as well. ;) $\endgroup$ Commented Jun 10, 2022 at 13:51
  • $\begingroup$ Wouldn’t Piecewise introduce the issue of coordinating on the name of the formal argument? That is not something that seems relevant to the original question. $\endgroup$
    – lericr
    Commented Jun 10, 2022 at 13:58
  • 1
    $\begingroup$ I mean, you could write it as Piecewise[{{T1[z, t], 0 <= z < a}, (* other stuff *)}, BadT[z, t]] (careful with the inequalities, tho) and it should still work, with the added bonus that it's guaranteed to play nice with calculus functionality. $\endgroup$ Commented Jun 10, 2022 at 14:08
  • 1
    $\begingroup$ For the lazy :), Which[z < 0, BadT[z, t], z < a, T1[z, t], z < b, T2[z, t], z < c, T3[z, t], True, BadT[z, t]] // PiecewiseExpand. (Execute before T1 etc. are defined, though.) $\endgroup$
    – Michael E2
    Commented Jun 10, 2022 at 14:13
  • 3
    $\begingroup$ There used to be advantages to Piecewise in a mathematical function over other programmatically equivalent formulations. They worked better with solvers. Nowadays, most or all solvers seem to apply PiecewiseExpand internally. E.g., try Integrate[Which[...], z] $\endgroup$
    – Michael E2
    Commented Jun 10, 2022 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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