Suppose I want to plot a certain function f(x) = a*x(1-x)
, for x in 0:0.001:1
, and to consider, i.e., four different values for parameter a: a = 1
, a = 2
, a = 3.2
, a = 3.8
. I want to do this in one single plot. I know how to do it using either the plot!
function or, as an alternative, creating four different functions and plotting all of them in one single plot plot([f1 ,f2, f3, f4], 0:0.001:1, kwarg)
. For example, the figure below shows what I have in mind:
The problem with the coding style I follow to produce that plot is that it is both ugly and inefficient (because we may have more parameter values than just 4). Trying to turn around these limitations, the best I could come up with was this piece of code:
using Plots, LaTeXStrings
#x ∈ -3:0.01:3
g(x, a) = a*x*(1-x);
fig = plot(); # produces an empty plot
for a = [1, 2, 3.2, 3.8] # the for loop
plot!(fig, [g(x, a) for x ∈ 0:0.01:1]) # the loop fills in the plot with this
end
plot(fig, label= [L"a=1" L"a=2" L"a=3.2" L"a= 3.8"],
fg_legend = :transparent, legend = :topleft) #just produces the plot
This code leads to the following plot:
The two problems with this plot are:(i) the scale of the x-axis follows the number of iterations (from 1 to 101, instead of 0 to 1 as desired), (ii) it misrepresents the true function we intend to plot, and it becomes useless.
I tried many variations on that piece of code, but I was unsuccessful. I have carefully checked the net for hints but found no clues. Help would be very appreciated. This is for teaching, and I do not want my students to see ugly code.