I have doubts about using Pots.jl

I’m trying to plot this function x = 0.2 * cos (2 * t + π) but there is something wrong with the definition of t.


using Pkg
using Plots

t=range(0,stop=10, length=100) #0:0.01:2π
y = 0.2

plot(t, y, label=“cos(x)”)
plot!(xlab=“t(s)”, ylab=“x(m)”)


Yeah, it’s a typo. lenght is spelled length.

You also don’t need to Pkg.add("Plots") once you already have plots. After running that command once (ever) you only need using Plots


Thank you very much, but I already made the changes and it still doesn’t work. I changed the expression of t to 0: 0.01: 10 and it didn’t work either.

should be

y = 0.2 .* cos.(2 .* t .+ π )

or equivalently

y = @. 0.2 * cos(2t + π)

Anyway the point is this has nothing to do with the Plot.jl package. I suggest you take a look at the julia manual to sort out syntactical issues.

By the way, I am using ` and ``` to quote my code so it’s nice to read :slightly_smiling_face:


It worked! Apologies if it looked like I was blaming the package. I’m new to Julia and in the community maybe I chose the wrong tags. Thank you very much for your help.

Here, you need a .+ when adding a scalar to a vector, like 0.2cos.(2t .+ π), or as suggested by @tomerarnon, use @. 0.2cos(2t + π). Also note, there is no space after function names cos ( or between 0.2 cos. Finally, range can be written shorter as below.

using Plots

t = range(0, 10, length=100) # 0:0.01:2π
y = 0.2cos.(2t .+ π)

plot(t, y, label="cos(x)")
plot!(xlab="Time (s)", ylab="Distance (m)")

Are these points included 0.2cos. (2t. + π) due to Broadcasting? And
can you explain to me the use of this macro @. 0.2cos (2t + π)?

1 Like

@. inserts a dot between all operators and function calls. If you are new to Julia, using the help system will be useful. Just start julia and enter ?@. and press enter.