Hi,
I am required to create a script where the user can input a number of years to project the population of turtles and then have the data displayed for them in separate vectors and a plot.
My script so far allows me to look at data up to 2 years, but anything over that and I get an array error... Any help would be appreciated!
NumberOfYears = input ('Please enter the number of years you would like to project the population of Loggerhead Turtles for: ');
while NumberOfYears <= 0
NumberOfYears = input ('Error! The number of years must be larger than zero. Please enter the number of years you would like to project the population of Loggerhead Turtles for: ');
end
%Initial populations of turtles at Year 0.
Hatchling = 1445570;
Youth = 5536790;
BreedingAdult = 17640;
ProjectedTime = 1:NumberOfYears;
HatchVec = zeros(1,length(ProjectedTime));
for t = ProjectedTime
HatchVec = Hatchling - (Hatchling .* 0.675) - (Hatchling .* 0.325) + (BreedingAdult .* 77.4);
HatchVec = [Hatchling HatchVec];
end
fprintf ('The projected population of Hatchlings per year is: \n')
disp(HatchVec)
YouthVec = zeros(1,length(ProjectedTime));
for t = ProjectedTime
YouthVec = (Hatchling .* 0.675) + (Youth .* 0.769) - (Youth .* 0.230);
YouthVec = [Youth YouthVec];
end
fprintf ('The projected population of Youth per year is: \n')
disp(YouthVec)
AdultVec = zeros(1,length(ProjectedTime));
for t = ProjectedTime
AdultVec = BreedingAdult - (BreedingAdult .* 0.809) + (Youth .* 0.000434);
AdultVec = [BreedingAdult AdultVec];
end
fprintf ('The projected population of Breeding Adults per year is: \n')
disp(AdultVec)
plot(ProjectedTime, HatchVec)
xlabel ('Projected Years')
ylabel ('Number of Loggerhead Turtles')
title ('Projected Population of Loggerhead Turtles')

 Respuesta aceptada

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato el 19 de Abr. de 2020

0 votos

Look this part as example:
for t = ProjectedTime
HatchVec = Hatchling - (Hatchling .* 0.675) - (Hatchling .* 0.325) + (BreedingAdult .* 77.4);
HatchVec = [Hatchling HatchVec];
end
Here your HatchVec is just a single number and Hatchling is fixed. So you will have always only 2 variables after this loop, even with 1 year. I'm not sure how your model is supposed to work, but a way to fix it would be somehting like this:
HatchVec = zeros(1,length(ProjectedTime));
HatchVec(1) = Hatchling;
for t = 2:length(ProjectedTime)
HatchVec(t) = HatchVec(t-1) - (HatchVec(t-1) .* 0.675) - (HatchVec(t-1) .* 0.325) + (BreedingAdult .* 77.4);
end
So you always based the "new" year in the old one. The same logic should be used in the other loops.

4 comentarios

