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. I posted a picture fr

ID: 3917234 • Letter: P

Question

Please solve the following question using Matlab Language. I posted a picture from my lecture to show what X ,Y, and A are.

Here are some hints for the following problem:

% Be careful: xdata and ydata are nx1 column vectors.

% Set up the matrices XA = Y, use A = XY. (Please look at the picture for what X,Y,and A are)

% cStar - (p+1)x1. Keep this dimensioning in mind.

% err - Use the command norm(X*A-Y,2).

% flag - Should return 1 if everything is okay, -1 if an error exists.

% If size xdata and ydata are not the same -> flag = -1, cStar = empty,

err= Inf.

% If no of coefficients of polynomial (p+1) are more than no of data points, then error so same thing.

Thanks!!

2 3 3 ayyy 2 3 1

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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!