Solution of Growth problem

4 Ansichten (letzte 30 Tage)
Oskar S.
Oskar S. am 27 Feb. 2024
Bearbeitet: Voss am 27 Feb. 2024
The function will take in two inputs and return one output. The function will be assessed by running the code: "yt=grow_population(y0,t);" for chosen values of a vector y0 and a scalar t. The vector y0 is a 6 X 1 vector that contains the initial populations of the 6 cities from the map (the first entry is A, the second is B, etc). The scalar t determines how many years of growth to compute. The change in city populations resulting from population growth can be found in the image above.
Inputs:
  1. a vector of size 6 X 1 for the initial population sizes of the 6 cities (the first entry is A, the second is B, etc)
  2. a scalar value for the number of years
Outputs:
  1. a vector of size 6 X 1 that gives the population sizes resulting from t years of growth.
How did I think ?
function yt = grow_population(y0, t)
% y0 is a 6 x 1 vector of initial city populations
y0 = [100; 300; 500; 125; 100; 200];
% t is the number of years of growth
t = 3;
% Defining growth factors from a to f
a = 1.0;
b = 0.8;
c = 1.1;
d = 1.1;
e = 1.3;
f = 0.9;
% Creating a vector of growth factors
variables = [a; b; c; d; e; f];
% Calculating population growth
yt = (y0 .* (variables.^t));
% Displaying the result
disp('The population yt is: ');
disp(yt);
end
What is the problem in here ? Thank you for helping.

Akzeptierte Antwort

Sam Chak
Sam Chak am 27 Feb. 2024
% y0 is a 6 x 1 vector of initial city populations
y0 = [100; 300; 500; 125; 100; 200];
% t is the number of years of growth
t = 3;
grow_population(y0, t)
The population yt is: 100.0000 153.6000 665.5000 166.3750 219.7000 145.8000
function grow_population(y0, t)
% Defining growth factors from a to f
a = 1.0;
b = 0.8;
c = 1.1;
d = 1.1;
e = 1.3;
f = 0.9;
% Creating a vector of growth factors
variables = [a; b; c; d; e; f];
% Calculating population growth
yt = (y0 .* (variables.^t));
% Displaying the result
disp('The population yt is: ');
disp(yt);
end
  3 Kommentare
Oskar S.
Oskar S. am 27 Feb. 2024
Thank you very much Sam
Sam Chak
Sam Chak am 27 Feb. 2024
You are welcome @Oskar S.. You can also learn from @Aquatris's approach. 👍

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Aquatris
Aquatris am 27 Feb. 2024
You overwrite the y0 and t variable within the function with this code:
y0 = [100; 300; 500; 125; 100; 200];
t = 3;
So the user entered values are ignored completely. Remove that from your function cause it should be defined by the user while calling the function.
Otherwise assuming your equation is correct, seems to work fine. Also you might want to add the number of years to your disp call within the function.
y0 = [100; 300; 500; 125; 100; 200];
yt = grow_population(y0,3);
The population yt after 3 years is: 100.0000 153.6000 665.5000 166.3750 219.7000 145.8000
yt = grow_population(y0,4);
The population yt after 4 years is: 100.0000 122.8800 732.0500 183.0125 285.6100 131.2200
function yt = grow_population(y0, t)
% y0 is a 6 x 1 vector of initial city populations
%% removed y0 here
%% remove t here
% Defining growth factors from a to f
a = 1.0;
b = 0.8;
c = 1.1;
d = 1.1;
e = 1.3;
f = 0.9;
% Creating a vector of growth factors
variables = [a; b; c; d; e; f];
% Calculating population growth
yt = (y0 .* (variables.^t));
% Displaying the result
fprintf('The population yt after %.d years is: \n',t);
disp(yt);
end

Kategorien

Mehr zu Geoscience finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by