user1482746
user1482746

Reputation: 15

Problems with Mathematica Plotting using Piecewise

I am trying to plot using piecewise in one of my problems and I have two variables: x and psi. However, the respective functions are only valid for a defined range of "x" and the psi range is the same. I am trying to make a 3D plot of these -- and I basically just have Plot3D[p,{x,0,1},{psi,0.01,1}] ---> These ranges are for the entire plot range and my x range for the respective functions is already defined in the Piecewise function.

I get the following error: saying that Plot::exclul: ...... must be a list of equalities or \ real-valued functions.

Can anyone please help me with this. I am trying to follow the same procedure as: How can I use Piecewise[] with a variable number of graphs/intervals But, I don't know what to do about the plotting part.

Thanks.

The following is my code:

j = 10; 
s = 0; r = 0;

K[x_, psi_] := 
  Sum[Sin[n*Pi*x]*
    Sin[n*Pi*
      psi]*(2*Exp[-(n*Pi)^2*
         Abs[s + r]] - (Exp[-(n*Pi)^2*Abs[s - r]] - 
         Exp[-(n*Pi)^2*(s + r)])/(n*Pi)^2 ), {n, 1, j}];

TL[x_, psi_] = Integrate[K[x - y, psi]*y, {y, -10, 10}];

TU[x_, psi_] = Integrate[K[x - y, psi]*(1 - y), {y, -10, 10}];

eq = {TL[x, psi], TU[x, psi]};
cond = {{0 <= x <= 0.5, 0.01 <= psi <= 1}, {0.5 < x <= 1, 
    0.01 <= psi <= 1}};
p = Piecewise[{eq, cond}];

Plot3D[p, {x, 0, 1}, {psi, 0.01, 1}]

Upvotes: 0

Views: 2449

Answers (1)

Chris Degnen
Chris Degnen

Reputation: 8645

Here is a working version:

time = AbsoluteTime[];
j = 10; s = 0; r = 0;
K[x_, psi_] :=
  Sum[Sin[n*Pi*x]*Sin[n*Pi*psi]*
    (2*Exp[-(n*Pi)^2*Abs[s + r]] -
      (Exp[-(n*Pi)^2*Abs[s - r]] -
         Exp[-(n*Pi)^2*(s + r)])/(n*Pi)^2), {n, 1, j}];
TL[x_, psi_] := Integrate[K[x - y, psi]*y, {y, -10, 10}];
TU[x_, psi_] := Integrate[K[x - y, psi]*(1 - y), {y, -10, 10}];
Plot3D[Piecewise[
  {{TL[x, psi], 0 <= x <= 0.5}, {TU[x, psi], 0.5 < x <= 1}}],
 {x, 0, 1}, {psi, 0.01, 1}]
ToString[Round[AbsoluteTime[] - time]] <> " seconds"

enter image description here

Upvotes: 1

Related Questions