2

This should be simple enough, but I cannot figure out where the logical error is, I am generating two Gaussian pulses and plotting them with some manipulatable elements. Here is what that a pulse looks like:

 Pulse[t_, arr_] := Module[{μ, σ}, {μ, σ} = arr; Exp[-(t - μ)^2/(2 σ^2)]]

And how making two of them (with a manipulatable shift) looks like. The first one is always centered at zero, the other one can be moved around without interfering with the first. δp simply means difference in peaks.

 TwoPulse[t_, δp_] := Piecewise[{{Pulse[t, {0, 1}], -5 <= t < 5}, 
                                 {Pulse[t, {12 + δp, 1}], 5 <= t}}]; 

So that makes one function with two pulses. Next, I need two such functions. So I overload TwoPulse:

TwoPulse[t_, μ_, δp_] := Piecewise[{{Pulse[t, {μ, 1}], μ - 5 <= t < μ + 5}, 
                                         {Pulse[t, {μ + δp, 1}], μ + 5 <= t}}]

where μ is the mean. Next I combine them into a list and try to plot them with manipulate: First make the list,

 TwoSig[t_, δμ_, δp_] := {TwoPulse[t, δp], TwoPulse[t, δμ, δp]}

Then plot:

Manipulate[
 Plot[TwoSig[t, p, r], {t, -10, 30}, PlotRange -> All, 
  PlotStyle -> {Directive[Blue], Directive[Orange]}], {p, 0, 10}, {r, 
  10, 20}]

At first, both the list elements were in the same color, so I tried adding PlotStyleand Directives. Usually Mathematica is very good at plotting different functions, items, etc. in different colors. But I think what I have done [a. overloading TwoPulse and b. using the same parameter δp has successfully confused Mathematica (or ... me)].

Same color plots

How can I get them to be different colors (figure is supposed to be a GIF, needs to be clicked?).

ITA
  • 393
  • 1
  • 8
  • 2
    Is there a reason for using TwoSig[t, p, r] in the Plot rather than {TwoPulse[t, r], TwoPulse[t, p, r]}? If you use the second form, Mathematica has no issues realizing that there are 2 functions. If you prefer to use TwoSig, then you can just wrap it in Evaluate like Plot[Evaluate[TwoSig[t, p, r]], ...]. – MassDefect Feb 10 '21 at 01:07

1 Answers1

2

Plot looks at the unevaluated form of the first argument. If it sees one function the plot will be in one color, only when the unevaluated first argument consists of several functions, multicolor are used. Therefore, you need to force evaluation of the first argument:

Manipulate[
 Plot[Evaluate@TwoSig[t, p, r], {t, -10, 30}, PlotRange -> All, 
  PlotStyle -> {Directive[Blue], Directive[Orange]}], {p, 0, 10}, {r, 
  10, 20}]

enter image description here

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57