如何计算Julia中的散乱点 [英] How to count scattered points in Julia

查看:19
本文介绍了如何计算Julia中的散乱点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算一下圆圈内散落的红点。
我的代码是:

using PyPlot # Here I define the circle

    k = 100
    ϕ = range(0,stop=2*π,length=k)
    c =  cos.(ϕ)
    d =  sin.(ϕ)

# Here I defined the scattered points with the circle

function scatterpoints(x,y)
    n = 1000
    x = -n:n
    x = x /  n
    y = rand(2*n+1)

    scatter(-x, -y;c="red",s=1)
    scatter(x, y;c="red", s=1)
    plot(c,d)
end

scatterpoints(x,y)

我的方法(伪代码)如下所示:

using LinearAlgebra
if norm < radius of circle then
amount of points in circle = amount of points in circle + 1
end

遗憾的是,我不确定如何在Julia中实现此功能。

推荐答案

您的伪代码就在这里

using LinearAlgebra

n = 1000
N = 2n+1

x = range(-1, 1, length=N)
y = rand(N)

center = (0,0)
radius = 1

n_in_circle = 0

for i in 1:N
    if norm((x[i], y[i]) .- center) < radius
        n_in_circle += 1
    end
end

println(n_in_circle) # 1565
println(pi*N/4)      # 1571.581724958294

这篇关于如何计算Julia中的散乱点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