I only ran from a terminal. I’m not sure, but WGLMakie might work in place of GLMakie.
For an x, given f(x) and f'(x), if you want the tangent as y = a + bx, then just match values and derivatives, ie f(x) = a + bx and f'(x) = b, obtaining a = f(x) - x f'(x).
using Plots
using ForwardDiff
f(x)=((x)^6)-2
fp(x)= ForwardDiff.derivative(f, x)
t(x)=-1*(f(x)-xfp(x)+fp(x))
P1=plot(f,1:.001:1.55,legend=false);hline!([0])
for i in 1:9
root=x
x=BigFloat(x)-BigFloat(f(x)/fp(x))
e=BigFloat(abs((x)-(root)))
push!(df,(x,e,f(x)))
t(x)=-(f(root)-xfp(root)+fp(root))
plot!(t,1:.001:1.55)
vline!([root])
end
this gives me lines with the correct slope, but they don't move with root or x, to be tangents.
Okay, this should work in Jupyter. The @manipulate
macro from SimplePlots
allows the interaction with the initial point.
using Plots, Interact
using ForwardDiff
D(f) = x -> ForwardDiff.derivative(f, float(x))
f(x) = x^5 - x - 1
a, b = -1.5, 1.5
nsteps = 20
@manipulate for x0=slider(a:0.1:b, value=1.0, label="x0")
ts = range(a, b, length=251)
plot(ts, f.(ts), xlim=(a,b), ylim=(-5,5), legend=false)
plot!(ts, 0 .* ts)
scatter!([x0],[0])
xs = Float64[]
ys = Float64[]
for i in 1:nsteps
x1 = x0 - f(x0)/D(f)(x0)
append!(xs, [x0, x0, x1])
append!(ys, [0,f(x0), 0])
x0 = x1
end
plot!(xs, ys)
end
[Edited to use Plots+Interact]
I,m getting slider not defined
UndefVarError: slider not defined
Stacktrace:
[1] top-level scope at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\interact\gui.jl:39
[2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
Don’t know if this is helpful but:
Also, see this:
Thanks, not what I was thinking,but an interesting approach. I’ll think about it.
You might need the master version using Pkg; Pkg.add("SimplePlots#master")
I’m told that’s not a valid package name. What version of Julia are you using?
now I get
MethodError: no method matching getindex(::typeof(f), ::Int64)
Stacktrace:
[1] (::SimplePlots.var"#16#17")(::Function) at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\plots\plots.jl:16
[2] plot!(::SimplePlots.SimplePlot, ::Function, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Tuple{Real,Real},Tuple{Symbol,Symbol},NamedTuple{(:xlim, :ylim),Tuple{Tuple{Float64,Float64},Tuple{Int64,Int64}}}}) at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\plots\plots.jl:23
[3] plot(::Function, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Tuple{Real,Real},Tuple{Symbol,Symbol},NamedTuple{(:xlim, :ylim),Tuple{Tuple{Float64,Float64},Tuple{Int64,Int64}}}}) at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\plots\plots.jl:3
[4] (::var"#21#22")(::Float64) at .\In[12]:9
[5] top-level scope at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\interact\gui\make_gui_block.jl:13
[6] top-level scope at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\interact\gui.jl:40
[7] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
Ooops, that Plots
syntax is something I have loaded privately. Add these two lines and comment out the offending one.
ts = range(a, b, length=250)
plot(ts, f.(ts), xlim=(a,b), ylim=(-5,5))
#plot(f, a, b, xlim=(a,b), ylim=(-5,5))
Back to saying scatter not deffined.
I got an eroor trying to install Simple Plots master, and instead did
(@v1.5) pkg> add SimplePlots#master
Not sure, that function is exported here: https://github.com/djsegal/SimplePlots.jl/blob/master/src/plots/plots.jl#L62
I edited the script posted earlier. Try restarting and running that entire script in a cell to make sure all the packages are loading. If that fails, you can do this with Plots
and Interact
in the same manner, I just thought this would be easier to get going.
still says scatter! not defined
function plot(varargs...; kwargs...)
simple_plot = SimplePlot(; kwargs...)
plot!(simple_plot, varargs...; kwargs...)
end
function scatter(varargs...; kwargs...)
simple_plot = SimplePlot(; kwargs...)
scatter!(simple_plot, varargs...; kwargs...)
end
it looks like scatter needs to be defined as a function.
Okay, I edited the script from an earlier post to use Plots
+ Interact
. SimplePlots
was only intended to make things easier. You may need to install Interact
, but try the above after a restart of the kernel.
I can’t execute it in the REPL. it seems to be missing a bracket
It said I didn’t have Web IO, so I put
using WebIO
using IJulia
but it says unable to load WebIO
I’m confused. Interact runs in jupyter, not the REPL. But copy and paste should work without a syntax error.