How to Convert this Code to For loop / Function?

Hi all,

I plot a series manually, then I thought I want to learn to use for loop / function. I don’t know which one is better.

The series are famous:

a_{n} = \frac{(-1)^{n}}{n}

For n even it will be \frac{1}{n}

For n odd it will be - \frac{1}{n}

When I try this code below I am unable to use && I can’t use i and j, moreover the graph does not even show up:

using Plots, LaTeXStrings
gr()

scatter([1], [1], color = "red", markersize = 4,
    legend=:outerright, label="", framestyle=:zerolines, 
    xlims = (0,30), xticks = 0:1:30, 
    ylims = (-2,2), yticks = -2:4:2, 
    linestyle=:dot, size=(720, 360))

for i in 1:2:33
    scatter!([i], [(-1)^(i)/i], color = "red", label="", markersize = 4)

end

for i in 2:2:33 
    scatter!([i], [(-1)^(i)/i], color = "red", label="", markersize = 4)
end

This is the manual code:

using Plots, LaTeXStrings
gr()

a, b = -10, 10

h(x) = 1

plot(h, a, b; legend=:outerright, label="", framestyle=:zerolines, 
    xlims = (0,10), xticks = 0:1:10, 
    ylims = (-2,2), yticks = -2:4:2, 
    linestyle=:dot, size=(720, 360))
plot!([1,0],[1,1], label="", linecolor=:green, linestyle=:dash)
plot!([2,0],[2,2], label="", linecolor=:green, linestyle=:dash)
plot!([3,0],[3,3], label="", linecolor=:green, linestyle=:dash)
plot!([4,0],[4,4], label="", linecolor=:green, linestyle=:dash)

scatter!([1], [-1], color = "red", label=L"a_{n}=\frac{1}{n}", markersize = 4)
scatter!([2], [1/2], color = "green", label=L"a_{n}=- \frac{1}{n}", markersize = 4)
scatter!([3], [-1/3], color = "red", label="", markersize = 4)
scatter!([4], [1/4], color = "green", label="", markersize = 4)
scatter!([5], [-1/5], color = "red", label="", markersize = 4)
scatter!([6], [1/6], color = "green", label="", markersize = 4)
scatter!([7], [-1/7], color = "red", label="", markersize = 4)
scatter!([8], [1/8], color = "green", label="", markersize = 4)
scatter!([9], [-1/9], color = "red", label="", markersize = 4)
scatter!([10], [1/10], color = "green", label="", markersize = 4)

annotate!([(5.1,0.2, (L"\mathbb{R}", 10, :black)), 
           (0.3,4.82, (L"\mathbb{R}", 10, :black))])

take a look at the display function as follows

using Plots
using LaTeXStrings
gr()

p = scatter([1], [1], color = "red", markersize = 4,
   legend=:outerright, label="", framestyle=:zerolines, 
   xlims = (0,30), xticks = 0:1:30, 
   ylims = (-2,2), yticks = -2:4:2, 
   linestyle=:dot, size=(720, 360))

for i in 1:2:33
   scatter!([i], [(-1)^(i)/i], color = "red", label="", markersize = 4)
end

display(p)
1 Like

Do you really need to plot each point in a separate scatter plot? You can scatterplot all the points at once:

f(n::Integer) = (-1)^isodd(n) / n  # or just use an anonymous function inside the plot command
p = scatter(1:33, f; color=:red, label=L"a_n=n", markersize=4)
display(p)

It’s better to do (-1)^isodd(n) or (isodd(n) ? 1 : -1) or something like that, instead of (-1)^n, which will take a long time, because it has to do a lot of power calculations.

1 Like

Full code:

p = hline([1]; legend=:outerright, label="", framestyle=:zerolines, 
    xlims = (0,10), xticks = 0:1:10, 
    ylims = (-2,5.2), yticks = -2:4:2, 
    linestyle=:dot, size=(720, 360))
for i in 1:4
    plot!(p, [i, 0], [i, i], label="", linecolor=:green, linestyle=:dash)
end
scatter!(p, 1:33, n->(-1)^isodd(n)/n; color=:red, label=L"a_n=n", markesize=4)

annotate!(p, (5.1,0.2, (L"\mathbb{R}", 10, :black)), 
             (0.3,4.82, (L"\mathbb{R}", 10, :black)))
annotate!(p, [(-0.23, 0.08+i, (L"a_{%$i}", 10, :red)) for i in 1:4])
display(p)

1 Like

(and no need to call display when the last line is a call to annotate! as that will return the plot object)

Hi @DNF

I gave wrong color before, I edit the code at my post above,

here is what I want to achieve with for loop / function:

I try your code:

I want to ask how to make different color for n even the color of the scatter will be green ? and if n odd the color of the scatter will be red.

the plot does not show up in Jupyter Notebook without display(p)

Here is the winner:

There is little reason to make that many plots. Just make one plot for odd and one for even.

scatter!(p, 1:2:33, n->(-1)^isodd(n)/n; color=:red, label=L"a_n=1/n", markesize=4) 
scatter!(p, 2:2:33, n->(-1)^isodd(n)/n; color=:green, label=L"a_n=-1/n", markesize=4)
2 Likes

33 is just for fun number, when I try your label it only shows 1, the other code showing 33 label of the same

Anyway, why you use isodd?

Do you want one label for each point? I thought you wanted one label for the odd values and one for the even ones.

It’s not really needed anymore when you separate the even and odd points, but I used it instead of (-1)^n, because (-1)^isodd(n) is much more efficient.

1 Like

Alright, I get it so you use isodd because it is more efficient.

You are right, maybe my language is not efficient thus create misunderstanding.

I want one label for the odd values and another one for the even values, yours are right. Not one for each point, that is so not efficient.