How do I fix the array error?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Samantha Pellegrino
am 19 Apr. 2020
Kommentiert: Thiago Henrique Gomes Lobato
am 19 Apr. 2020
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')
0 Kommentare
Akzeptierte Antwort
Thiago Henrique Gomes Lobato
am 19 Apr. 2020
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 Kommentare
Thiago Henrique Gomes Lobato
am 19 Apr. 2020
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')
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!