Assign x values is plots

using Roots
using Plots

J=f(x)=x^2+10sin(x)
g(x)=f(x)-1000
find_zeros(g,0,10000)

plot(J)

The above works fine, but I want to plot a horizontal line at 1000, and plot J, so that x goes from 0 to 100. I’m not sure from the documentation, how to assign 0 values

something like

using Roots
using Plots

J=f(x)=x^2+10sin(x)
g(x)=f(x)-1000
find_zeros(g,0,10000)

plot(J, 0, 100)
hline!([1000])

?
I’m not really sure, what you mean by

how to assign 0 values

Assign them to what?

1 Like

to x, I want to assign a range of 0, 100 to x, so that it shows x for that range. Which is exactly what you provided! Thanks!

1 Like

Nice function find_roots.

You don’t need to name the function by J, unless there is another reason for introducing it.

I played with a modification of your problem:

using Plots, LaTeXStrings, Roots
f(x) = (x/4)^2 +10sin(x+pi/3)
g(x) = (x/5)^2
h(x) = f(x)-g(x)
xr = find_zeros(h,0,20)
#
hline(f.(xr),lw=4,lc=:orange,la=0.15,label="")
vline!(xr,lw=4,lc=:orange,la=0.15,label="")
plot!(f,0,20,lw=3,lc=:cornflowerblue,label=L"f(\cdot)")
plot!(g,0,20,lw=3,lc=:crimson,label=L"g(\cdot)")
scatter!(xr,f.(xr),ms=7,mc=:orange,label="roots",legend=:topleft)

which produces:

Julia is nice :-). I love the support of html colors, see, e.g., HTML Color Names .

3 Likes

I thought I dod for plots, but it looks like you assign values for plots. I like the graph, but the big thing is to show that since f(x) is continuous, there is value for every point, even irrationals, like sqrt(1000)

What is LaTexStrings? I think I need to work more with LaTex, I’ve gotten handy with the equation functions in Word.

It’s not clear what you want to show. Could you please be more specific?

Just the that that f is the function f(x) when plotting. I changed it in Julia afterwards.

Package LaTeXStrings allows for a modifier (L) placed in front of strings ("..."), e.g., L"...", which inserts some \LaTeX code and makes it simpler to construct strings with math. So I can write L"f(\cdot)" which the LaTeXStrings package changes to the appropriate string of \LaTeX code.

To appreciate LaTeXStrings, you need to find a book on \LaTeX/\TeX math, and study the documentation of LaTeXStrings.

LaTeXStrings is not perfect, essentially because \LaTeX uses symbol $ to bracket math, while $ is simultaneously used in Julia as “interpolator” in text strings. I don’t have any perfect solution to this, though.

1 Like

You want the function nameof giving the name of a function

julia> function g(x)
       x + 1
       end
g (generic function with 1 method)

julia> function myplot(fun, x)
           plot(fun.(x), label = string(nameof(fun)))
       end
myplot (generic function with 1 method)

julia> x = [1, 2, 3];

julia> myplot(g, x)
2 Likes

Yes, I want to study LaTex, but I’m in a rush to study everything.

1 Like