필터 지우기
필터 지우기

Solution of Growth problem

조회 수: 1 (최근 30일)
Oskar S.
Oskar S. 2024년 2월 27일
편집: Voss 2024년 2월 27일
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.

채택된 답변

Sam Chak
Sam Chak 2024년 2월 27일
% 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
Oskar S.
Oskar S. 2024년 2월 27일
Thank you very much Sam
Sam Chak
Sam Chak 2024년 2월 27일
You are welcome @Oskar S.. You can also learn from @Aquatris's approach. 👍

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Aquatris
Aquatris 2024년 2월 27일
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
  댓글 수: 1
Oskar S.
Oskar S. 2024년 2월 27일
Thank you very much

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by