1

I created a UnitRange in Julia and trying to create a plot of x with respect to x and x ^ 2. I have written the following code.

x = [-10:10]
p1 = plot(x, x)
p2 = plot(x, x.^2)

I am getting the following error:

MethodError: no method matching ^(::UnitRange{Int64}, ::Int64)
Closest candidates are:
  ^(!Matched::Float16, ::Integer) at math.jl:795
  ^(!Matched::Missing, ::Integer) at missing.jl:120
  ^(!Matched::Missing, ::Number) at missing.jl:93
  ...

Stacktrace:
 [1] _broadcast_getindex at ./none:0 [inlined]
 [2] getindex at ./broadcast.jl:515 [inlined]
 [3] copy at ./broadcast.jl:790 [inlined]
 [4] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Base.literal_pow),Tuple{Base.RefValue{typeof(^)},Array{UnitRange{Int64},1},Base.RefValue{Val{2}}}}) at ./broadcast.jl:756
 [5] top-level scope at In[18]:3

What is the mistake in my code?

double-beep
  • 5,031
  • 17
  • 33
  • 41

1 Answers1

0

You can change:

x.^2

to:

x[1].^2
double-beep
  • 5,031
  • 17
  • 33
  • 41