- Details
- Parent Category: Programming Assignments' Solutions
We Helped With This MATLAB Programming Assignment: Have A Similar One?
SOLVED

Short Assignment Requirements
Please write a matlab script for the attached matlab question
Assignment Image
![MATLAB Assignment Description Image [Solution]](/images_solved/751489/1/im.webp)
Q2.
In the accompanying file, climate_data.txt, the average global temperature (in degrees Fahrenheit) is
provided for the years 1880 through 2015 [from NOAA.gov]. The first column of data contains the
number of years since 1880 (i.e. "10" would be the year 1890), and the second column contains the
average global temperature for that year. As with any natural phenomena, these temperatures consist of
underlying trends overlaid with random fluctuations. We will use least-squares in an attempt to extract
these underlying trends.
Following MATLAB code constructs the best cubic polynomial fit to the data, use it to plot both the data
and resulting predictions from 1880 through 2025. The code is commented to better explain the process:
>> load climate_data.txt% load data into MATLAB
>> years = climate_data(:,1); % extract 1st column as "years" vector
>> temps = climate_data(:,2); extract 2nd column as "temps" vector
& build the matrix and solve the least-squares system.
% Note: the .^ notation raises each entry to the requested power
>> A = [years.^3, years. ^2, years. ^1, years. ^0]; % construct A
>> b= temps; % build the right-hand side
>> x = (A *A) (A¹*b); solve the least-squares problem
& set the dates that we want to plot over (offset by 1880)
>> dates = linspace (0, 145, 200);
& compute the projections based on the model
>> proj
x (1) *dates.^3 + x (2) *dates. ^2 + x (3) *dates.^1 + x (4) *dates. ^0;
plot the data as black dots and the model as a blue line
>> plot (years+1880, temps, 'k.', dates+1880, proj, 'b-')
>> xlabel('year')
>> ylabel('temperature')
>> title ('Climate data and cubic fit!).
Note here that our cubic polynomial has the form
p(t) = x₁t³ + x₂t² + x3 + x4
so the matrix in our over determined linear system has the form
A =
tz
⠀
tm
1
⠀
1
.0
m
Perform the above commands. Repeat the process for both a quadratic model and a linear model.
For these models, you should only need to modify the lines
A = [years. ^3, years.^2, years.^1, years. ^0];
and
proj = x (1) *dates.^3 + x (2) *dates. ^2 + x(3) *dates.^1 + x (4) *dates. ^0;
and
title ('Climate data and cubic fit')
Turn in printouts of your three plots for question 2 only.
DO NOT print matrices A and b for either questions 1 and 2!!