2
$\begingroup$

I'd like to select between the Tent and Hat function to convolve, but my switch function produces an error.

I don't exactly need the initial plots. But I would like to plot the two that I select.

Error Image

(*Defining Tent Funtion*)

Tri[x_, wT_] := 
  Piecewise[{{(1/wT^2)*x + (1/wT), 
     0 > x > -wT}, {-(1/wT^2)*x + (1/wT), 0 < x < wT}}];

(*Defining Top Hat Funtion*)

Hat[x_, wH_] := 
  Piecewise[ {{(1/(2*wH)), 0 > x > -wH},  {(1/(2*wH)), 0 < x < wH}}];

 (*Defining Incline Funtion*)

(*Plotting & Manipulate Function*)
Manipulate[
 pT = Plot[Tri[x, wTent], {x, -10, 10}, PlotStyle -> Blue, 
   PlotRange -> All];
 pH = Plot[Hat[x, wH], {x, -10, 10}, PlotStyle -> LightGreen, 
   PlotRange -> All];
 Show[{pT, pH}, PlotRange -> All, ImageSize -> Medium],
 s1 = Switch[fx, "Tent Function", Tri[x + df, wTent], 
   "Top Hat Function", Hat[x + df, wH]];
 s2 = Switch[gx, "Tent Function", Tri[x + dg, wTent], 
   "Top Hat Function", Hat[x + dg, wH]];
 plotConv1 = 
  Plot[{s1, s2}, {x, -10, 10}, Exclusions -> None, PlotRange -> All];
 {{wTent, 1, "Triangle Width"}, 0.1, 1},
 {{wH, 1, "Top Hat Width"}, 0.1, 1},
 {fx, {"Tent Function", "Top Hat Function"}},
 {{df, 0, "Shift fx"}, -5, 5},
 {gx, {"Tent Function", "Top Hat Function"}},
 {{dg, -5, "Shift fg"}, -10, 10}
 ]
$\endgroup$
  • $\begingroup$ Try putting the Show and the plotConv inside something like GraphicsColumn. $\endgroup$ – MelaGo May 5 at 3:36
  • 1
    $\begingroup$ Immediately following Show[ ... ], change comma to semicolon. (The semicolon will turn red.) Immediately following plotConv1=Plot[ ... ], change semicolon to comma. $\endgroup$ – LouisB May 5 at 3:36
7
$\begingroup$
(*Defining Tent Funtion*)
Tri[x_, wT_] :=
  Piecewise[{
    {(1/wT^2)*x + (1/wT), 0 > x > -wT},
    {-(1/wT^2)*x + (1/wT), 0 < x < wT}}];

(*Defining Top Hat Funtion*)
Hat[x_, wH_] :=
  Piecewise[{
    {(1/(2*wH)), 0 > x > -wH},
    {(1/(2*wH)), 0 < x < wH}}];

(*Plotting& Manipulate Function*)
Manipulate[
 s1 = Switch[fx, "Tent Function", Tri[x + df, wTent], 
   "Top Hat Function", Hat[x + df, wH]];
 s2 = Switch[gx, "Tent Function", Tri[x + dg, wTent], 
   "Top Hat Function", Hat[x + dg, wH]];
 Column[{
   Plot[
    {Tri[x, wTent], Hat[x, wH]},
    {x, -10, 10},
    PlotStyle -> {Blue, Green},
    Exclusions -> None,
    PlotRange -> All,
    ImageSize -> Medium],
   Plot[{s1, s2}, {x, -10, 10},
    Exclusions -> None,
    PlotRange -> All,
    ImageSize -> Medium]}],
 {{wTent, 1, "Triangle Width"}, 0.1, 1, Appearance -> "Labeled"}, 
 {{wH, 1, "Top Hat Width"}, 0.1, 1, Appearance -> "Labeled"}, 
 {fx, {"Tent Function", "Top Hat Function"}}, 
 {{df, 0, "Shift fx"}, -5, 5, Appearance -> "Labeled"}, 
 {gx, {"Tent Function", "Top Hat Function"}}, 
 {{dg, -5, "Shift fg"}, -10, 10, Appearance -> "Labeled"},
 ControlPlacement -> Bottom]

enter image description here

$\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.