A town has a population census every 10 years. The results of the census are shown below. Create a spreadsheet and MATLAB Script that to return the estimation of the value of the population of the town for any year between 1900 and 2000.Make your script interactive in case there is a change in data.

댓글 수: 9

Geoff Hayes
Geoff Hayes 2021년 5월 12일
Jack - since this is homework, please describe what you have attempted so far. Show your code and discuss what problems/errors you are encountering.
i tried doing this but it says that there's an error at n_pop=cencus(yrr)
t close all;clear;
yrr=1985:1:1995;
n_pop=census(yrr);
%%%%%%%%%%%%%%%%
function n_pop=cencus(n_year)
%given data points
year=[1900:10:2000];
pop=[550 720 1265 2287 2756 3012 4086 5220 7478 10112 14226];
%display the data
fprintf('Given population data\n')
fprintf('\tyear, population\n')
%interpolation
n_pop=interpl(year,pop,n_year,'spline')
%print new year and corresponding population
fprintf('\n New Population data for given years\n')
fprintf('\t\t\tyear, population\n')
disp([n_year' n_pop']);
end
Rik
Rik 2021년 5월 12일
That looks like interpl with an L to me, instead of interp1.
This error keeps popping up interp1 or interpl
'census' is used in Predicting the US Population.
Error in number2 (line 4)
n_pop=census(yrr);
Rik
Rik 2021년 5월 12일
The solution by Walter below will probably solve your current problem, but note for the future that you should post the complete error message (all the red text). This often contains necessary information.
I have a new problem, how do i remove the [1.0e+04 *] and make everything under the new population data whole number i dont know why it ran and showed 0.1985 instead of 1985
close all;clear all;
yrr=1985:1:1995;
n_pop=census(yrr);
%%%%%%%%%%%%%%%%
function n_pop=census(n_year)
%given data points
year=[1900:10:2000];
pop=[550 720 1256 2287 2756 3012 4086 5220 7478 10112 14226];
%display the data
fprintf('Given population data\n')
fprintf(' \t\tyear population \n')
disp([year' pop']);
%interpolation
n_pop= interp1(year, pop, n_year,'spline');
%print new year and corresponding population
fprintf('\n New Population data for given years\n')
fprintf('\t\t\tyear, population\n')
disp([n_year' n_pop']);
end
Rik
Rik 2021년 5월 13일
Why are you doing close all and clear all? You're using a function, so you don't even need clear or clearvars, and you don't open any figure so there is nothing you should close. What you're doing is equivalent to restarting Matlab every time you run your code. It works, but why would you do that?
Anyway, the solution to your current problem is to know that displaying data has nothing to do with the data itself. You can force a specific format with the sprintf and fprintf format specifier. Read the documentation for examples.
Jack Wallard
Jack Wallard 2021년 5월 13일
Is there a way to change this? I just want it to show 1985 instead of 0.1985. If you run the codes thats what comes out please help
Displaying new population data for given years
year, population
1.0e+04 *
0.1985 0.8721
0.1986 0.8980
0.1987 0.9247
0.1988 0.9524
0.1989 0.9811
0.1990 1.0112
0.1991 1.0428
0.1992 1.0760
0.1993 1.1110
0.1994 1.1481
0.1995 1.1873
Rik
Rik 2021년 5월 13일
What syntax are you using in fprintf? Or are you still using disp? If you want a specific format, you should not be using disp.

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

답변 (2개)

Walter Roberson
Walter Roberson 2021년 5월 12일

1 개 추천

function n_pop=cencus(n_year)
^^^^^^
Spelling.
Steven Lord
Steven Lord 2021년 5월 13일

0 개 추천

Change the format you use to display the data.
year=[1900:10:2000];
pop=[550 720 1265 2287 2756 3012 4086 5220 7478 10112 14226];
n_year = 1955:3:1995;
n_pop= interp1(year, pop, n_year,'spline');
format % use the default display format
data = [n_year.', n_pop.']
data = 14×2
1.0e+04 * 0.1955 0.3502 0.1958 0.3860 0.1961 0.4189 0.1964 0.4480 0.1967 0.4799 0.1970 0.5220 0.1973 0.5794 0.1976 0.6484 0.1979 0.7228 0.1982 0.7972
format longg % use a different format
data
data = 14×2
1955 3501.6750103553 1958 3859.51906774669 1961 4189.48262780744 1964 4480.27168483063 1967 4799.11598356959 1970 5220 1973 5794.44895199742 1976 6484.15102503682 1979 7228.335146493 1982 7971.59789248895
You could even create a table array of your data.
T = table(n_year.', n_pop.', 'VariableNames', {'Year', 'Population'})
T = 14×2 table
Year Population ____ ________________ 1955 3501.6750103553 1958 3859.51906774669 1961 4189.48262780744 1964 4480.27168483063 1967 4799.11598356959 1970 5220 1973 5794.44895199742 1976 6484.15102503682 1979 7228.335146493 1982 7971.59789248895 1985 8720.93475584499 1988 9523.59859499264 1991 10427.5132244569 1994 11480.6024587629

카테고리

도움말 센터File Exchange에서 Install Products에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 5월 12일

답변:

2021년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by