Plotting graph in Julia with Float64 error - julia

using Plots
α = 1
γ = 1.5
y(x) = -α(1-(γ*x)^(2))*exp(-0.5*γ*x^(2))
plot(y,-3,3)
I have no idea how to solve this error.
MethodError: objects of type Int64 are not callable
1. y(::Float64)#Local: 2
2. (::Base.var"#62#63"{RecipesPipeline.var"#7#8"{Symbol},typeof(Main.workspace315.y)})(::Float64)#operators.jl:875
3. (::PlotUtils.var"#27#29"{Base.var"#62#63"{Base.var"#62#63"{RecipesPipeline.var"#7#8"{Symbol},typeof(Main.workspace315.y)},RecipesPipeline.var"#9#10"{Symbol}}})(::Float64)#adapted_grid.jl:46
4. _broadcast_getindex_evalf#broadcast.jl:648[inlined]
5. _broadcast_getindex#broadcast.jl:621[inlined]
6. getindex#broadcast.jl:575[inlined]
7. copy#broadcast.jl:876[inlined]
8. materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,PlotUtils.var"#27#29"{Base.var"#62#63"{Base.var"#62#63"{RecipesPipeline.var"#7#8"{Symbol},typeof(Main.workspace315.y)},RecipesPipeline.var"#9#10"{Symbol}}},Tuple{Array{Float64,1}}})#broadcast.jl:837
9. #adapted_grid#26(::Int64, ::Float64, ::typeof(PlotUtils.adapted_grid), ::Any,
::Tuple{Float64,Float64})#adapted_grid.jl:57
10. adapted_grid(::Any, ::Tuple{Float64,Float64})#adapted_grid.jl:16
11. _scaled_adapted_grid(::Function, ::Symbol, ::Symbol, ::Int64, ::Int64)#user_recipe.jl:353
12. macro expansion#user_recipe.jl:293[inlined]
13. apply_recipe(::AbstractDict{Symbol,Any}, ::Function, ::Number,
::Number)#RecipesBase.jl:283
14. _process_userrecipes!(::Any, ::Any, ::Any)#user_recipe.jl:36
15. recipe_pipeline!(::Any, ::Any, ::Any)#RecipesPipeline.jl:70
16. _plot!(::Plots.Plot, ::Any, ::Any)#plot.jl:208
17. #plot#154#plot.jl:91[inlined]
18. plot(::Any, ::Any, ::Any)#plot.jl:85
19. top-level scope#Local: 3

short answer: the function should be defined as:
y(x) = -α * (1 - (γ * x)^2) * exp(-0.5γ * x^2)
The longer answer: the problem is not with the plotting because calling the y function would yield a similar error message. To answer the "how " to solve the error, I recommend testing the code bit by bit.
While Julia automatically understands a multiplication between a float and a variable (like the 0.5γ), Julia understood that you wanted to use α as a function and because α is an integer, it raised the error.

Related

Plot complex inequality in Julia

