In: Computer Science
Create a Matlab program to load in the attached file File_Q5_Use.csv. You will first need to click on the link to open it, then save it as a .csv file in the directory you are using in your Matlab programs before you can load it in to Matlab.
It has 2 columns, the first column is the x values, the second column is the y values. Set Figure (1). Plot the points using red stars and a blue line with a title of 'Original Points' and get a general idea of what the degree of the polynomial is. It might be easier to split the matrix into 2 vectors x_vec and y_vec.
From the graph - where approximately are the real zeros of this polynomial? Does this polynomial appear to be ODD or EVEN. ? Answer the questions in the %RESULTS.
After this plot, set Figure (2) to get a different Figure window open but so that the old figure window does not close.
Next, use POLYFIT to find the polynomial that fits the data [Hint: it is less than degree 8 and more than degree 1]. Use a FOR loop to cycle through the from n = 2:1:7 and do the following on each loop:
a. Find using POLYFIT, the polynomial for each degree of n. Do NOT suppress the values of the coefficients returned by each pass through the FOR loop.
b. Using SUBPLOT where n is the location, plot on the same graph, the original points using red stars and the polynomial created by POLYFIT using a range of [-3:0.1:3] using a blue line. Make a title for each subgraph showing the degree of the polynomial fitted.
Based upon the 6 graphs, which do you think is the correct degree of the polynomial? Answer in %RESULTS.
The basis for this polynomial was: 2x^5 - 3x^4 + 2x^3 -3x^2 - 144x + 216 = 0. Create a Figure 3 which plots the original points in red stars and points from this polynomial in a blue line for a range of [-3 : 0.1 : 3 ].
Looking at the graphs, how many data points appear to be outliers, i.e. they probably should not be used in the graph? Answer in %RESULTS.
Looking at the graphs, the plots for degree 5, 6, 7 all appear to be almost the same. Switch to full screen to examine Figure 2. Remembering that the line of best fit passes through as many points as possible and minimizes the distances between the line and the points, can you determine by eye which degree of 5, 6, or 7 would be best?
Save as LastName_FirstName_Quiz_5_6_Q5
File to open. Hint: Make sure this file is in the same directory where you are saving LastName_FirstName_Quiz_5_6_Q5.
File_Q5_Use.csv
this is the file content
-3 | -162 |
-2.9 | -63 |
-2.8 | 23 |
-2.7 | 98 |
-2.6 | 161 |
-2.5 | 315 |
-2.4 | 258 |
-2.3 | 294 |
-2.2 | 324 |
-2.1 | 347 |
-2 | 364 |
-1.9 | 376 |
-1.8 | 385 |
-1.7 | 389 |
-1.6 | 389.5 |
-1.5 | 388 |
-1.4 | 384 |
-1.3 | 378 |
-1.2 | 370 |
-1.1 | 360 |
-1 | 350 |
-0.9 | 339 |
-0.8 | 326 |
-0.7 | 314 |
-0.6 | 299 |
-0.5 | 285 |
-0.4 | 273 |
-0.3 | 258 |
-0.2 | 245 |
-0.1 | 329 |
0 | 216 |
0.1 | 200 |
0.2 | 187 |
0.3 | 174 |
0.4 | 153 |
0.5 | 143 |
0.6 | 129 |
0.7 | 114 |
0.8 | 99 |
0.9 | 84 |
1 | 70 |
1.1 | 55 |
1.2 | 41 |
1.3 | 27 |
1.4 | 13 |
1.5 | 0 |
1.6 | -13 |
1.7 | -24 |
1.8 | -35 |
1.9 | -44 |
2 | -52 |
2.1 | -58 |
2.2 | -61.5 |
2.3 | -62 |
2.4 | -59 |
2.5 | -53 |
2.6 | -43 |
2.7 | -28 |
2.8 | -7 |
2.9 | 20 |
3 | 54 |
(*Note: Please up-vote. If any doubt, please let me know in the comments)
Matlab Code:
data = csvread('File_Q5_Use.csv');
%dividing the data into x & y vectors
x_vec = data(:,1);
y_vec = data(:,2);
%set figure 1
figure(1)
%make first plot as per the required specifications
plot(x_vec,y_vec,'Marker','*','color','b','markerfacecolor','r','MarkerEdgeColor','r')
title('Original Points')
%set figure 2
figure(2)
for n=2:1:7
subplot(2,3,n-1)
p = polyfit(x_vec,y_vec,n)
x = -3:0.1:3
y = polyval(p,x)
plot(x,y)
hold on
%plotting original data using red stars
plot(x_vec,y_vec,'Marker','*','markerfacecolor','r','MarkerEdgeColor','r','linestyle','None')
s = sprintf("Degree= %d",n)
title(s)
end
figure(3)
x = -3:0.1:3
basis = 2*x.^5 - 3*x.^4 + 2*x.^3 -3*x.^2 - 144*x + 216
%plotting original data using red stars
plot(x_vec,y_vec,'Marker','*','markerfacecolor','r','MarkerEdgeColor','r','linestyle','None')
hold on
%plotting the given function in blue line
plot(x,basis,'color','b')
%RESULTS: Based on the 6 subplots, the polynomial seems to be
degree 5 as the points are best aligned with the curve
%Based on figure 3, 2 points appear to be outliers
Output Screenshots: