RandomName42
RandomName42

Reputation: 55

Plotting function with sums

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

Answers (1)

max xilian
max xilian

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.

  • either f(x) = x .+ 1; plot(x,f(x))
  • or 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

Related Questions