mathplotjulia

Evaluate symbolically defined function for an array in Julia


I would like to evaluate the symbolically defined function below and plot the results using Julia.

f(x) = 2*x*(1-x/10) for x in (0,20].

How would I go about this?


Solution

  • The previous answer works well enough, but I'll just point out that f is not a symbolic function (as you would encounter in Symbolics.jl), but just a regular function, so there's no need to do using Symbolics.

    Also, Plots.jl has an extra trick up its sleeve. You don't need to manually calculate the sampled values, you can just give it the function and the plot range, and it will figure out appropriate sampling points, like this:

    using Plots
    
    f(x) = 2x * (1 - x/10)
    plot(f, 0, 20; label="f(x) = 2x(1-x/10)", xlabel="x", ylabel="f(x)", lw=2)
    

    If you want to jazz up the plot a bit, you can use LaTeX in the plot as well:

    using LaTeXStrings  # gives you the `L` string macro
    plot(f, 0, 20; label=L"f(x) = 2x\,(1-x/10)", xlabel=L"x", ylabel=L"f(x)", lw=2)