TransWikia.com

Giving a range to Plot to plot multiple lines at the same time

Mathematica Asked on August 22, 2021

I’d like to plot something like this:

enter image description here

Except that instead of manually specifying all the functions to be plotted, I specify a range. So for example I could feed the command Range[0,6,2] to get it to plot four lines on the same plot, i.e. $mathrm{Sin}[0],mathrm{Sin}[2x],mathrm{Sin}[4x]$ and $mathrm{Sin}[6x]$.

How can I do this? I tried Plot[Sin[# x], {x, 0, 2 Pi}] & /@ Range[1, 3] which yields the desired three lines, but each in their own plot as opposed to one. Attempting to map twice with Plot[#, {x, 0, 2 Pi}] & /@ {Sin[# x] /@ Range[1, 3]} yields no plot at all.

3 Answers

Plot[Sin[# x] & /@ Range[1, 3] // Evaluate, {x, 0, 2 Pi}]

Correct answer by cvgmt on August 22, 2021

Try Show to combine the plots

Show[Plot[Sin[# x], {x, 0, 2 Pi}] & /@ Range[1, 3] ]

or

Plot[Table[Sin[om x], {om, Range[1, 3] }] // Evaluate, {x, 0, 2 Pi}]

Answered by Ulrich Neumann on August 22, 2021

Note that Sin is Listable

Attributes[Sin]

(* {Listable, NumericFunction, Protected} *)

Plot[Evaluate@Sin[Range[3] x], {x, 0, 2 Pi},
 PlotLegends -> "Expressions"]

enter image description here

Answered by Bob Hanlon on August 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP