Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Introduction to the Live Editor

This example is an introduction to the Live Editor. In the Live Editor, you can create live scripts that show output together with the code that produced it. Add formatted text, equations, images, and hyperlinks to enhance your narrative, and share the live script with others as an interactive document.

Create a live script in the Live Editor. To create a live script, on the Home tab, in the New drop-down menu, select Live Script.

Add the Census Data

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. To create a new section, go to the Live Editor tab and click the Section Break button.

Add the US Census data for 1900 to 2000.

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 = 

  Columns 1 through 7

   75.9950   91.9720  105.7110  123.2030  131.6690  150.6970  179.3230

  Columns 8 through 11

  213.2120  228.5050  250.6330  265.4220

Visualize the Population Change Over Time

Sections can be run independently. To run the code in a section, go to the Live Editor tab and click the Run Section button. You can also click the blue bar that appears when you move the mouse to the left of the section. When you run a section, output and figures appear together with the code that produced them.

Plot the population data against the year.

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])

Can we predict the US population in the year 2010?

Fitting the Data

Add supporting information to the text, including equations, images, and hyperlinks.

Let's try fitting the data with polynomials. We'll use the MATLAB polyfit function to get the coefficients.

The fit equations are:

x = (years-1900)/50;
coef1 = polyfit(x,pop,1) 
coef1 = 

   98.9924   66.1296

coef2 = polyfit(x,pop,2)
coef2 = 

   15.1014   68.7896   75.1904

coef3 = polyfit(x,pop,3)
coef3 = 

  -17.1908   66.6739   29.4569   80.1414

Plotting the Curves

Create sections with any number of text and code lines.

We can plot the linear, quadratic, and cubic curves fitted to the data. We'll use the polyval function to evaluate the fitted polynomials at the points in x.

pred1 = polyval(coef1,x);
pred2 = polyval(coef2,x);
pred3 = polyval(coef3,x);
[pred1; pred2; pred3]
ans = 

  Columns 1 through 7

   66.1296   85.9281  105.7266  125.5250  145.3235  165.1220  184.9205
   75.1904   89.5524  105.1225  121.9007  139.8870  159.0814  179.4840
   80.1414   88.5622  101.4918  118.1050  137.5766  159.0814  181.7944

  Columns 8 through 11

  204.7190  224.5174  244.3159  264.1144
  201.0946  223.9134  247.9403  273.1753
  204.8904  227.5441  248.9305  268.2243

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

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

Predicting the Population

You can share your live script with other MATLAB users so they can reproduce your results. You can also publish your results as PDF or HTML.

We can now calculate the predicted population in 2010 using our three equations.

x2010 = (2010-1900)/50;
pred1 = polyval(coef1,x2010);
pred2 = polyval(coef2,x2010);
pred3 = polyval(coef3,x2010);
[pred1 pred2 pred3]
ans = 

  283.9129  299.6184  284.6005

The linear and cubic fits predict similar values of about 284 million people, while the quadratic fit predicts a much higher value of about 300 million people.

Was this topic helpful?