I have the mathematical expression |z - (-1)| < 1, with z element of Complexes, which is equivalent of a circle of radius 1 centered in (x,y)=(-1,0).
How can I plot this expression,
preserving the structure of the mathematical expression it was derived from, as much as
possible?
It should be an area.
What I tried so far:
using ImplicitEquations, Plots
f(a,b) = abs.(a+im*b - (-1))
plot(f<1)
The error I got:
ERROR: MethodError: no method matching isless(::typeof(f), ::Int64)
Closest candidates are:
isless(::Union{StatsBase.PValue, StatsBase.TestStat}, ::Real) at /home/buddhilw/.julia/packages/StatsBase/PGTj8/src/statmodels.jl:514
isless(::AbstractGray{T} where T, ::Real) at /home/buddhilw/.julia/packages/ColorTypes/6m8P7/src/operations.jl:31
isless(::ForwardDiff.Dual{Tx, V, N} where {V, N}, ::Integer) where
Tx at /home/buddhilw/.julia/packages/ForwardDiff/UDrkY/src/dual.jl:144
...
Stacktrace:
[1] <(x::Function, y::Int64)
# Base ./operators.jl:279
[2] top-level scope
# REPL[62]:1
There's not a lot of documentation for ImplicitEquations, but something stands out: you're not using the right operators. The package relies on unusual operators to represent math expressions with Julia functions: ≪ (\ll[tab]), ≦ (\leqq[tab]), ⩵ (\Equal[tab]), ≶ (\lessgtr[tab]) or ≷ (\gtrless[tab]), ≧ (\geqq[tab]), ≫ (\leqq[tab]).
So that fix would look like:
using ImplicitEquations, Plots
f(a,b) = sqrt((a+1)^2 + b^2)
plot(f ≪ 1)
Update:
f(a,b) = abs(a + im*b - (-1)) causes a method ambiguity error. f(a, b) = hypot(a+1, b), which is what abs calls, also causes the error. It looks like the issue is that at some point in hypot, OInterval(x::Ointerval) is called, but dispatch could not pick between (::Type{T})(x::T) where T<:Number in boot.jl or OInterval(a) in intervals.jl. Just redefining OInterval(a::Ointerval) = a won't work either because you run into another MethodError for decompose(::OInterval), which is a method intended for processing floats. Looking at the comments in intervals.jl, the dispatch seems like a work in progress.

Plot an array of plots as subplots with Plots.jl

Continuation of this thread: How to create an arbitrary number of subplots in Julia Plots
When I tried
using Plots
plot_array = Any[]
for i in 1:5
push!(plot_array, plot(rand(10))) # make a plot and add it to the plot_array
end
plot(plot_array)
I received Error:
MethodError: no method matching Plots.Plot{Plots.PlotlyBackend}(::Char, ::Char, ::Char, ::Char, ...)
Closest candidates are: Plots.Plot{Plots.PlotlyBackend}(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any)
What did I miss?
You need to "splat" the array of subplots in the last plot call using ...:
using Plots
plot_array = []
for i in 1:5
push!(plot_array, plot(rand(10)))
end
plot(plot_array...) # note the "..."
will produce something like

2D graph in Scilab

I have to make a 2D graph in Scilab, and I'm not sure if the code I wrote is correct.
This problem aims for 2-D plot.
Plot the function y = x2 − 2 in the interval [-5, 5]
I have to graph this equation in Scilab and I'm not sure if the way y wrote it is correct or not.
--> x = -5 : 5;
--> y = x^2 - 2
Warning: Syntax "vector ^ scalar" is obsolete. It will be removed in Scilab 6.0. Use "vector .^ scalar" instead.
y =
23. 14. 7. 2. - 1. - 2. - 1. 2. 7. 14. 23.
--> plot(x,y)
The only weak point in your code is
y=x^2-2 instead of y=x.^2-2

Julia how to eval the sym

Help! I got a result in the form of sym. But it seems eval doesn't work. How can I get a numerical answer? Thanks.
#show BBias
#show eval(BBias)
#show typeof(eval(BBias))
BBias = -213.53387843501*cos(6) + 73.4119295548356*sin(6) - 50*sin(6)*cos(6) + 316.255048160247
eval(BBias) = -213.53387843501*cos(6) + 73.4119295548356*sin(6) - 50*sin(6)*cos(6) + 316.255048160247
typeof(eval(BBias)) = SymPy.Sym
What you have here is a symbolic SymPy expression, not a Julia expression. eval in Julia will only evaluate a Julia expression. The N function from SymPy will evaluate and expression down to it's floating point value.
You can do a typeof(BBias) to see what kind of an object this is. Since I don't know how you have generated that object, I cannot replicate it fully. But here is some simple example showing how to using SymPy from Julia
julia> using SymPy
julia> x=Sym("pi")
pi
julia> typeof(x)
SymPy.Sym
julia> y=sin(x)
0
julia> typeof(y)
SymPy.Sym
julia> typeof(eval(y))
SymPy.Sym
julia> z=N(y)
0
julia> typeof(z)
Int64
A detailed tutorial on using SymPy from Julia is available here: http://nbviewer.jupyter.org/github/jverzani/SymPy.jl/blob/master/examples/tutorial.ipynb

