How to pass multiple dimension arrays into a function in Julia? - julia

I am still learning the language Julia, however, i am writing a population model that describes the dynamics of 100 mosquito subpopulations. I have many different functions but i think the error lays in the passing to my main function.
xf = XLSX.readxlsx("C:/Scriptie_mosquitoes/knmi_csv.xlsx")
sh = xf["knmi_csv"]
temperature = sh["B3:B368"]
precip = sh["F3:F368"]
subpopulation_amount = 100
imat_list = zeros(subpopulation_amount,length(temperature))
adul_list = zeros(subpopulation_amount,length(temperature))
egg_list = zeros(subpopulation_amount,length(temperature))
diaegg_list = zeros(subpopulation_amount,length(temperature))
imat_list[1] = 100.0
adul_list[1] = 1000.0
egg_list[1] = 100.0
diaegg_list[1] = 100.0
for counter = 1:subpopulation_amount
u = Normal()
temp_change = rand(u)
tempa = temperature .+ temp_change
e = Normal()
precip_change = rand(e)
main(counter,tempa,precip,precip_change,imat_list[:],adul_list[:],egg_list[:],diaegg_list[:])
end
This is the error shown
julia> for counter = 1:subpopulation_amount
u = Normal()
temp_change = rand(u)
tempa = temperature .+ temp_change
e = Normal()
precip_change = rand(e)
main(counter,tempa,precip,precip_change,imat_list,adul_list,egg_list,diaegg_list)
end
ERROR: UndefVarError: Point2f0 not defined
Stacktrace:
[1] newImmaturetoAdult(::Float64) at .\REPL[1137]:9
[2] main(::Int64, ::Array{Float64,2}, ::Array{Any,2}, ::Float64, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,2}) at .\REPL[1158]:10
[3] top-level scope at .\REPL[1187]:7
I would very much appreciate it if you guys would like to help me. Anything can help.
Thanks!
I will add the whole code below as well.
using Statistics
using Plots
using JuliaDB
using XLSX
using Distributions
using Random
# Possible struct for climaticfactors per day
struct Climaticfactors
temperature
precipiation
evaporation
photoperiod
end
# First function written, describes the development rate of larvae to adults. on Temperature
# function developRate(temp)
# # function
# develop = (-1/225)*(temp-25)^2+1
# # Check if development is not negative
# if develop < 0
# develop = 0
# return develop
# else
# return develop
# end
# end
# Linear segments between the data points. The function finds the percentage of larvae growing up on temperature
function newImmaturetoAdult(temp)
# Data point from literature
x = [5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0]
y = [0.0, 0.0, 50.0, 77.5, 76.3, 67.5, 2.5, 0.0]
# Loop that goes through all possible temperatures
for i in 1:length(x)-1
# If in the temperature falls between a segment find that point
if temp >= x[i] && temp <= x[i+1]
a = LineSegment(Point2f0(x[i], y[i]), Point2f0(x[i+1], y[i+1]))
b = LineSegment(Point2f0(temp,0.0),Point2f0(temp,100.0))
j = intersects(a,b)
c = j[2]
# Returning the percentage
return c[2]
# Temperatures do nothing
elseif temp>40.0 || temp<5.0
return 0.0
end
end
end
# Same technique as before but with egg to larvae on temperature
function eggtolarvea(temp)
# Data from literature
x = [5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0]
y = [4.4, 4.0, 8.2, 66.9, 49.2, 51.4, 10, 0.0]
for i in 1:(length(x)-1)
if temp >= x[i] && temp <= x[i+1]
a = LineSegment(Point2f0(x[i], y[i]), Point2f0(x[i+1], y[i+1]))
b = LineSegment(Point2f0(temp,0.0),Point2f0(temp,100.0))
j = intersects(a,b)
c = j[2]
return c[2]
elseif temp>40.0 || temp<5.0
return 0.0
end
end
end
# function humidityindex(temp, precip...)
# moist_effect = 0
# temp_effect = 0
# for i in 1:size(precip)
# moist_effect += ((precip[i]/100) * (1-(i/10)))
# temp_effect -= ((temp[i]/25) * (1-(i/10)))
# end
# effect = temp_effect + moist_effect
# return effect
# end
function diapauserate(day)
y = 0.045*day - 10.75
end
# Egg_population dynamics
function eggpop(hatch,effect,diarate,egg_pop, adult_pop)
# if eggs don't hatch do nothing
if hatch == 0
egg_pop *= 0.8
end
if effect > 0
tot = adult_pop * 10
dia_pop = tot * diarate
egg_pop = tot - dia_pop
else
tot = adult_pop * 4
dia_pop = tot * diarate
egg_pop = tot - dia_pop
end
return dia_pop, egg_pop
end
# Larvae population dynamics
function immaturepop(develop_rate, hatch, immature_pop, egg_pop)
# Hatch encreases larvae population
if hatch == 0
change = immature_pop * develop_rate
immature_pop -= change
else
change = immature_pop * develop_rate
immature_pop -= change
immature_pop += egg_pop * hatch
end
return immature_pop
end
# Adult population dynamics
function adultpop(develop_rate,immature_pop,adult_pop)
# New adults
change = immature_pop * develop_rate
adult_pop = adult_pop + change
# Survival of 80%
adult_pop *= 0.95
return adult_pop
end
# Main
function main(counter,temperature,precip, precip_change,imat_list,adul_list,egg_list,diaegg_list)
# Iterating through temperature data (year)
for i in 2:1:length(temperature);
# Only from 10 march to 30 september mosquito season, otherwise diapause
if i >= 69 && i <= 273;
# temperatures in 1 degrees celcius
temp = temperature[i] / 10;
# Development and hatch values in 0.02 values
develop = newImmaturetoAdult(temp) / 100;
# println(develop)
hatch = eggtolarvea(temp) / 100;
# println(hatch)
# Moist_index
evap = collect(1:1:7);
precipi = collect(1:1:7);
for j in 1:7
t = temperature[-j+i];
evap[j]= t;
p = precip[-j+i];
precipi[j] = p;
end
moist_effect = 0;
temp_effect = 0;
for h in 1:length(precipi)
moist_effect += ((precipi[h]/100.0) * (1.0-(h/10.0)));
temp_effect -= ((evap[h]/200.0) * (1.0-(h/10.0)));
end
effect = temp_effect + moist_effect + precip_change;
if diapauserate(i) <= 0
diarate = 0 ;
elseif diapauserate(i) >= 1
diarate = 1;
else
diarate = diapauserate(i);
end
egg = eggpop(hatch,effect,diarate, egg_list[counter,i-1],adul_list[counter,i-1]);
egg_list[counter, i] = egg[2]
diaegg_list[counter, i] = egg[1]
# Changing and adding eggpop
# Changing and adding immature pop
ima = immaturepop(develop, hatch, imat_list[counter,i], egg_list[counter,i-1]);
imat_list[counter,i] = ima
# Changing and adding adult pop
adu = adultpop(develop, imat_list[counter,i-1], adul_list[counter,i]);
adul_list[counter,i] = adu
# from 30 september keep egg_level the same but larvae and adult decreasing
elseif i > 273 && i < 365
egg_list[counter,i] = egg_list[counter,i-1]/1.1
diaegg_list[counter,i] = diaegg_list[counter,i-1]
imat_list[counter,i] = imat_list[counter,i-1]/1.1
adul_list[counter,i] = adul_list[counter,i-1]/1.1
else i > 0 && i < 69
egg_list[counter,i] = egg_list[counter,i-1]
diaegg_list[counter,i] = diaegg_list[counter,i-1]
imat_list[counter,i] = imat_list[counter,i-1]
adul_list[counter,i] = adul_list[counter,i-1]
end
end
end
# using Distributions, Random
# Random.seed!(123)
# td = Truncated(Normal(0, 0.05),-5,5)
# x = rand(td, 10000000)'
# using Distributions
# using Random
# Random.seed!(123)
# d = Normal()
# x = rand(d, 100)
# Importing KNMI data
xf = XLSX.readxlsx("C:/Scriptie_mosquitoes/knmi_csv.xlsx")
sh = xf["knmi_csv"]
temperature = sh["B3:B368"]
precip = sh["F3:F368"]
# Starting values
# imat_list = collect(1.0:1:length(temperature));
# adul_list = collect(1.0:1:length(temperature));
# egg_list = collect(1.0:1:length(temperature));
# diaegg_list = collect(1.0:1:length(temperature));
subpopulation_amount = 100
imat_list = zeros(subpopulation_amount,length(temperature))
adul_list = zeros(subpopulation_amount,length(temperature))
egg_list = zeros(subpopulation_amount,length(temperature))
diaegg_list = zeros(subpopulation_amount,length(temperature))
imat_list[1] = 100.0
adul_list[1] = 1000.0
egg_list[1] = 100.0
diaegg_list[1] = 100.0
for counter = 1:subpopulation_amount
u = Normal()
temp_change = rand(u)
tempa = temperature .+ temp_change
e = Normal()
precip_change = rand(e)
main(counter,tempa,precip,precip_change,imat_list,adul_list,egg_list,diaegg_list)
end
# # Visualizing
# populaties = [imat_list, adul_list]
# x = 1:1:length(temperature)
# p1 = plot(x,populaties, title= "Populatie niveaus", label = ["ImmaturePopulatie" "AdultPopulatie"], lw = 3)
# plot(p1)
# # plot(x,egg_list, title="Egg population")
# egg_popu = [egg_list, diaegg_list]
# p2 = plot(x,egg_popu, title="Egg populations", label=["Eggpop" "DiaeggPop"])
# plot(p1,p2, layout=(2,1), legend=true)

