Main Content

MATLAB手机的实时编辑简介

此示例显示了如何与结合输入代码和输出的实时脚本进行交互。

添加格式的文本以增强您的叙述,并使用MATLAB DRIVE™与他人共享实时脚本作为交互式文档。

在iPad或Android平板电脑上使用MATLAB Mobile中的实时脚本时,您可以使用可用的工具程序来创建新的实时脚本,格式文本和运行代码。不支持包括插入交互式控件和任务在内的高级插入。万博1manbetx如果现有的实时脚本包含交互式控件,则可以使用现有控件。

在iPhone或Android手机上,您可以在MATLAB移动设备中打开并编辑现有的实时脚本,并使用可用的按钮。The toolstrip is not available and text formatting is not supported.

打开现场脚本

You can open and edit an existing live script in MATLAB Mobile. To open a live script, go to the文件view or use the编辑命令。在iPad或Android平板电脑上的MATLAB移动设备中,您还可以创建一个实时脚本。要创建一个实时脚本,在实时编辑器工具条上,请点击。You also can create a live script from the文件通过点击查看和选择新的实时脚本

添加人口普查数据

Divide your live script into sections. Sections can contain text, code, and output. MATLAB code appears with a gray background and output appears with a white background.

要在实时编辑器工具条上创建一个新部分,请点击section break按钮。您还可以输入两百分点(%%)在实时脚本中,然后按进入

添加1900年至2000年的美国人口普查数据。

年=(1900:10:2000);% Time intervalpop = [75.995 91.972 105.711 123.203 131.669...人口数据%150.697 179.323 213.212 228.505 250.633 265.422]
pop =1×1175.9950 91.9720 105.7110 123.2030 131.6690 150.6970 179.3230 213.2120 228.5050 250.6330 265.4220

可视化人口随时间变化

可以独立运行部分。要在一个部分中运行代码,在实时编辑器工具条上,点击“运行”部分按钮。当您点击部分的左侧时,您还可以点击出现的蓝色条。当您运行一个部分时,输出和数字与产生它们的代码一起出现。

绘制与年度的人口数据。

情节(年份,流行音乐,'bo');%绘制人口数据轴([[1900 2020 0 400]);标题('Population of the U.S. 1900-2000');ylabel('Millions');Xlabel('年')Ylim([50 300])

我们可以预测2010年的美国人口吗?

拟合数据

将支持信万博1manbetx息添加到文本中。使用实时编辑器工具条中的选项进行格式化文本。

Let's try fitting the data with polynomials.

x =(1900年)/50;coef1 = polyfit(x,pop,1)
COEF1 =1×298.9924 66.1296
coef2 = polyfit(x,pop,2)
COEF2 =1×315.1014 68.7896 75.1904
coef3 = polyFit(x,pop,3)
COEF3 =1×4-17.1908 66.6739 29.4569 80.1414

绘制曲线

使用任意数量的文本和代码行创建部分。

We can plot the linear, quadratic, and cubic curves fitted to the data. We'll use thepolyval在评估拟合的多项式处的功能X

pred1 = polyval(coef1,x); pred2 = polyval(coef2,x); pred3 = polyval(coef3,x); [pred1; pred2; pred3]
ans =3×1166.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

Now let's plot the predicted values for each polynomial.

抓住on情节(年,pred1)图(年,pred2)图(年,ped3)ylim([50 300])传奇({{'Data'“线性”“二次”'立方体'},,'Location',,,,'西北') hold离开

预测人口

You can share your live script with other MATLAB users so that they can reproduce your results using MATLAB Drive. If you open a live script that contains sliders, drop-downs, check boxes, edit fields, or buttons, you can use the controls to change the value of variables interactively. Inserting new controls is not supported on MATLAB Mobile.

现在,我们可以通过以下滑块调整一年来计算给定年的预测人群:

年=2027;xyear =(1900年)/50;pred1 = polyval(coef1,xyear);pred2 = polyval(coef2,xyear);PED3 = polyVal(COEF3,XYEAR);[Ped1 Pred2 Pred3]
ans =1×3317.5703 347.3443 303.4082

例如,在2010年中,线性和立方拟合预测约2.84亿人的相似值,而二次拟合预测的价值约为3亿人。