Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please solve the following question using Matlab Language. When it refers to X &

ID: 3916793 • Letter: P

Question

Please solve the following question using Matlab Language.

When it refers to X & Y I think it it is reffering to setting up XA=Y and taking A = XY. I can't post the .mat file, but you can still create the code without it. Thanks!

Homework Problem 2: Polynomial Regression In the previous problem, you fitted a line to a given data in the x-y plane. However, from graphical visualization, you must have observed this to be a crude fit. In this problem, you are going to use a polynomial to fit the data Create a function polyRegression which takes in three input arguments xdata, ydata and p the degree of the polynomial. Note that xdata and ydata are stored as nxl column vectors You should return three output arguments - cStar, a (p+1) x 1 column array which corresponds to the coefficients of the polynomial fitted, err which is calculated using the MATLAB command norm(XA-Y,2) where X and Y are as discussed in lecture, and flag which should be assigned to be 1 if everything works out okay, or -1 in case of an error In case of an error, you must return cStar to be empty and err to be Inf. Errors should be detected for two cases: if the sizes of xdata and ydata do not match, or if the number of coefficients of the polynomial are more than the no of input data points. You should test your function with the data provided in problem2data.mat. You should be able to reproduce the following result. > [cStar , err,flag] -polyRegression (xdata, ydata ,4) ?. 9364 1.0261 -11.3279 32.6086 32.9614 err 1.2742 Store the code of this problem in the filename polyRegression.m. This file must go into your zipped fo »lder as it will be evaluated.

Explanation / Answer

As the data set is not given, as sample of 10x1 x and y data set is generated.

Please find the required script as the following:

Note: Be very careful while copying/re-writing the code. The function name must be the same as the name of the file it is contained in.

%================= Main script starts ====================

clear all;
clc;

% Generating random column vectors for xdata and ydata
xdata=rand(10,1);
ydata=rand(10,1);

% Specifying the degree
degree=4;

% Calling the regression function
[cStar,err,flag] = polyRegression(xdata,ydata,degree)

%================= Main script ends ====================

%================= Function starts ====================

function [cStar,err,flag] = polyRegression(xdata,ydata,degree)

% Assuming and creating xdata and ydata as a column vector
[rowx,colx]=size(xdata);
[rowy,coly]=size(ydata);
X = xdata;
Y = ydata;

% Creating a polynomial fit of given order between data
coef = polyfit(X,Y,degree)';

% Calculating the size of coefficient matrix
[rowc,colc] = size(coef);

% Checking conditions and generating results
if(rowc>rowx || rowx~=rowy)
flag=-1;
cStar=[];
err=inf;
else
cStar = coef;
err = norm(polyval(coef,X) - Y,2);
flag = 1;
end

end

%================= Function ends ====================

Sample output:


cStar =

   -0.5479
    0.1188
   -0.9357
    1.3027
    0.1389


err =

    0.6121


flag =

     1

Note: as the values are randomly generated for each iteration, the result will vary for each iteration too.

Hope this helps!

PLEASE THUMBS UP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!