Recursion in Prolog - Finding Path Between Cities

I'm trying to work my way through the exercises at the bottom of this page and I find myself utterly confused on number 3.
We are given the following knowledge base of travel information:
byCar(auckland, hamilton).
byCar(hamilton, raglan).
byCar(valmont, saarbruecken).
byCar(valmont, metz).
byTrain(metz, frankfurt).
byTrain(saarbruecken, frankfurt).
byTrain(metz, paris).
byTrain(saarbruecken, paris).
byPlane(frankfurt, bangkok).
byPlane(frankfurt, singapore).
byPlane(paris, losAngeles).
byPlane(bangkok, auckland).
byPlane(singapore, auckland).
byPlane(losAngeles, auckland).
It's simple to find out if it's possible to travel between two cities. I just did this:
connected(X, Y) :- byCar(X, Y); byTrain(X, Y); byPlane(X, Y).
travel(X, Y) :- connected(X, Y).
travel(X, Z) :- connected(Y, Z), travel(X, Y).
However, when I have to actually unify the path with a variable, I am utterly confused!
I wrote this:
connected(X, Y) :- byCar(X, Y); byTrain(X, Y); byPlane(X, Y).
connected(Y, Z, Out) :- connected(Y, Z).
travel(X, Y, Out) :- connected(X, Y).
travel(A, Z, Out) :- connected(Y, Z),travel(A, Y, connected(Y, Z, Out)).
And called travel(valmont, losAngeles,X).
There is a point during the trace where the correct path shows up, aside from the anonymous variable at the end:
travel(valmont, metz, connected(metz, paris, connected(paris, losAngeles, _17)))
but I don't actually know how to unify this with the variable X!
I can't really wrap my mind around this. Can anyone give me a hint just to push me in the right direction? Is there just a termination condition I'm missing or something?
Edit:
Now I have:
connected(X,Y) :- byCar(X,Y);byTrain(X,Y);byPlane(X,Y).
go(X,Y) :- connected(X,Y).
travel(X,Y,go(X,Y)) :- connected(X,Y).
travel(A,Z,Path) :- travel(Y,Z,Path),go(A,Y,Path).
go(A,Y,Path) :- travel(A,Y,Path).
but it gets stuck like this:
4 4 Exit: byPlane(paris,losAngeles) ?
3 3 Exit: connected(paris,losAngeles) ?
2 2 Exit: travel(paris,losAngeles,go(paris,losAngeles)) ?
5 2 Call: go(metz,paris,go(paris,losAngeles)) ?
6 3 Call: travel(metz,paris,go(paris,losAngeles)) ?
7 4 Call: travel(_217,paris,go(paris,losAngeles)) ?
8 5 Call: travel(_242,paris,go(paris,losAngeles)) ?
9 6 Call: travel(_267,paris,go(paris,losAngeles)) ?
10 7 Call: travel(_292,paris,go(paris,losAngeles)) ?
I've played around with it, but I can't get it to build the whole go(a,b,go(b,c)) etc...
I'll give you the base case of the recursion:
travel(X, Y, go(X, Y)) :- connected(X, Y).
The recursive case looks extremely similar, except that the go/3 term you're building must have locations as its first two arguments, and a path (another go/2 or go/3 term) as its second.
I had this explained to me by aBathologist the following way:
Your objective is to get X = go(valmont,metz,go(metz,paris,go(paris,losAngeles))) in response to the query travel(valmont,losAngeles,X).
To solve this problem, your travel/3 predicate needs to have a From, To, and Path, but it must end with a simple go(From, To) without the Path. The simple go(From, To) is your base condition of travel/3, so:
travel(X, Y, go(X, Y)) :- connected(X, Y).
This is exactly as larsmans states.
Now, you need to create your recursive travel/3 predicate:
travel(X, Y, go(X, Z, Path)) :-
connected(X, Z),
travel(Z, Y, Path).
Your go/2 predicate is redundant and a little confusing given the travel/3 predicate has something that looks like a go predicate. By removing the go/2 predicate, the code is a little easier to read and understand.

Resources