# 实时编辑器介绍

### 添加人口统计数据

```years = (1900:10:2000); % Time interval pop = [75.995 91.972 105.711 123.203 131.669 ... % Population Data 150.697 179.323 213.212 228.505 250.633 265.422]```
```pop = 1×11 75.9950 91.9720 105.7110 123.2030 131.6690 150.6970 179.3230 213.2120 228.5050 250.6330 265.4220 ```

### 以可视方式呈现一段时间内的人口变化

```plot(years,pop,'bo'); % Plot the population data axis([1900 2020 0 400]); title('Population of the U.S. 1900-2000'); ylabel('Millions'); xlabel('Year') ylim([50 300])```

### 拟合数据

`$\begin{array}{ll}y=ax+b& \phantom{\rule{2em}{0ex}}\text{linear}\\ y=a{x}^{2}+bx+c& \phantom{\rule{2em}{0ex}}\text{quadratic}\\ y=a{x}^{3}+b{x}^{2}+cx+d.& \phantom{\rule{2em}{0ex}}\text{cubic}\end{array}$`

```x = (years-1900)/50; coef1 = polyfit(x,pop,1) ```
```coef1 = 1×2 98.9924 66.1296 ```
`coef2 = polyfit(x,pop,2)`
```coef2 = 1×3 15.1014 68.7896 75.1904 ```
`coef3 = polyfit(x,pop,3)`
```coef3 = 1×4 -17.1908 66.6739 29.4569 80.1414 ```

### 绘制曲线图

```pred1 = polyval(coef1,x); pred2 = polyval(coef2,x); pred3 = polyval(coef3,x); [pred1; pred2; pred3]```
```ans = 3×11 66.1296 85.9281 105.7266 125.5250 145.3235 165.1220 184.9205 204.7190 224.5174 244.3159 264.1144 75.1904 89.5524 105.1225 121.9007 139.8870 159.0814 179.4840 201.0946 223.9134 247.9403 273.1753 80.1414 88.5622 101.4918 118.1050 137.5766 159.0814 181.7944 204.8904 227.5441 248.9305 268.2243 ```

```hold on plot(years,pred1) plot(years,pred2) plot(years,pred3) ylim([50 300]) legend({'Data' 'Linear' 'Quadratic' 'Cubic'},'Location', 'NorthWest') hold off```

### 预测人口

```year = 2018; xyear = (year-1900)/50; pred1 = polyval(coef1,xyear); pred2 = polyval(coef2,xyear); pred3 = polyval(coef3,xyear); [pred1 pred2 pred3]```
```ans = 1×3 299.7517 321.6427 295.0462 ```