0
$\begingroup$

I tried this:

f[x_] := (x^2 - 1) (x^2 - 2)
g[x_] := (x^2 - 1) (x^2 - 2) + 3 sin (20 x)
p1 = Plot[f[x], {x, 0, 3}, PlotStyle -> Orange]
p2 = Plot[g[x], {x, 0, 3}, PlotStyle -> Blue]
Show[p1, p2]

However, that didn't produce a single plot with the two functions in different colors that I expected. What am I doing wrong here?

$\endgroup$
  • 3
    $\begingroup$ Functions in Mathematica start with a capital letter and use square braces, e.g. use Sin[20 x] instead of sin (20 x). $\endgroup$ – user6014 Aug 27 '19 at 16:59
  • 1
    $\begingroup$ Thanks, @user6014, of course! Sin[20 x] worked as expected. $\endgroup$ – Michael Aug 27 '19 at 17:09
  • 1
    $\begingroup$ You can use a single Plot: Plot[{f[x], g[x]}, {x, 0, 3}, PlotStyle -> {Orange, Blue}, PlotLegends -> Placed["Expressions", {0.5, 0.5}]] $\endgroup$ – Bob Hanlon Aug 27 '19 at 17:28
2
$\begingroup$

After fixing the syntax error, you can also use your list of functions in the first argument and a list of colors in PlotStyle option setting to get the desired result more conveniently with a single Plot command:

Plot[{f[x], g[x]}, 
  {x, 0, 3}, 
  PlotStyle -> {Orange, Blue}, 
  PlotLegends -> "Expressions"]

enter image description here

$\endgroup$

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