You forgot to import the package that defines Point2f0:
using GeometryTypes
BTW your structs should have types for fields - never used untyped structs because of performance.
Moreover to generate normally distributed numbers use randn().
It is also worth noting that when defining a distribution parameters such as Normal() it is enough to do it once and than reuse the object.

Related

Creating histogram for 10 coins flipped simultaneously for 1000 times using R

I am new to R and just working on a statistics class project.
I have to create a histogram for 10 simultaneous coin flips, 1000 times.
Here is my code for generating the 1000 flips and counting number of heads based on the assignment.
# one coin toss
coin <- c('heads','tails')
tossResult = NULL
# variables to store the counts of number of heads in each toss
heads7 = 0
headsLessOrEquatTo7 = 0
headsLessThat7 = 0
heads7OrMore = 0
headsMoreThan7 = 0
heads3 = 0
heads4 = 0
for (i in 1:1000) {
# resetting number of heads to 0 before each iteration
numOfHeads = 0
tossResult = sample (coin, size = 10, replace = TRUE)
numOfHeads = sum (tossResult == 'heads')
hist(numOfHeads)
if (numOfHeads == 7) {
heads7 = heads7 + 1
}
if (numOfHeads <= 7) {
headsLessOrEquatTo7 = headsLessOrEquatTo7 + 1
}
if (numOfHeads < 7) {
headsLessThat7 = headsLessThat7 + 1
}
if (numOfHeads >= 7) {
heads7OrMore = heads7OrMore + 1
}
if (numOfHeads > 7) {
headsMoreThan7 = headsMoreThan7 + 1
}
}
print (paste0("Exactly 7 Heads:: ", heads7))
print (paste0("7 Heads or fewer:: ", headsLessOrEquatTo7))
print (paste0("Fewer than 7 Heads:: ", headsLessThat7))
print (paste0("7 Heads or more:: ", heads7OrMore))
print (paste0("More than 7 Heads:: ", headsMoreThan7))
I need to produce the histogram for the number of heads in each iteration. Any help is appreciated.
You could create a function that counts number of heads in one iteration.
count_heads <- function() {
tossResult = sample(c('heads','tails'), 10, replace = TRUE)
sum(tossResult == 'heads')
}
Use replicate to repeat it 1000 times and plot histogram
hist(replicate(1000, count_heads()), xlab = "number of heads",
main = "Histogram of number of heads")

