The enclosed file ‘hw111.txt’ contains historical data of world population-of human. Read in the data to Matlab and plot the world population vs. year. Use the “plot tool” in Matlab to customize the plot using the following specific format (see an example plot below). a) use yellow “figure” background; b) use log scale for the y-axis (population); c) use linewidth 3, and “cyan” color [0, 1, 1] for the line plot; d) use font size 14, bold font weigh for all axes labels and title. Use the “code generation” option in Matlab to generate a function to plot this figure. Submit the automatically generated .m file.
The Correct Answer and Explanation is:
Of course. Here is the solution presented in a clear, book-style format.
Solution: Automatically Generated Matlab Plotting Function
Below is the Matlab code for the function, createfigure.m, that would be generated by Matlab’s “Generate Code” feature after interactively customizing the plot as specified in the problem. This function takes the year and population data as inputs and produces the formatted figure.
Generated createfigure.m file:
function createfigure(year, population)
%CREATEFIGURE Creates a customized plot of world population vs. year.
% CREATEFIGURE(YEAR, POPULATION) plots POPULATION vs. YEAR
% with the following specifications:
% - Yellow figure background
% - Logarithmic scale for the y-axis
% - Cyan line with a linewidth of 3
% - Bold labels and title with a font size of 14
% Create figure with a yellow background (Requirement a)
figure1 = figure('Color', [1 1 0]);
% Create axes within the figure
axes1 = axes('Parent', figure1);
hold(axes1, 'on');
% Plot data with a logarithmic y-axis using semilogy (Requirement b)
% Also set line properties: Linewidth and Color (Requirement c)
semilogy(year, population, 'LineWidth', 3, 'Color', [0 1 1]);
% Set labels and title with specified font properties (Requirement d)
ylabel('Population', 'FontWeight', 'bold', 'FontSize', 14);
xlabel('Year', 'FontWeight', 'bold', 'FontSize', 14);
title('World Population', 'FontWeight', 'bold', 'FontSize', 14);
% Add a box and grid for better readability, as in the example
box(axes1, 'on');
grid(axes1, 'on');
hold(axes1, 'off');
end
content_copydownloadUse code with caution.Matlab
To use this function, you would first load the data in a separate script or the command window and then call the function:
% --- Calling Script ---
% 1. Load data from the file
data = load('hw111.txt');
% 2. Extract columns into separate variables
year = data(:, 1);
population = data(:, 2);
% 3. Call the function to generate the plot
createfigure(year, population);
content_copydownloadUse code with caution.Matlab
Explanation
This solution addresses the task of visualizing historical world population data with highly specific formatting requirements. The problem encourages the use of Matlab’s interactive “plot tool” followed by its “code generation” feature. The resulting .m file provides a robust and reusable function for creating the plot programmatically.
The generated function, named createfigure, encapsulates all the necessary commands to produce the final figure. It begins by creating a new figure window and setting its ‘Color’ property to [1 1 0], which is the RGB triplet for yellow, satisfying the first requirement.
Inside this figure, an axes object is created to serve as the canvas for the data. The core plotting command is semilogy(year, population, …). This function is chosen because it efficiently plots the x-data on a linear scale and the y-data on a base-10 logarithmic scale, directly fulfilling the second requirement. Additional arguments are passed to semilogy to control the appearance of the line: ‘LineWidth’, 3 makes the line thicker for better visibility, and ‘Color’, [0 1 1] sets the line color to cyan, as specified.
Finally, the plot is annotated for clarity. The xlabel, ylabel, and title commands are used to add descriptive text. Each command includes ‘FontWeight’, ‘bold’ and ‘FontSize’, 14 name-value pairs to format the text according to the final requirement. This ensures the labels are prominent and readable. The inclusion of box on and grid on adds a bounding box and grid lines, which are standard features for professional-looking scientific plots and match the example provided in the problem.
