2
Manipulate[
 Plot[A Sin[\[Omega] x + \[Phi]] + B, {x, -10, 10}, 
  PlotLabel -> 
   "f(x)=" <> ToString[A] <> "sin(" <> ToString[\[Omega]] <> "x+" <> 
    ToString[\[Phi]] <> ")+" <> ToString[B], PlotRange -> {-6, 6}, 
  Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}], 
  AxesLabel -> {x, y}, ImageSize -> 700, 
  Ticks -> {Table[i, {i, -4 \[Pi], 4 \[Pi], \[Pi]/4}], 
    Table[t, {t, -5, 5, 0.5}]}], {{A, 1}, -5, 5, 
  Appearance -> "Labeled"}, {{\[Omega], 1}, -3, 8, 
  Appearance -> "Labeled"}, {{\[Phi], 0}, -\[Pi], 2 \[Pi], 
  Appearance -> "Labeled"}, {{B, 0}, -1, 5, Appearance -> "Labeled"}]

enter image description here

How to modify the following label code so that when the value of [Phi] is negative, only a - sign is used to connect it in the middle, instead of continuously appearing in the label+-

PlotLabel -> 
   "f(x)=" <> ToString[A] <> "sin(" <> ToString[\[Omega]] <> "x+" <> 
    ToString[\[Phi]] <> ")+" <> ToString[B]

enter image description here

csn899
  • 3,953
  • 6
  • 13

3 Answers3

3

Add a variable (I am using plotLabel) with ControlType -> None.

Use a Which statement to remove the + sign when \[Phi]is negative. (I also removed it entirely when \[Phi] was 0).

Manipulate[
 plotLabel = Which[
   \[Phi] < 0,
   "f(x)=" <> ToString[A] <> "sin(" <> ToString[\[Omega]] <> "x" <> 
    ToString[\[Phi]] <> ")+" <> ToString[B],
   Or[\[Phi] == 0.0, \[Phi] == 0],
   "f(x)=" <> ToString[A] <> "sin(" <> ToString[\[Omega]] <> "x)+" <> 
    ToString[B],
   True,
   "f(x)=" <> ToString[A] <> "sin(" <> ToString[\[Omega]] <> "x+" <> 
    ToString[\[Phi]] <> ")+" <> ToString[B]
   ];

Plot[A Sin[[Omega] x + [Phi]] + B, {x, -10, 10}, PlotLabel -> plotLabel, PlotRange -> {-6, 6}, Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}], AxesLabel -> {x, y}, ImageSize -> 700, Ticks -> { Table[i, {i, -4 [Pi], 4 [Pi], [Pi]/4}], Table[t, {t, -5, 5, 0.5}] } ], {{A, 1}, -5, 5, Appearance -> "Labeled"}, {{[Omega], 1}, -3, 8, Appearance -> "Labeled"}, {{[Phi], 0}, -[Pi], 2 [Pi], Appearance -> "Labeled"}, {{B, 0}, -1, 5, Appearance -> "Labeled"}, {plotLabel, ControlType -> None} ]

Jack LaVigne
  • 14,462
  • 2
  • 25
  • 37
3

another option

Manipulate[
 Module[{e, p},
  p = _. _?Negative; 
  e = "f(x)=" <> ToString[A] <> " sin(" <> ToString[ω] <> 
    " x " <> If[MatchQ[ϕ, p], "", "+"] <> ToString[ϕ] <> 
    ")" <> If[MatchQ[B, p], "", "+"] <> ToString[B];
  p = Plot[A Sin[ω x + ϕ] + B, {x, -10, 10},
    PlotLabel -> e,
    PlotRange -> {-6, 6}, Axes -> True,
    AxesStyle -> Arrowheads[{0.0, 0.04}],
    AxesLabel -> {x, y}, ImageSize -> 700,
    Ticks -> {Table[i, {i, -4 π, 4 π, π/4}], 
      Table[t, {t, -5, 5, 0.5}]}
    ];
  p
  ],
 {{A, 1}, -5, 5, Appearance -> "Labeled"},
 {{ω, 1}, -3, 8, Appearance -> "Labeled"},
 {{ϕ, 0}, -π, 2 π, Appearance -> "Labeled"},
 {{B, 0}, -1, 5, Appearance -> "Labeled"},
 TrackedSymbols :> {A, ω, ϕ, B}]

enter image description here

Reference Given a symbolic expression how to find if starts with a minus or not?

Nasser
  • 143,286
  • 11
  • 154
  • 359
2

Use StringForm

$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

Manipulate[ f[x_] = A Sin[ω x + ϕ] + B; Plot[f[x], {x, -10, 10}, PlotLabel -> StringForm["=", HoldForm@f[x], f[x]], PlotRange -> {-6, 6}, Axes -> True, AxesStyle -> Arrowheads[{0.0, 0.04}], AxesLabel -> {x, y}, ImageSize -> 700, Ticks -> {Range[-4 π, 4 π, π/4], Range[-5, 5, 0.5]}], Grid[{ {Control[{{A, 1}, -5, 5, Appearance -> "Labeled"}], Control[{{B, 0}, -1, 5, Appearance -> "Labeled"}]}, {Control[{{ω, 1}, -3, 8, Appearance -> "Labeled"}], Control[{{ϕ, 0}, -π, 2 π, Appearance -> "Labeled"}]}}], SynchronousUpdating -> False, TrackedSymbols :> {A, ω, ϕ, B}]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198