I have written a path tracer using julia programming language but i think it is slow

I have changed my post and posted the whole of my code! Could someone tell me how can I optimize it?
import Base: *, +, -, /, ^
using Images
const Π = convert(Float64, π)
#define vector
mutable struct Vec3
x::Float64
y::Float64
z::Float64
end
function +(u::Vec3, v::Vec3)
Vec3(u.x+v.x, u.y+v.y, u.z+v.z)
end
function -(u::Vec3, v::Vec3)
Vec3(u.x-v.x, u.y-v.y, u.z-v.z)
end
function /(u::Vec3, v::Float64)
Vec3(u.x/v, u.y/v, u.z/v)
end
function *(u, v::Vec3)
if typeof(u) == Float64
Vec3(u*v.x, u*v.y, u*v.z)
elseif typeof(u) == Vec3
Vec3(u.x*v.x, u.y*v.y, u.z*v.z)
end
end
function ^(u::Vec3, v::Float64)
Vec3(u.x^v, u.y^v, u.z^v)
end
function dot(u::Vec3, v::Vec3)
u.x*v.x + u.y*v.y + u.z*v.z
end
function normalize(u::Vec3)
u/sqrt(dot(u,u))
end
function cross(u::Vec3, v::Vec3)
Vec3(u.y*v.z - v.y*u.z, u.z*v.x - v.z*u.x, u.x*v.y - v.x*u.y)
end
function gamma(u::Vec3)
Vec3(u.x^(1/2.2), u.y^(1/2.2), u.z^(1/2.2))
end
function clamp(u::Vec3)
u.x = u.x <= 1 ? u.x : 1
u.y = u.y <= 1 ? u.y : 1
u.z = u.z <= 1 ? u.z : 1
u
end
#define ray
struct Ray
s::Vec3
d::Vec3
end
#define planes
struct xyRect
z; x1; x2; y1; y2::Float64
normal; emittance; reflectance::Vec3
isLight::Bool
end
struct xzRect
y; x1; x2; z1; z2::Float64
normal; emittance; reflectance::Vec3
isLight::Bool
end
struct yzRect
x; y1; y2; z1; z2::Float64
normal; emittance; reflectance::Vec3
isLight::Bool
end
#define sphere
mutable struct Sphere
radius::Float64
center; normal; emittance; reflectance::Vec3
isLight::Bool
end
#define empty object
struct Empty
normal; emittance; reflectance::Vec3
end
#define surfaces
Surfaces = Union{xyRect, xzRect, yzRect, Sphere}
#define intersection function
function intersect(surface::Surfaces, ray::Ray)
if typeof(surface) == xyRect
t = (surface.z - ray.s.z)/ray.d.z
if surface.x1 < ray.s.x + t*ray.d.x < surface.x2 && surface.y1 < ray.s.y + t*ray.d.y < surface.y2 && t > 0
t
else
Inf
end
elseif typeof(surface) == xzRect
t = (surface.y - ray.s.y)/ray.d.y
if surface.x1 < ray.s.x + t*ray.d.x < surface.x2 && surface.z1 < ray.s.z + t*ray.d.z < surface.z2 && t > 0
t
else
Inf
end
elseif typeof(surface) == yzRect
t = (surface.x - ray.s.x)/ray.d.x
if surface.y1 < ray.s.y + t*ray.d.y < surface.y2 && surface.z1 < ray.s.z + t*ray.d.z < surface.z2 && t > 0
t
else
Inf
end
elseif typeof(surface) == Sphere
a = dot(ray.d, ray.d)
b = 2dot(ray.d, ray.s - surface.center)
c = dot(ray.s - surface.center, ray.s - surface.center) - surface.radius*surface.radius
Δ = b*b - 4*a*c
if Δ > 0
Δ = sqrt(Δ)
t1 = 0.5(-b-Δ)/a
t2 = 0.5(-b+Δ)/a
if t1 > 0
surface.normal = normalize(ray.s + t1*ray.d - surface.center)
t1
elseif t2 > 0
surface.normal = normalize(ray.s + t2*ray.d - surface.center)
t2
else
Inf
end
else
Inf
end
end
end
#define nearest function
function nearest(surfaces::Array{Surfaces, 1}, ray::Ray, tMin::Float64)
hitSurface = Empty(Vec3(0,0,0), Vec3(0,0,0), Vec3(0,0,0))
for surface in surfaces
t = intersect(surface, ray)
if t < tMin
tMin = t
hitSurface = surface
end
end
tMin, hitSurface
end
#cosine weighted sampling of hemisphere
function hemiRand(n::Vec3)
ξ1 = rand()
ξ2 = rand()
x = cos(2π*ξ2)*sqrt(ξ1)
y = sin(2π*ξ2)*sqrt(ξ1)
z = sqrt(1-ξ1)
r = normalize(Vec3(2rand()-1, 2rand()-1, 2rand()-1))
b = cross(n,r)
t = cross(n,b)
Vec3(x*t.x + y*b.x + z*n.x, x*t.y + y*b.y + z*n.y, x*t.z + y*b.z + z*n.z)
end
#trace the path
function trace(surfaces::Array{Surfaces, 1}, ray::Ray, depth::Int64, maxDepth::Int64)
if depth >= maxDepth
return Vec3(0,0,0)
end
t, material = nearest(surfaces, ray, Inf)
if typeof(material) == Empty
return Vec3(0,0,0)
end
if material.isLight == true
return material.emittance
end
ρ = material.reflectance
BRDF = ρ/Π
n = material.normal
R = hemiRand(n)
In = trace(surfaces, Ray(ray.s + t*ray.d, R), depth+1, maxDepth)
return Π*BRDF*In
end
#define camera
struct Camera
eye; v_up; N::Vec3
fov; aspect; distance::Float64
end
#render function
function render(surfaces::Array{Surfaces,1},camera::Camera,xRes::Int64,yRes::Int64,numSamples::Int64,maxDepth::Int64)
n = normalize(camera.N)
e = camera.eye
c = e - camera.distance*n
θ = camera.fov*(π/180)
H = 2*camera.distance*tan(θ/2)
W = H*camera.aspect
u = normalize(cross(camera.v_up,n))
v = cross(n,u)
img = zeros(3, xRes, yRes)
pixHeight = H/yRes
pixWidth = W/xRes
L = c - 0.5*W*u - 0.5*H*v
for i=1:xRes
for j=1:yRes
cl = Vec3(0,0,0)
for s=1:numSamples
pt = L + (i-rand())*pixWidth*u + (yRes-j+rand())*pixHeight*v
cl = cl + trace(surfaces, Ray(e, pt-e), 0, maxDepth)
end
cl = gamma(clamp(cl/convert(Float64, numSamples)))
img[:,j,i] = [cl.x, cl.y, cl.z]
end
end
img
end
#the scene
p1 = xzRect(1.,0.,1.,-1.,0.,Vec3(0,-1,0),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
p2 = xzRect(0.,0.,1.,-1.,0.,Vec3(0,1,0),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
p3 = xyRect(-1.,0.,1.,0.,1.,Vec3(0,0,1),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
p4 = yzRect(0.,0.,1.,-1.,0.,Vec3(1,0,0),Vec3(0,0,0),Vec3(0.75,0.25,0.25),false)
p5 = yzRect(1.,0.,1.,-1.,0.,Vec3(-1,0,0),Vec3(0,0,0),Vec3(0.25,0.25,0.75),false)
p6 = xzRect(0.999,0.35,0.65,-0.65,-0.35,Vec3(0,-1,0),Vec3(18,18,18),Vec3(0,0,0),true)
s1 = Sphere(0.15,Vec3(0.3,0.15,-0.6),Vec3(0,0,0),Vec3(0,0,0),Vec3(0.75,0.75,0.75),false)
surfs = Surfaces[p1,p2,p3,p4,p5,p6,s1]
cam = Camera(Vec3(0.5,0.5,2),Vec3(0,1,0),Vec3(0,0,1),28.07,1,2)
#time image = render(surfs, cam, 400, 400, 1, 4);
colorview(RGB, image)
I need to know why my code is bad and slow. I am a beginner programmer and I don't have enough experience. My path tracer scene contains 7 objects, its maximum depth is 4, and it takes more than 2 seconds to generate an image of size 400*400. I think It shouldn't be that slow because my cpu is core i7 4770.
Sorry for changing my post.
To start with,
struct yzRect
x; y1; y2; z1; z2::Float64
normal; emittance; reflectance::Vec3
isLight::Bool
end
ends up only applying the type to the last variable for each line:
julia> fieldtypes(yzRect)
(Any, Any, Any, Any, Float64, Any, Any, Vec3, Bool)
so Julia will basically not know about any types in your structs which slows things down.
Also, your Vec3 should really be immutable and you then just create new instances of it when you want to "modify" the Vector.
There might be many more issues but those are two standing out.
Reading through https://docs.julialang.org/en/v1/manual/performance-tips/index.html and applying the guidelines in there is strongly recommended when analyzing performance.

error bernstein vandermonde julia.

the following error occurs. I tried to change the n .... but not working
"LoadError: BoundsError: attempt to access 9-element Array{Float64,1}:"
function bernstein_vandermonde( n )
if n == 1
v = ones(1, 1);
return v
end
v = zeros( n, n );
x = linspace( 0, 1, n );
for i = 1:n
println("entra no loop")
v[i,1:n] = bernstein_poly_01(n - 1, x[i])
end
return v
end
function bernstein_poly_01( n, x )
bern = ones(n)
if n == 0
bern[1] = 1
elseif 0 < n
bern[1] = 1 -x
bern[2] = x
for i = 2:n
bern[i+1] = x*bern[i];
for j = i-1:-1: 1
bern[j+1] = x*bern[j] + (1 - x)*bern[j+1]
end
bern[1] = (1 - x)*bern[1]
end
end
return bern
end
I can not solve :(

Invalid index error in a for loop

I've been working in this code for a while, it's pretty simple, just recursive calculation. At the while loop, in the first line I got an index error. Funny thing is before I start using the if statement in loop my index was fine and I could compile it. My believe my problem is the lack of familiarity with the scilab way of programming.
///CONSTANTES////
roh_H20 = 1030.00 // [kg/m^3] Água Salgada
roh_ar = 1.22 // [kg/m^3]
g = 9.81 // [m/s^2]
V_elo = 0.00041 // [m^3]
V_boia = 0.00811 // [m^3]
V_lastro = 0.00811 // [m^3]
D_corda = 0.030 // [m]
A_corda = 1/4*%pi*D_corda**2
L_corda = 30 // [m]
roh_corda = 905 // [kg/m^3] poliester
V_corda = A_corda*L_corda // [m^3]
roh_elo = 952 // [kg/m^3] PE Alta Densidade
m_elo = 0.38817 // [kg]
m_boia = 1.5654 // [Kg]
m_lastro = 2.5 // [kg] CHUTE
m_corda = roh_corda*V_corda; // [kg]
Cd = 1.3 // coeficiente de arrasto placa plana FOX pg 396
N_elo = 7*24 // conjuntos a cada 1 metro
N_corda = 4 // A definir
N_boia = 7 // 2 primeiros conjuntos
N_lastro = 7 // 2 últimos conjuntos
/// PROBLEMA ESTATICO ////
M = N_elo*m_elo + N_boia*(m_boia + m_lastro) // FALTA CORDA
P = M*g
Emp = g*(N_boia*V_boia + N_elo*V_elo + N_lastro*V_lastro)*roh_H20;
printf("\n*****PROBLEMA ESTATICO*****\n")
printf("\nO Peso do conjunto eh: %f N\n", P)
printf("\nO Empuxo eh: %f N\n", Emp)
/// PROBLEMA DINÂMICO ///
A_vela = 0.03230 // [m^2] dado pelo SolidWorks
N_vela = 7
printf("\n*****PROBLEMA DINAMICO*****\n")
a = zeros(0,1e3)
a(1)= (Emp-P)/ M
printf("\na_0 = %f [m/s^2]\n", a(1))
t=0
v= zeros(0,1e3)
dy = zeros(0,1e3)
Fd = zeros(0,1e3)
for i=1:50
// if i == 1 then
// a(i)= (Emp-P)/ M
// else
// 1+1
// end
v(i+1) = v(i) + a(i)*t
dy(i+1) = dy(i)+v(i+1)*t+ a(i)*t*t/2.0
Fd(i+1) = 0.5*roh_H20*Cd*N_vela*A_vela*v(i+1)**2
if v(i+1) > 0 then
a(i+1) = (Emp - P - Fd(i+1))/ M;
elseif v(i+1) <= 0 then
a(i+1) = (Emp - P + Fd(i+1))/ M;
end,
t=t+0.05
end
x = linspace(0,t,int(i))
// Grafico velocidade/aceleração
clf(0)
scf(0)
subplot(2,1,1)
plot(x,a,'ro-')
xlabel("Tempo [s]")
ylabel("Acelaração [m/s^2]")
legend("aceleração")
subplot(2,1,2)
plot(x,v,'bs:')
xlabel("Tempo [s]")
ylabel("velocidade [m/s]")
legend("velocidade")
// Grafico forca/deslocamento
clf(1)
scf(1)
subplot(2,1,1)
plot(x,Fd,'r--d')
xlabel("Tempo [s]")
ylabel("Força [N]")
legend("Força Arrasto")
subplot(2,1,2)
plot(x,dy,'m--^')
xlabel("Tempo [s]")
ylabel("Descolcamento [m]")
legend("deslocamento")
Whenever you have index error, there are two things to do in the console:
Check the value of the index variable (just by entering its name)
Check the size(s) of any arrays involved (using the command size)
In this case, checking the size of a would show it's not what you expected it to be. Why? Because of this:
a = zeros(0,1e3)
v= zeros(0,1e3)
dy = zeros(0,1e3)
Fd = zeros(0,1e3)
The arguments of zeros are the height and width of a matrix. You are asking for height 0... this means empty matrix, since it has no rows. What you want is a row vector, which is a matrix with one row.
After fixing this you'll run into a problem with
x = linspace(0,t,int(i))
plot(x,a,'ro-')
because x and a are not of the same size.
To keep sizes consistent, you could do something like this:
imax = 50;
a = zeros(1,imax)
v= zeros(1,imax)
dy = zeros(1,imax)
Fd = zeros(1,imax)
and later for i=1:imax and x = linspace(0,t,imax).
This way you are not creating oversized arrays, but only arrays of the size you want, which can be adjusted by changing the value of imax.

How can I generate a set of points evenly distributed along the perimeter of an ellipse?

If I want to generate a bunch of points distributed uniformly around a circle, I can do this (python):
r = 5 #radius
n = 20 #points to generate
circlePoints = [
(r * math.cos(theta), r * math.sin(theta))
for theta in (math.pi*2 * i/n for i in range(n))
]
However, the same logic doesn't generate uniform points on an ellipse: points on the "ends" are more closely spaced than points on the "sides".
r1 = 5
r2 = 10
n = 20 #points to generate
ellipsePoints = [
(r1 * math.cos(theta), r2 * math.sin(theta))
for theta in (math.pi*2 * i/n for i in range(n))
]
Is there an easy way to generate equally spaced points around an ellipse?
This is an old thread, but since I am seeking the same task of creating evenly spaced points along and ellipse and was not able to find an implementation, I offer this Java code that implements the pseudo code of Howard:
package com.math;
public class CalculatePoints {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
*
dp(t) = sqrt( (r1*sin(t))^2 + (r2*cos(t))^2)
circ = sum(dp(t), t=0..2*Pi step 0.0001)
n = 20
nextPoint = 0
run = 0.0
for t=0..2*Pi step 0.0001
if n*run/circ >= nextPoint then
set point (r1*cos(t), r2*sin(t))
nextPoint = nextPoint + 1
next
run = run + dp(t)
next
*/
double r1 = 20.0;
double r2 = 10.0;
double theta = 0.0;
double twoPi = Math.PI*2.0;
double deltaTheta = 0.0001;
double numIntegrals = Math.round(twoPi/deltaTheta);
double circ=0.0;
double dpt=0.0;
/* integrate over the elipse to get the circumference */
for( int i=0; i < numIntegrals; i++ ) {
theta += i*deltaTheta;
dpt = computeDpt( r1, r2, theta);
circ += dpt;
}
System.out.println( "circumference = " + circ );
int n=20;
int nextPoint = 0;
double run = 0.0;
theta = 0.0;
for( int i=0; i < numIntegrals; i++ ) {
theta += deltaTheta;
double subIntegral = n*run/circ;
if( (int) subIntegral >= nextPoint ) {
double x = r1 * Math.cos(theta);
double y = r2 * Math.sin(theta);
System.out.println( "x=" + Math.round(x) + ", y=" + Math.round(y));
nextPoint++;
}
run += computeDpt(r1, r2, theta);
}
}
static double computeDpt( double r1, double r2, double theta ) {
double dp=0.0;
double dpt_sin = Math.pow(r1*Math.sin(theta), 2.0);
double dpt_cos = Math.pow( r2*Math.cos(theta), 2.0);
dp = Math.sqrt(dpt_sin + dpt_cos);
return dp;
}
}
(UPDATED: to reflect new packaging).
An efficient solution of this problem for Python can be found in the numeric branch FlyingCircus-Numeric, derivated from the FlyingCircus Python package.
Disclaimer: I am the main author of them.
Briefly, the (simplified) code looks (where a is the minor axis, and b is the major axis):
import numpy as np
import scipy as sp
import scipy.optimize
def angles_in_ellipse(
num,
a,
b):
assert(num > 0)
assert(a < b)
angles = 2 * np.pi * np.arange(num) / num
if a != b:
e2 = (1.0 - a ** 2.0 / b ** 2.0)
tot_size = sp.special.ellipeinc(2.0 * np.pi, e2)
arc_size = tot_size / num
arcs = np.arange(num) * arc_size
res = sp.optimize.root(
lambda x: (sp.special.ellipeinc(x, e2) - arcs), angles)
angles = res.x
return angles
It makes use of scipy.special.ellipeinc() which provides the numerical integral along the perimeter of the ellipse, and scipy.optimize.root()
for solving the equal-arcs length equation for the angles.
To test that it is actually working:
a = 10
b = 20
n = 16
phi = angles_in_ellipse(n, a, b)
print(np.round(np.rad2deg(phi), 2))
# [ 0. 17.55 36.47 59.13 90. 120.87 143.53 162.45 180. 197.55
# 216.47 239.13 270. 300.87 323.53 342.45]
e = (1.0 - a ** 2.0 / b ** 2.0) ** 0.5
arcs = sp.special.ellipeinc(phi, e)
print(np.round(np.diff(arcs), 4))
# [0.3022 0.2982 0.2855 0.2455 0.2455 0.2855 0.2982 0.3022 0.3022 0.2982
# 0.2855 0.2455 0.2455 0.2855 0.2982]
# plotting
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
ax.axes.set_aspect('equal')
ax.scatter(b * np.sin(phi), a * np.cos(phi))
plt.show()
You have to calculate the perimeter, then divide it into equal length arcs. The length of an arc of an ellipse is an elliptic integral and cannot be written in closed form so you need numerical computation.
The article on ellipses on wolfram gives you the formula needed to do this, but this is going to be ugly.
A possible (numerical) calculation can look as follows:
dp(t) = sqrt( (r1*sin(t))^2 + (r2*cos(t))^2)
circ = sum(dp(t), t=0..2*Pi step 0.0001)
n = 20
nextPoint = 0
run = 0.0
for t=0..2*Pi step 0.0001
if n*run/circ >= nextPoint then
set point (r1*cos(t), r2*sin(t))
nextPoint = nextPoint + 1
next
run = run + dp(t)
next
This is a simple numerical integration scheme. If you need better accuracy you might also use any other integration method.
I'm sure this thread is long dead by now, but I just came across this issue and this was the closest that came to a solution.
I started with Dave's answer here, but I noticed that it wasn't really answering the poster's question. It wasn't dividing the ellipse equally by arc lengths, but by angle.
Anyway, I made some adjustments to his (awesome) work to get the ellipse to divide equally by arc length instead (written in C# this time). If you look at the code, you'll see some of the same stuff -
void main()
{
List<Point> pointsInEllipse = new List<Point>();
// Distance in radians between angles measured on the ellipse
double deltaAngle = 0.001;
double circumference = GetLengthOfEllipse(deltaAngle);
double arcLength = 0.1;
double angle = 0;
// Loop until we get all the points out of the ellipse
for (int numPoints = 0; numPoints < circumference / arcLength; numPoints++)
{
angle = GetAngleForArcLengthRecursively(0, arcLength, angle, deltaAngle);
double x = r1 * Math.Cos(angle);
double y = r2 * Math.Sin(angle);
pointsInEllipse.Add(new Point(x, y));
}
}
private double GetLengthOfEllipse()
{
// Distance in radians between angles
double deltaAngle = 0.001;
double numIntegrals = Math.Round(Math.PI * 2.0 / deltaAngle);
double radiusX = (rectangleRight - rectangleLeft) / 2;
double radiusY = (rectangleBottom - rectangleTop) / 2;
// integrate over the elipse to get the circumference
for (int i = 0; i < numIntegrals; i++)
{
length += ComputeArcOverAngle(radiusX, radiusY, i * deltaAngle, deltaAngle);
}
return length;
}
private double GetAngleForArcLengthRecursively(double currentArcPos, double goalArcPos, double angle, double angleSeg)
{
// Calculate arc length at new angle
double nextSegLength = ComputeArcOverAngle(majorRadius, minorRadius, angle + angleSeg, angleSeg);
// If we've overshot, reduce the delta angle and try again
if (currentArcPos + nextSegLength > goalArcPos) {
return GetAngleForArcLengthRecursively(currentArcPos, goalArcPos, angle, angleSeg / 2);
// We're below the our goal value but not in range (
} else if (currentArcPos + nextSegLength < goalArcPos - ((goalArcPos - currentArcPos) * ARC_ACCURACY)) {
return GetAngleForArcLengthRecursively(currentArcPos + nextSegLength, goalArcPos, angle + angleSeg, angleSeg);
// current arc length is in range (within error), so return the angle
} else
return angle;
}
private double ComputeArcOverAngle(double r1, double r2, double angle, double angleSeg)
{
double distance = 0.0;
double dpt_sin = Math.Pow(r1 * Math.Sin(angle), 2.0);
double dpt_cos = Math.Pow(r2 * Math.Cos(angle), 2.0);
distance = Math.Sqrt(dpt_sin + dpt_cos);
// Scale the value of distance
return distance * angleSeg;
}
From my answer in BSE here .
I add it in stackoverflow as it is a different approach which does not rely on a fixed iteration steps but rely on a convergence of the distances between the points, to the mean distance.
So the calculation is shorter as it depends only on the wanted vertices amount and on the precision to reach (about 6 iterations for less than 0.01%).
The principle is :
0/ First step : calculate the points normally using a * cos(t) and b * sin(t)
1/ Calculate the lengths between vertices
2/ Adjust the angles variations depending on the gap between each distance to the mean distance
3/ Reposition the points
4/ Exit when the wanted precision is reached or return to 1/
import bpy, bmesh
from math import radians, sqrt, cos, sin
rad90 = radians( 90.0 )
rad180 = radians( 180.0 )
def createVertex( bm, x, y ): #uses bmesh to create a vertex
return bm.verts.new( [x, y, 0] )
def listSum( list, index ): #helper to sum on a list
sum = 0
for i in list:
sum = sum + i[index]
return sum
def calcLength( points ): #calculate the lenghts for consecutives points
prevPoint = points[0]
for point in points :
dx = point[0] - prevPoint[0]
dy = point[1] - prevPoint[1]
dist = sqrt( dx * dx + dy *dy )
point[3] = dist
prevPoint = point
def calcPos( points, a, b ): #calculate the positions following the angles
angle = 0
for i in range( 1, len(points) - 1 ):
point = points[i]
angle += point[2]
point[0] = a * cos( angle )
point[1] = b * sin( angle )
def adjust( points ): #adjust the angle by comparing each length to the mean length
totalLength = listSum( points, 3 )
averageLength = totalLength / (len(points) - 1)
maxRatio = 0
for i in range( 1, len(points) ):
point = points[i]
ratio = (averageLength - point[3]) / averageLength
point[2] = (1.0 + ratio) * point[2]
absRatio = abs( ratio )
if absRatio > maxRatio:
maxRatio = absRatio
return maxRatio
def ellipse( bm, a, b, steps, limit ):
delta = rad90 / steps
angle = 0.0
points = [] #will be a list of [ [x, y, angle, length], ...]
for step in range( steps + 1 ) :
x = a * cos( angle )
y = b * sin( angle )
points.append( [x, y, delta, 0.0] )
angle += delta
print( 'start' )
doContinue = True
while doContinue:
calcLength( points )
maxRatio = adjust( points )
calcPos( points, a, b )
doContinue = maxRatio > limit
print( maxRatio )
verts = []
for point in points:
verts.append( createVertex( bm, point[0], point[1] ) )
for i in range( 1, len(verts) ):
bm.edges.new( [verts[i - 1], verts[i]] )
A = 4
B = 6
bm = bmesh.new()
ellipse( bm, A, B, 32, 0.00001 )
mesh = bpy.context.object.data
bm.to_mesh(mesh)
mesh.update()
Do take into consideration the formula for ellipse perimeter as under if the ellipse is squashed. (If the minor axis is three times as small as the major axis)
tot_size = np.pi*(3*(a+b) -np.sqrt((3*a+b)*a+3*b))
Ellipse Perimeter
There is working MATLAB code available here. I replicate that below in case that link ever goes dead. Credits are due to the original author.
This code assumes that the major axis is a line segment from (x1, y1) to (x2, y2) and e is the eccentricity of the ellipse.
a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
t = linspace(0,2*pi, 20);
X = a*cos(t);
Y = b*sin(t);
w = atan2(y2-y1,x2-x1);
x = (x1+x2)/2 + X*cos(w) - Y*sin(w);
y = (y1+y2)/2 + X*sin(w) + Y*cos(w);
plot(x,y,'o')
axis equal

Resources