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
(*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:
i hope it helps..
If you have any doubts please comment and please don't dislike.
PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME