代做MAC2233 Project1代做Matlab编程
- 首页 >> C/C++编程PROJECT 1
OVERVIEW
This is a mandatory Project that must be completed in Groups — you can split the work but help each other if needed. Each group will submit for grading ONE final script as a PDF file via Canvas page Assignments, Project 1. The file must be named MAC2233_Project1_Group # (insert your group number).
Each Exercise must be completed in MATLAB Live Script. using the Live Editor, and the Live Script will be saved with an extension .mlx — please refer to Project 0 on detail how to access MATLAB via UF Apps and how to work in the Live Script using the Live Editor.
Each member of a Group is required to post the completed Live Script file onto the Group Homepage under the Files no later than on November 14th, including the Last name and the numbers of completed Exercises in the name of the file. The Group Leader will be using these files to compose the group’s final script, run it in MATLAB, Export to PDF, and submit for grading. A Group Leader must post a Report (Word documents) onto the Group Homepage, listing the contributions to this Project, no later than on the due date for the Project, which is November 20th.
BEFORE POSTING YOUR EXERCISE(S) ONTO THE GROUP HOMEPAGE VERIFY THAT
1) All tasks are completed.
2) No missing inputs/outputs.
3) The inputs/outputs that were required to be displayed are displayed.
4) The inputs/outputs that were required to be suppressed are suppressed.
5) No extra inputs.
6) No extra commands/outputs.
7) No extra formatting commands.
8) All comments are present.
9) The formatting is as required.
10) All plots are present and displayed properly.
Please refer to the Canvas page Assignments, Project 1 for more detail on the Group work, Submission, and Grading.
NOTE on the Format of the Project 1 Instructions:
(1) The sign ** at the beginning of an instructional sentence indicates that this task must be
performed in MATLAB by an appropriate command or a sequence of commands. This sign is not a MATLAB command — it is used in the Instructions to emphasize that the task must be performed in MATLAB.
(2) The sign % at the beginning of an instructional sentence indicates that you need to type a comment in the Live Script. — comments must be typed, not output by display commands.
(3) If a semicolon is placed after a MATLAB command given in the Instructions, it is an indication that the output must be suppressed (not printed), and if a semicolon is not present — the output must be displayed (printed).
IMPORTANT:
When working on the Exercises of Project 1:
Follow the instructions precisely and complete all tasks that are required.
Insert Section Break after each Exercise.
Mark down the Exercises within the Project, such as Exercise 1, Exercise 2, etc.
Do not place any commands that are not required - there will be points deducted for any extra command or extra output in your file.
Type your comments, do not output them by display commands; otherwise, the points will be taken off for the incorrect formatting.
EXERCISE 1: LIFE EXPECTANCY
When working on this Exercise, you are required to use MATLAB. For help, please refer to the file Using Live Script (Video) under the Resources section on the Canvas page Assignments, Project 1 and to the file MATLAB Project0 (PDF) located on the Canvas page Assignments, Project 0.
DESCRIPTION: In this exercise, we will be working with the Life Expectancy. Please refer to Chapter 1, Pages 46 — 47 of the textbook for more information regarding this topic.
When working in MATLAB on outputting a curve of the best fit, plotting the curve and the data points, evaluating the correlation coefficient, etc., you can use sample code fragments presented in Exercise 2 of Project 0.
EXERCISE 1: The following table gives the average life expectancy at birth of both sexes born in the United States in various years from 1900 to 2018. Source: National Center of Health Statistics ( https://www.cdc.gov/nchs/data-visualization/mortality-trends/index.htm )
Let x be the number of years since 1900 and let y be the average life expectancy. We will consider y to be a function of x.
First, we output the table which columns are the vectors x and y.
** Place the formatting command in your Live Script.
format('default')
**Input the vectors x (number of years since 1900) and y (average life expectancy) as column vectors (see Project 0, Exercise 1). Do not display x and y here (put a semicolon after a command that outputs a vector).
**Output and display the table whose columns are the vectors x and y by the commands below:
T = table(x,y,...
'VariableNames',{'x (years since 1900)','y (life expectancy)'});
disp(T)
Next, we will output the polynomial of the best fit of the degree n = 1.
** Output a vector Y1 of the coefficients ofthe polynomial of degree n = 1 of the best fit by the command polyfit() (see Project 0, Exercise 2). Display Y1 (do not put a semicolon after the command that outputs Y1).
**Output (and display) the polynomial in symbolic form with the coefficients up to 5 significant digits by the command below:
P1=vpa(poly2sym(Y1),5)
Next, we will plot in the same axis the data points and the polynomial defined by the vector Y1 of its coefficients.
**Plot the data points (x,y) by the commands below:
scatter(x,y) %plots the data points
axis([0 130 40 90]) %sets the limits on axis
hold on %holds future plots in the same axis
**Input the vector xmesh of a 100 equidistant numbers on the interval [0, 130] and output the vector y1mesh of the values of the polynomial at these points by the commands:
xmesh=linspace(0,130);
y1mesh=polyval(Y1,xmesh);
**Plot the polynomial in the same axis with the data points (you have already placed the command hold on which allows you to add another plot to the same axis) by using the command plot() (see Project 0, Exercise 2), which has to be run on the vectors xmesh and y1mesh. Make a graph of the polynomial in the 'red' color.
**Place the command
hold off
to set the hold state to off.
Next, we will output the correlation coefficient, r1, which is a correlation between the column vector of the observed y-values (the vector of the y-components of the data points) and the column vector of the predicted y-values (the vector of the values of the polynomial of the “best fit” at the x-components of the data points).
Note: the correlation coefficient that we use here can be also defined as the cosine of the angle between the deviation vectors ofthe observed y-values and predicted y-values.
**Output the correlation coefficient r1 using the set ofthe commands presented in Exercise 2 of Project 0 by making necessary adjustments. Display r1 in your code (do not put a semicolon after the command that outputs r1).
%Use the plot and the correlation coefficient r1 to evaluate how well the polynomial of degree n = 1 interpolates the data. Type a comment about it.
Next, we will fit the data with a polynomial of degree n = 2 of the “best fit” .
Note: You can use the code fragments created above for the polynomial of degree n = 1 by making necessary changes.
**Output and display the vector Y2 of the coefficients ofthe polynomial of degree n = 2 of the “best fit” .
**Output the polynomial itself in a symbolic form, P2, with 5 significant digits for the coefficients.
**Plot the data points (the axis limits stay the same as for the polynomial of degree n = 1), place the command hold on, output the vector y2mesh of the values of the polynomial defined by Y2 at the entries of the vector xmesh, and plot the polynomial of degree 2 in the same axis with the data points. Place the command hold off.
**Output and display the correlation coefficient r2, as a correlation between the column vectors of the observed y-values and the y-values predicted by the polynomial of the degree n = 2.
%Based on the correlation coefficient r2 and on the plot that you created, type a comment whether the polynomial of degree n = 2 of the “best fit” indeed interpolates the data well.
%Compare the correlation coefficients r1 and r2 and type a comment which of the polynomials of the best fit, of degree 1 or 2, fits the data better.
**Predict the life expectancies for the people born in 2025 by using the polynomials of degree n = 1 (defined by the vector Y1) and ofdegree n = 2 (defined by the vector Y2) and employing a MATLAB command polyval(). Make sure to identify correctly the x-value for the commands: the x-value must be the number of years since 1900. Assign your outputs to the variables expectancy1_2025 and expectancy2_2025, respectively, and display them (do not put a semicolon after a command that outputs a variable).
%Type a comment on which of the polynomials, of degree 1 or of degree 2, from your point of view, gives a better prediction of the life expectancy for the people born in 2025.
EXERCISE 2: APPROXIMATING DATA WITH A POWER FUNCTION
When working on this Exercise, you are required to use MATLAB. For help, please refer to the file Using Live Script (Video) under the Resources section on the Canvas page Assignments, Project 1 and to the file MATLAB Project0 (PDF) located on the Canvas page Assignments, Project 0.
DESCRIPTION: In this Exercise, we will be approximating the data by a power function.
Please refer to Chapter 2, Pages 131 — 133 of the textbook for more information regarding this Exercise. You can use here the sample code fragments presented in Exercises 2, 3 of Project 0.
We will be using a power function
y = axb , (1)
where a and b are real numbers and x, y > 0 , to fit the data. Prior to doing that, we will run the TEST (see below) to check if a power function is suitable for fitting the data.
TEST: The power function y = axb can be used to fit the data (xk, yk ), k = 1: n , if a linear function can be used to fit the logarithmically transformed data (Xk, Yk ) = (ln xk , ln yk ) .
Indeed, let x and y be the independent and dependent variables ( x, y > 0 ). Taking natural logarithm of both sides of the equation in (1), we will have:
ln y = ln a + b ln x . (2)
Let
ln y = Y , ln a = A , ln x = X . (3)
Then (2) becomes
Y = A + bX . (4)
Obviously, the original data (xk, yk ) , k = 1: n can be approximated by a power function in (1) if the new data (Xk, Yk ) = (ln xk , ln yk ) , can be fit by a linear function in (4).
EXERCISE 2: (Head Start) The enrollment in Head Start for some recent years is included in
the table below. Source: Head Start .
Let x be the number of years since 1960 (the year 1970 corresponds to x = 10) and let y be the enrollment (in thousands). We will consider y to be a function of x.
First, we will output and display the table which columns are the vectors x and y.
** Place the formatting command:
format('default')
**Input the x- and y-components of the data as the column vectors x and y (see Project 0,
Exercise 1). Do not display them here — put a semicolon after a command that outputs a vector.
**Output and display the table with the data by the commands below:
T = table(x,y,...
'VariableNames',{'Years, x (since 1960)',...
'Enrollment, y (in thousands)'});
disp(T)
Next, we will work with the logarithmically transformed data.
**Output (do not display) a new set of (X,Y)-data whose components are the natural logarithms ofthe x- and y-components of the original data (remember MATLAB is case sensitive):
X=log(x);
Y=log(y);
**Use the command scatter() to create the scatterplot of the new (X,Y) - data (see Project 0, Exercise 2).
**Place the command
hold on
Now, we are going to fit the logarithmically transformed data by a straight line.
**Output the vector L ofthe coefficients ofthe polynomial of degree n = 1 of the best fit for the (X,Y)-data by using the command polyfit() (see Project 0, Exercise 2).
Display L (do not put a semicolon after the command that outputs L).
Next, we will plot the polynomial defined by the vector L in the same axis with the (X,Y)-data.
**Place the command lines given below (X and Y are the capital letters here):
Xmesh=linspace(min(X),max(X));
Ymesh=polyval(L,Xmesh);
and run the command plot() on the vectors Xmes and Ymesh; make a graph of the line in the 'red' color (see Project 0, Exercise 2).
**Place the command
hold off
to turn off the hold state.
**Output and display the correlation coefficient r for the line (see Project 0, Exercise 2).
%Analyze the plot and the correlation coefficient r and type a comment on whether the constructed regression line fits the (X,Y)-data well. Then, refer to the TEST and justify whether a power function in (1) can be used to fit the original (x, y)-data.
Now, we will proceed with outputting the parameters a and b of the power function in (1).
**Use the equations (3)-(4) and the vector L = (b, A ) , to output and display b and a as below:
b=L(1)
A=L(2);
a=exp(A)
**Display the power function whose parameters are a and b by the command below:
fprintf([ 'the power function is\n' ...
'f(x)=%.3fx^%.3f'],a,b)
Next, we will plot the original data and the power function in the same axis.
**Plot the (x,y)-data by the command below
scatter(x,y)
and set the hold state to on:
hold on
**Create (do not display) the function handle (see Project 0, Exercise 3) for the power function f (x) = axb .
Plot the power function in the same axes with the scatterplot (the command hold on placed previously will allow you to do that) by using the command plot(). Proceed as below:
**Input the vectors
xmesh=linspace(1,60);
ymesh=f(xmesh);
and run the command plot() using these vectors. Make the plot of the function in the 'red' color.
**Place the command hold off
**Use the function f(x) to output the enrollment for the year 2020 (you need to identify correctly the variable x at which the function needs to be evaluated: remember, x is the number of years since 1960). Assign your output to the variable enrollment_2020 and display it (do not put a semicolon after the command that outputs it).
%Analize the plot of the function f(x). Answer the questions below:
Is the enrollment at the Head Start increasing? And if so, is the rate of increase increasing or decreasing —justify your response by referring to the concavity of the graph of f(x).
EXERCISE 3: APPROXIMATING DATA WITH AN EXPONENTIAL FUNCTION
When working on this Exercise, you are required to use MATLAB. For help, please refer to the file Using Live Script (Video) under the Resources section on the Canvas page Assignments, Project 1 and to the file MATLAB Project0 (PDF) located on the Canvas page Assignments, Project 0.
DESCRIPTION: In this Exercise, we will approximate the data by an exponential function. Not all MATLAB packages have the exponential regression feature, that why we are adopting here a technique of fitting the data by an exponential function based on the approach introduced in Exercise 2 of this Project: we will, first, transform the data using the logarithmic function, then we will fit the new data by the polynomial of degree n = 1 of the best fit and, finally, we will use the obtained parameters to output the exponential function that fits the original data.
Below is the description of the algorithm
To fit the data (xk, yk ) , k = 1: n , we will be using an exponential function of the form y = a e bx , (1)
where a and b are real numbers ( a > 0 ) and x ≥ 0 .
Taking natural logarithm of both sides of the equation in (1), we have:
ln y = ln a + bx . (2)
Let ln y = Y and ln a = A , (3)
then (2) becomes
Y = A + bx . (4)
We will construct a polynomial of degree n = 1 of the best fit in the form (4) that approximates the data (xk, Yk ) = (xk , ln yk ) , k = 1: n .
The obtained parameters A and b will be used to output the exponential function f(x) = aebx , where a = eA , that fits the original data (xk, yk ) , k = 1: n .
EXERCISE 3: (Carbon Dioxide) The table below gives the average monthly atmospheric concentration of carbon dioxide, a major contributor to climate change, in parts per million for December of selected years, as recorded at the Mauna Loa Observatory. Source: Economics: NOAA.
Let x be the number of years since 1960 (the year 1960 corresponds to x = 0) and let y be the concentrations of carbon dioxide. We will consider y to be a function of x.
** Place the formatting command:
format('default')
First, we output and display the table which columns are the vectors x and y.
**Input the x- and y-components of the data as the column vectors x and y (see Project 0, Exercise 1). Do not display them here — put a semicolon after a command that outputs a vector.
**Output and display the table with the data by the commands below:
T = table(x,y,...
'VariableNames',{'Years since 1960, x',...
'Concentration, y (in ppm'});
disp(T)
Next, we will plot the data points.
**Place the following commands in your Live Script.
scatter(x,y)
hold on
%Based on the scatterplot and the nature of the topic (change of carbon dioxide concentration with the time), type a comment which of the functions, linear or exponential, could possibly approximate the existing data better and be used for the future predictions.
Now, we proceed with our algorithm and create a new set of data.
**Place the line below in your Live script that outputs the logarithmically transformed y- components of the data:
Y=log(y);
The new data set has components x and Y, where x is the original vector ofthe x-components and Y is the logarithmically transformed vector of the y-components of the data.
Next, we will output the polynomial of degree n = 1 of the best fit for the new data set.
**Use the command polyfit() to output and display (do not put a semicolon after the command) the vector L ofthe coefficients ofthe polynomial of the best fit of degree n = 1 that approximates the (x, Y) - data (see Exercise 2 of Project 0).
Now, we are going to restore the exponential function that approximates the original data.
**Copy and paste the commands below that output (and display) the parameters a and b of the exponential function f(x) = aebx :
b=L(1);
A=L(2);
a=exp(A);
**Output (do not display) the function handle for the exponential function f(x) = aebx (see Project 0, Exercise 3)
**Place the command that displays the exponential function that you created:
fprintf([ 'the exponential function is\n'...
'f(x)=%.4fe^(%.4fx)'],a,b)
Next, we are going to plot the exponential function in the same axis with the data points. Note that you have already placed the command hold on, which makes it possible to add a new plot to the existing axis.
**Place the following line in your code that inputs the set ofthe x-values:
xmesh=linspace(0,70);
and use the command plot() to plot the exponential function f(x); make the plot of f(x) in the 'red' color (see Project 0, Exercise 3).
**Place the command
hold off
to turn off the hold state.
**Next, we will estimate how well the constructed exponential function fits the data by analyzing the correlation coefficient r. Refer to Exercise 2 of Project 0 on how to output the correlation coefficient. Display the correlation coefficient in your Live Script (do not put a semicolon after the command that outputs it).
Note: the vector of the predicted y-values has to be a column vector of the values of the function f on the vector x ofthe x-components of the data — make sure that you had created the function handle for the function f(x) to be able to calculate those values.
%Based on the correlation coefficient r and the plot, type a comment on how well the constructed exponential function fits the original data.
Next, we will work with the exponential function f(x) to obtain some estimates.
**Output and display (do not put a semicolon after the command) the average rate of change in the function f(x) over the period from 1960 to 2019 (use the formula for the average rate of change). Assign your output to the variable averinc1960_2019.
Our next task is to predict the first year when the concentration will be double what it was in 1960 if the current trend continues.
**Output (do not display) the reference value of a doubled concentration.
D=2*y(1);
**Construct the function handle for the function that is the difference between f(x) and the constant function D as below:
dif=@(x) f(x)-D;
and approximate its zero by using a MATLAB command fzero() (see Exercise 3 of Project 0).
To choose an initial approximation, we will plot the functions dif and y = 0 in the same axis.
**Construct the function handle for y = 0 (See Exercise 3 of Project 0)
**Plot both functions dif and y in the new axis by the set of commands: xmesh=linspace(0,200);
plot(xmesh,dif(xmesh), 'red',xmesh,y(xmesh), '-- ')
grid on
Using the plot, choose an initial approximation for the zero of the function dif as a vector x0 with two entries: the first entry is a number to the left of the approximated zero, and the second entry is the number to the right of the approximated zero, such that the function dif has the
opposite signs at these numbers (see Project 0, Exercise 3).
**Input your vector x0 (do not display it — put a semicolon after the command).
**Use the command fzero() to output the approximation of the desired zero — assign it to the variable zero (do not display it).
**Then, output the actual year when the concentration will be double what it was in year 1960 (do not display it):
year=1960+zero;
**Display your result by the command below (which also formats the output):
fprintf( 'concentration will double in year %.f',year)