Samantha Pellegrino
Samantha Pellegrino el 19 de Abr. de 2020
Hi,
Thank you for your answer.
I made the adjustments you suggested, but now I am getting a different error stating:
Index exceeds the number of array elements (2)
Error in loggerhead (line 19)
HatchVec(t) = HatchVec(t-1) - (HatchVec(t-1) .* 0.675) - (HatchVec(t-1) .* 0.325) + (AdultVec(t-1) .* 77.4);
I am at a loss at the moment as to why this is happening...
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato el 19 de Abr. de 2020
Does the BreedingAdult variable changes for you to need a vector for it? It seems one of your vectors has wrong size, I belive it is the AdultVec.
Samantha Pellegrino
Samantha Pellegrino el 19 de Abr. de 2020
I adjusted the script to include your suggestions below:
NumberOfYears = input ('Please enter the number of years you would like to project the population of Loggerhead Turtles for: ');
while NumberOfYears <= 0
NumberOfYears = input ('Error! The number of years must be larger than zero. Please enter the number of years you would like to project the population of Loggerhead Turtles for: ');
end
%Initial populations of turtles at Year 0.
Hatchling = 1445570;
Youth = 5536790;
BreedingAdult = 17640;
ProjectedTime = 1:NumberOfYears;
HatchVec = zeros(1,length(ProjectedTime ));
HatchVec(1) = Hatchling;
for t = 2:length(ProjectedTime)
HatchVec(t) = HatchVec(t-1) - (HatchVec(t-1) .* 0.675) - (HatchVec(t-1) .* 0.325) + (AdultVec(t-1) .* 77.4 );
end
fprintf ('The projected population of Hatchlings per year is: \n')
disp(HatchVec)
YouthVec = zeros(1,length(ProjectedTime));
YouthVec(1) = Youth;
for t = 2:length(ProjectedTime)
YouthVec(t) = (HatchVec(t-1) .* 0.675) + (YouthVec(t-1) .* 0.769) - (YouthVec(t-1) .* 0.230);
end
fprintf ('The projected population of Youth per year is: \n')
disp(YouthVec)
AdultVec = zeros(1,length(ProjectedTime));
AdultVec(1) = BreedingAdult;
for t = 2:length(ProjectedTime)
AdultVec(t) = AdultVec(t-1) - (AdultVec(t-1) .* 0.809) + (YouthVec(t-1) .* 0.000434);
end
fprintf ('The projected population of Breeding Adults per year is: \n')
disp(AdultVec)
plot(ProjectedTime, HatchVec, ProjectedTime, YouthVec, ProjectedTime, AdultVec)
xlabel ('Projected Years')
ylabel ('Number of Loggerhead Turtles')
title ('Projected Population of Loggerhead Turtles')
I changed BreedingAdult to AdultVec as it is also dependent on the year prior. Let me know if you can see what I have done wrong...
All those vectors depend on each other in your model, so they must be in one loop. Also, you had forgotten to declare the variable initially. You can read the error the appear in the matlab window to find out where the problem was. This version of your code will work, although I'm not sure if the model you have programmed is the one you actually want to:
NumberOfYears = input ('Please enter the number of years you would like to project the population of Loggerhead Turtles for: ');
while NumberOfYears <= 0
NumberOfYears = input ('Error! The number of years must be larger than zero. Please enter the number of years you would like to project the population of Loggerhead Turtles for: ');
end
%Initial populations of turtles at Year 0.
Hatchling = 1445570;
Youth = 5536790;
BreedingAdult = 17640;
ProjectedTime = 1:NumberOfYears;
HatchVec = zeros(1,length(ProjectedTime ));
HatchVec(1) = Hatchling;
YouthVec = zeros(1,length(ProjectedTime));
YouthVec(1) = Youth;
AdultVec = zeros(1,length(ProjectedTime));
AdultVec(1) = BreedingAdult;
for t = 2:length(ProjectedTime)
HatchVec(t) = HatchVec(t-1) - (HatchVec(t-1) .* 0.675) - (HatchVec(t-1) .* 0.325) + (AdultVec(t-1) .* 77.4 );
YouthVec(t) = (HatchVec(t-1) .* 0.675) + (YouthVec(t-1) .* 0.769) - (YouthVec(t-1) .* 0.230);
AdultVec(t) = AdultVec(t-1) - (AdultVec(t-1) .* 0.809) + (YouthVec(t-1) .* 0.000434);
end
fprintf ('The projected population of Hatchlings per year is: \n')
disp(HatchVec)
fprintf ('The projected population of Youth per year is: \n')
disp(YouthVec)
fprintf ('The projected population of Breeding Adults per year is: \n')
disp(AdultVec)
plot(ProjectedTime, HatchVec, ProjectedTime, YouthVec, ProjectedTime, AdultVec)
xlabel ('Projected Years')
ylabel ('Number of Loggerhead Turtles')
title ('Projected Population of Loggerhead Turtles')

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by