如何在Julia中将多维数组传递到函数中?
我仍在学习语言Julia,但是,我正在编写一个种群模型,描述100个蚊子亚种群的动态。我有很多不同的函数,但我认为错误在于传递给我的主函数如何在Julia中将多维数组传递到函数中?,julia,Julia,我仍在学习语言Julia,但是,我正在编写一个种群模型,描述100个蚊子亚种群的动态。我有很多不同的函数,但我认为错误在于传递给我的主函数 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(subp
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
这是显示的错误
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
如果你们愿意帮助我,我将非常感激。任何事都能帮上忙。
谢谢
我也将在下面添加整个代码
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)
使用统计信息
使用绘图
使用JuliaDB
使用XLSX
使用分布
使用随机
#每日气候因子的可能结构
结构气候因子
温度
沉淀
蒸发
光周期
结束
#首先写函数,描述幼虫到成虫的发育速度。在温度上
#功能开发(临时)
##功能
#发展=(-1/225)*(temp-25)^2+1
##检查发展是否为负
#如果发展<0
#发展=0
#回归发展
#否则
#回归发展
#结束
#结束
#数据点之间的直线段。该函数用于查找在温度下生长的幼虫的百分比
函数newImagentoDult(温度)
#资料来源于文献
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]
#通过所有可能温度的回路
对于1中的i:长度(x)-1
#如果温度在一段之间下降,找到该点
如果temp>=x[i]&&temp 40.0 | | temp=x[i]&&temp 40.0 | | temp您忘记导入定义Point2f0
的包:
using GeometryTypes
顺便说一句,您的struct
s应具有字段类型-由于性能原因,切勿使用非类型的struct
s
此外,要生成正态分布的数字,请使用randn()
。
还值得注意的是,在定义分布参数(如Normal()
)时,只需执行一次即可,然后即可重用对象。您忘记导入定义Point2f0
的包:
using GeometryTypes
顺便说一句,您的struct
s应具有字段类型-由于性能原因,切勿使用非类型的struct
s
此外,要生成正态分布的数字,请使用randn()
。
还值得注意的是,在定义分布参数(如Normal()
时,只需执行一次即可,然后即可重用对象。感谢您的回答!添加“使用几何类型”后,最后一个问题得到了解决。然而,现在我有一个错误,我不知道如何修复```谢谢你的回答!添加“使用几何类型”后,最后一个问题得到了解决。然而,现在我有一个错误,我不知道如何修复```