-1

This question fairly easy doing it manually however, I am struggling to have this written in code.

There is a quartic polynomial:

P(x)=ax^4+bx^3+cx^2+dx+e

There is also a given matrix M:

5 0 -1 2 9
-2 -1 0 1 2

Which the first row gives P(x) and the second row gives the value of x.

Using the information in matrix M, find the coefficients:

a, b, c, d, e

I would know how to work this manually, subbing each column and solve simultaneously with the other columns to obtain a value for each coefficient or put it in a matrix.

I have an idea of what to do, but I don't know how to code it.

I do believe the last line would be linearsolve(M(,1),M(,2)) and thus be able to obtain each coefficient but I have no idea how to get to that line.

i9-9980XE
  • 11
  • 5
  • I've tried ```y=M(1,:); x=M(2,:); a = polyval(y,x);``` already. and It gives me ```p(x) = 81*x^8 + 11*x^7 + 9*x^6 + 15*x^5 + 89*x^4 + 0*x^3 + 0*x^2 + 0*x^1 + 0```. I don't know how or why as it is supposedly suppose to be a quartic. – i9-9980XE Apr 29 '19 at 05:05
  • Thanks for your reply @Andy. However, I have also tried that already polyfit(y,x,n) where n = 4 as it's the 4th degree. and I get values of 0 or very close to zero. ```p(x) = 0*x^8 + 0*x^7 + 1*x^6 + 0*x^5 - 1*x^4 + 0*x^3 + 0*x^2 + 0*x^1 + 0``` – i9-9980XE Apr 29 '19 at 05:22

2 Answers2

0

Welcome J Cheong

% Values of y and x (note: in your post you had 2 values at x = 1, I assumed that was an accident)
M = [5 0 -1 2 9 ; -2 -1 0 1 2];

% Separate for clarity
y = M(1,:);
x = M(2,:);

% Fit to highest order polynomial model
f = fit(x',y',['poly', num2str(length(y)-1)])

% Extract coefficients
coeff = coeffvalues(f);

% Plotting
X = linspace(min(x)-1, max(x) + 1, 1000) ;
plot(x,y,'.',X,f(X))

Edit

Sorry, I'm using Matlab. Looking at the Octave documentation. You should be able to get the coefficients using

p = polyfit(x,y,length(y)-1)';

Then to display the coefficients the way you specified try this

strcat(cellstr(char(96+(1:length(p))')), { ' = ' } , cellstr(num2str(p)))
user1543042
  • 3,422
  • 1
  • 17
  • 31
  • Hello @user1543042 and thank you for your reply. That is correct, I have changed the value. I have tried what you have written and have received this. ``` error: 'fit' undefined near line 21 column 3 error: called from code at line 21 column 2 ``` How would I be able to have it generalised in the sense that I can do ```print( a= b= c= d= e= )``` Regardless of what polynomial i choose the quartic to be – i9-9980XE Apr 28 '19 at 17:05
  • I am using octave *** – i9-9980XE Apr 28 '19 at 17:34
  • Thank you, I was able to base a solution off your edited response! – i9-9980XE Apr 29 '19 at 06:29
0
y=[5 0 -1 2 9];
x=[-2 -1 0 1 2];
P=polyfit(x,y,2)

gives

P =
   2.0000   1.0000  -1.0000

these are your coefficients for c,d,e the others are zero. You can check the result:

polyval(P, x)

ans =
   5.0000e+00   2.2204e-16  -1.0000e+00   2.0000e+00   9.0000e+00

which gives you y

Btw, you can solve this very fast just inside your head without calculator because the function values for x=0 and x=+/-1 are very easy to calculate.

Andy
  • 7,931
  • 4
  • 25
  • 45
  • This is what I've written in my code. ```y=M(1,:); x=M(2,:); P=polyfit(x,y,2); p=polyval(P, x); a = p(1); b = p(2); c = p(3); d = p(4); e = p(5);``` Can you clarify that this is correct ? – i9-9980XE Apr 29 '19 at 05:36