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

An expert solved the following problem; however, there is one issue. I am gettin

ID: 3917695 • Letter: A

Question

An expert solved the following problem; however, there is one issue. I am getting the outputs for cStar in reverse order when doing the test case. The issue is the line coef = polyfit(X,Y,degree). I am supposed to refer to a lecture slide to figure out this problem, so I must use a matrix for X, use a similar matrix for Y, and then use XY instead of using polyfit. Please refer to the picture of the matrix of X and do the same for Y. Can you please modify the code to meet these requirements and make it work. Thanks!

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 (Use matrix for X and do the same for Y, finally XY)

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

model: y=a?

Explanation / Answer

Dear for fitting a polynomial, you don't need to augment the Y matrix. Creating an augmented matrix with values of different powers of x is sufficient as you are going to fit Y = f(X) not X=f(Y). In other words you are modeling a polynomial in X to fit to the values of Y and not the other way round.

Please find the required script giving you the order of coefficeint and XY operation included as per the requirement.

%===================================================================

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);

% Creating augmented X matrix as mentioned:
X=[];
for i=1:rowx
for j=1:degree+1
X(i,j)=[xdata(i)^(j-1)];
end
end

Y = ydata;

% Creating a polynomial fit of given order between data (Use matrix for X and do the same for Y, finally XY)

coef = XY;

% 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(X*coef - Y,2);

flag = 1;

end

end

%=====================================================================

Sample output:

Comparison of earlier script and modified script:

Earlier script

cStar =

   -3.1119
   -1.8029
    5.4400
   -1.0171
    0.0613


err =

    0.6867


flag =

     1

Modified script:

cStar =

    0.0613
   -1.0171
    5.4400
   -1.8029
   -3.1119


err =

    0.6867


flag =

     1

Hope this helps! Btw, i was the one who provided you the earlier answer. :)

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote