using Plots
function propagation_virus(taux_transmission::Float64, personnes_infectees_initiales, population_totale, duree_simulation)
# Initialisation des tableaux pour le suivi des données
temps = 1:duree_simulation
susceptibles = [population_totale - personnes_infectees_initiales]
infectes = [personnes_infectees_initiales]
recuperes = [round(4/3)]
plots = [] # Stockage des tracés pour créer le GIF
for jour in 2:duree_simulation
# Calcul du nombre de nouvelles infections
nouvelles_infections = (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 = [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)
Given that this is the third time I’ve said this in response to your post I promise it’s also the last:
You seem to be doing a simulation based on agents in a model, and therefore thinking in terms of integers (e.g. 100 infected individuals, 36 recovered individuals etc.)
In calculating these numbers however you inevitably end up with non-integer numbers: e.g. 25% of all infected recover each period, so the 100 in the first period become 75 in the second, but then 56.25 in the third. If you want to keep your models in terms of “whole agents” you therefore need to round your non-integer numbers.
The error you keep asking about has always been the same: Int(3.5) in Julia doesn’t work as Int is not rounding numbers; it will only create integers if the number you are passing in is exactly representable as an integer (e.g. Int(3.0) will work). Wherever you divide integers, or multiply by non-integer numbers (e.g. proportions) you will have to round your results by doing round(Int, result) to ensure you keep working with integers.
I was incorrect when I suggested removing the Int would fix the problem because Julia infers the type of the elements anyway. These array constructors both return the same type, so you cannot push! Floats into either. (Only an empty array constructor defaults to Any.)
(This error is totally unrelated to Plots.jl. You need to break your problem down into smaller pieces and get those working first before trying to rerun the entire script. Try setting jour=2 and then step through the script in the REPL one line at a time to better understand the problem. You have been told what these errors mean and how to fix them. Now you need to work through your script slowly and implement those fixes as necessary.)
I already explained this in my answer four days ago:
You need to be careful about the distinction between a number (e.g. 4) and a one-element vector holding a single number ([4]). They are not the same thing and the difference matters.
Not to unfairly prejudge you, but it seems to me you might have very limited experience with coding and Julia specifically. Instead of posting a full model here and then letting people on the forum debug it line-by-line, I think you might find it more productive to spend a bit of time learning the basics; Think Julia: How to Think Like a Computer Scientist is a good starting point for getting started both on programming and Julia, if you’re more of a video person I know many people like https://www.youtube.com/c/juliafortalentedamateurs/about