using Plots
function propagation_virus(taux_transmission, personnes_infectees_initiales, population_totale, duree_simulation)
# Initialisation des tableaux pour le suivi des données
temps = 1:duree_simulation
susceptibles = Int[population_totale - personnes_infectees_initiales]
infectes = Int[personnes_infectees_initiales]
recuperes = Int[0]
plots = [] # Stockage des tracés pour créer le GIF
for jour in 2:duree_simulation
# Calcul du nombre de nouvelles infections
nouvelles_infections = Int[susceptibles[jour - 1] * taux_transmission * infectes[jour - 1] / population_totale]
# Calcul du nombre de récupérés (hypothétiquement, après un certain temps)
recuperes_jour = Int[infectes[jour - 1] - nouvelles_infections]
# Mise à jour des tableaux
push!(susceptibles, susceptibles[jour - 1] - nouvelles_infections)
push!(infectes, infectes[jour - 1] + nouvelles_infections - recuperes_jour)
push!(recuperes, recuperes[jour - 1] + recuperes_jour)
# Création d'un graphique à chaque étape
p = plot(temps[1:jour], susceptibles[1:jour], label="Susceptibles")
plot!(temps[1:jour], infectes[1:jour], label="Infectés")
plot!(temps[1:jour], recuperes[1:jour], label="Récupérés", xlabel="Jours", ylabel="Nombre de personnes", legend=true)
push!(plots, p)
end
return plots
end
# Paramètres de la simulation
taux_transmission = 0.2 # Taux de transmission (0.2 signifie qu'une personne infecte 20% de la population)
personnes_infectees_initiales = 1
population_totale = 1000
duree_simulation = 100
# Exécution de la simulation
plots = propagation_virus(taux_transmission, personnes_infectees_initiales, population_totale, duree_simulation)
# Création du GIF
anim = @animate for p in plots
p
end
gif(anim, "propagation_virus.gif", fps = 5)
This is the same error you were having before. In line 14, you are trying to place a Float into an array of Integers. The quick fix is to remove all of the “Int” from your code. Test line-by-line to get a better understanding of what is going wrong rather than running the entire code at once.
(Your code formatting is better this time! But you should also copy and paste your error message into a code block between ``` rather than posting a screenshot and try to use a more descriptive title.)
Here you are saying the number of new infections is given by the number of susceptible agents in the population yesterday, multiplied by a transmission rate and the share of infected in the total population. Clearly it is unlikely that this number will be an integer - the transmission rate is probably not an exact integer, and neither will the share of infected in the total population be. So this calculation is bound to create a non-integer number; if you want the number to be integer you have to round it explicitly, so:
Note here I’ve replaced Int[...] with round(Int, ...). This will give a single integer number which is then assigned to nouvelles_infections. It’s not clear to me why you are doing Int[...] here but note that this creates an array of integers with a single number in it, which is not the same as just the number. Later on you are doing:
which suggests that susceptibles is a vector that you are using to track the number of not-yet-infected over time. Then susceptibles[jour-1] is an element of that vector, i.e. likely a single integer, which means susceptibles[jour - 1] - nouvelles_infections will fail if nouvelles_infections is an array rather than an integer:
julia> 4 - [2]
ERROR: MethodError: no method matching -(::Int64, ::Vector{Int64})
For element-wise subtraction, use broadcasting with dot syntax: scalar .- array