Reputation: 55
However I try to plot a function including a sum I get errors. Can´t find any example anywhere.
using PyPlot
x = range(0,stop=2,length=10)
f(x) = x + 1
plot(x,f(x))
for example gives me:
MethodError: no method matching +(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Int64)
Upvotes: 0
Views: 63
Reputation: 623
The problem is not the plotting. The problem is that you try to add a number (1
) to a range (x
). This is also what the error message states. You need to do it element-wise. Like in e.g. matlab this is achieved with .
-operations.
There are two possibilities for you in this example.
f(x) = x .+ 1; plot(x,f(x))
f(x) = x + 1; plot(x,f.(x))
Take a look at https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators-1
Upvotes: 2