Convert Symbolics.jl expression to plottable function

The solution here isn’t working for me. I am getting “not a valid function argument name” error when trying to use built function.

julia> using Symbolics, UnicodePlots

julia> @variables a x b
3-element Vector{Num}:
 a
 x
 b

julia> y = a*x+b
b + a*x

julia> dict = Dict(a => 2, b => 3)
Dict{Num, Int64} with 2 entries:
  a => 2
  b => 3

julia> y_unique = substitute(y, dict)
3 + 2x

julia> y_func(x) = eval(build_function(y_unique, x))
y_func (generic function with 1 method)

julia> y_func(1)
ERROR: syntax: "1" is not a valid function argument name
Stacktrace:
 [1] top-level scope
   @ none:1
 [2] eval
   @ .\boot.jl:373 [inlined]
 [3] eval
   @ .\client.jl:453 [inlined]
 [4] y_func(x::Int64)
   @ Main .\REPL[28]:1
 [5] top-level scope
   @ REPL[29]:1

julia> lineplot(y_func, 0, 5)
ERROR: syntax: "0" is not a valid function argument name
Stacktrace:
  [1] top-level scope
    @ none:1
  [2] eval
    @ .\boot.jl:373 [inlined]
  [3] eval
    @ .\client.jl:453 [inlined]
  [4] y_func(x::Float64)
    @ Main .\REPL[28]:1
  [5] (::UnicodePlots.var"#130#131"{typeof(y_func)})(i::Float64)
    @ UnicodePlots .\none:0
  [6] iterate
    @ .\generator.jl:47 [inlined]
  [7] collect(itr::Base.Generator{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, UnicodePlots.var"#130#131"{typeof(y_func)}})
    @ Base .\array.jl:724
  [8] lineplot(f::typeof(y_func), x::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}; name::String, xlabel::String, ylabel::String, kw::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:width,), Tuple{Int64}}})
    @ UnicodePlots C:\Users\nboyer.AIP\.julia\packages\UnicodePlots\QW1wc\src\interface\lineplot.jl:205
  [9] #lineplot#132
    @ C:\Users\nboyer.AIP\.julia\packages\UnicodePlots\QW1wc\src\interface\lineplot.jl:220 [inlined]
 [10] lineplot(f::Function, startx::Int64, endx::Int64)
    @ UnicodePlots C:\Users\nboyer.AIP\.julia\packages\UnicodePlots\QW1wc\src\interface\lineplot.jl:218
 [11] top-level scope
    @ REPL[30]:1

You cannot list function variables on the left side of the equation when using build_function.

Changing the 6th line as below fixed the error:

julia> y_func = eval(build_function(y_unique, x))
#1 (generic function with 1 method)

Continuing on:

julia> y_func(1)
5

julia> lineplot(y_func, 0, 5)
           ┌────────────────────────────────────────┐
        13 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡤⠊│ #1(x)
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡤⠖⠁⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠔⠋⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡔⠊⠁⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠎⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠔⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡔⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
   f(x)    │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠎⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠔⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡤⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠖⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⠀⠀⠀⣀⠔⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⠀⠀⢀⡤⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           │⠀⠀⠀⡠⠖⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
         3 │⣀⠔⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
           └────────────────────────────────────────┘
           ⠀0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀5⠀
           ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀x⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
1 Like