Need help in creating a code
显示 更早的评论
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
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.
Jack Wallard
2021-5-12
Rik
2021-5-12
That looks like interpl with an L to me, instead of interp1.
Jack Wallard
2021-5-12
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.
Jack Wallard
2021-5-13
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
2021-5-13
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
2021-5-12
function n_pop=cencus(n_year)
^^^^^^
Spelling.
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.']
format longg % use a different format
data
You could even create a table array of your data.
T = table(n_year.', n_pop.', 'VariableNames', {'Year', 'Population'})
类别
在 帮助中心 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!