Solution of Growth problem
1 次查看(过去 30 天)
显示 更早的评论
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:
- 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)
- a scalar value for the number of years
Outputs:
- 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.
0 个评论
采纳的回答
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)
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 个评论
更多回答(1 个)
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);
yt = grow_population(y0,4);
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!