Jump to content

MATLAB Question (Finding Coefficients of a Polynomial)


mooeypoo

Recommended Posts

Okay, I am completely stuck.

 

I have a homework question that I just can't manage to find the right approach for. I know what I should do if I solve it manually, though it's a long process, but I can't manage to build the function, and I really -- really really -- need help. Quite urgently, actually. It's due tomorrow. I've been trying this for the entire day already.

 

The sad thing is that the solution is probably quite easy, and I am going to feel really stupid :P but I'm going to risk it.

 

The problem is this:

 

Suppose it is known that the graph of the function

[math]y=ax^3+bx^2+cx+d[/math]

passes through four points (xi,yi), i=1,2,3,4. Write a user-defined function that accepts these four points as input and computes the coefficients a, b, c and d. The function should solve four linear equations in terms of the four unknowns a,b,c,d.

Test your function for the case where (xi,yi) = (-2,-20), (0,4), (2,68), (4,508), whose answer is a=7, b=5, c=-6, and d=4

Now. I am thinking that I need to do this in stages, so I have my representations of a, b, c and d:

[math]a=(bx^2+cx+d-y)/x^3[/math]

[math] b=(ax^3+cx+d-y)/x^2[/math]

[math]c=(ax^3+bx^2+d-y)/x[/math]

[math] d=ax^3+bx^2+cx+d[/math]

 

But I have two problems. First, these are unknown, so if I try to compute them one by one in the function, i get an error message because the variables are unknown. I tried defining them as symbolic (syms) but that didn't work. Plus.. they're not really symbolic, so I am not sure how to do that.

 

I think I'm overcomplicating things, but I did try this method:

I represented C in terms of a b d and y, and then replaced it within the representation of a, and simplified everything to get:

 

[math]

a=((1+x^2)/(1-x^2))*(b/x + (d-y)/x^3)

[/math]

 

Agh.. okay that didn't really lead me anywhere, other than cancelling out C and representing the equation in a bit of a more ordered way... but I had to try...

 

help... <sobs> heelllppppp...

 

 

~moo

Link to comment
Share on other sites

Do you know how to solve linear systems of equations of the type Ma=b (with M being the matrix of coefficients, a being the vector looked for and b some other vector)?

 

This case, you want to solve

 

[math]\left( \begin{array}{cccc} x_1^3 & x_1^2 & x_1 & 1 \\ x_2^3 & x_2^2 & x_2 & 1 \\ x_3^3 & x_3^2 & x_3 & 1 \\ x_4^3 & x_4^2 & x_4 & 1 \end{array} \right) \left( \begin{array}{c} a \\ b \\ c \\ d \end{array} \right) = \left( \begin{array}{c} y_1 \\ y_2 \\ y_3 \\ y_4 \end{array} \right)[/math]

Link to comment
Share on other sites

Okay. No.. I have no idea how to do that O.o

 

Well, I know how to multiply matrices in vector, I just don't know the method you're talking about here (I'm new to Matrices... egh)

 

What do the x31, x32, x33, x34 ... mean? multiplications?

 

I'm sorry for being a pain.. I am new to both Matlab and Matrices, and my book doesn't really give examples for this.

 

~moo

 

----- EDIT: -----

 

Oh, another thing --

 

I understand the x32 x23 etc are actually *powers* of the different xs.. I just saw lots of numbers with x's and freaked out. Sorry :P

 

But.. the question in the book requests that the function solves 4 linear equations to figure out the a b c d variables... I am not sure if that fits under that. On the other hand, I've been working on making 4 linear equations to represent the different variables with not much success, so I guess I'm going to do that instead ...

 

Uhm.. Thanks a bunch :)

 

~moo

 

Okay, It's working!

 

I reviewed half the book again (sobs) but I finally managed to apply the technique you showed. Here's the final code:

 

disp('Executing Problem #17');
function [A,B,C,D]=getcoeffs(x1,y1,x2,y2,x3,y3,x4,y4)
   %create a matrix for x variables:
   XMatrix=[x1^3 x1^2 x1 1;x2^3 x2^2 x2 1;x3^3 x3^2 x3 1;x4^3 x4^2 x4 1]
   YVector=[y1; y2; y3; y4];
   Allvars=XMatrix\YVector;
   A=Allvars(1);
   B=Allvars(2);
   C=Allvars(3);
   D=Allvars(4);
end
%%Command:
[a b c d]=getcoeffs(-2,-20,0,4,2,68,4,508);

 

Works like a charm. Thank you VERY VERY MUCH!!!!!

 

~moo

Link to comment
Share on other sites

np, glad that little piece of information was enough for you. On a sidenote, because I am not sure to what extent that became clear: Solving the matrix equation I gave is exactly the same as solving a system of four linear equations. Using matrices is just the way it is done in numerics.

As a simplified example:

Solving

[math]

\left( \begin{array}{cc} a & b \\ c & d \end{array}\right) \left(\begin{array}{c} x_1 \\ x_2 \end{array} \right) = \left(\begin{array}{c} i \\ j \end{array} \right)

[/math]

for x1 and x2 (with all other variables given) is exactly the same as solving the system

a x1 + b x2 = i

c x1 + d x2 = j

for x1 and x2 with all other variables given. Performing the matrix multiplication the matrix expression reads

[math]

\left( \begin{array}{cc} a & b \\ c & d \end{array}\right) \left(\begin{array}{c} x_1 \\ x_2 \end{array} \right) =

\left(\begin{array}{c} a x_1 + b x_2 \\ c x_1 + d x_2 \end{array} \right)[/math],

which is just a vector (and must be because it shall equal some vector. Therefore you have to solve

[math]

\left(\begin{array}{c} a x_1 + b x_2 \\ c x_1 + d x_2 \end{array} \right)

= \left(\begin{array}{c} i \\ j \end{array} \right) [/math]

for x1 and x2 such that the equation is true for all components of the vectors. That is equivalent to solving the system of linear equations; each of the two component of the vectors leads to one of the two linear equations.

Link to comment
Share on other sites

Yeah I knew that for when a b c and d are known. There is also a command called "roots()" in Matlab and "poly()" that deals with finding the roots of and building a polynomial from the roots. However, this time I had the other way around -- no a b c d and 4 sets of x and y, which is why I was that lost.

 

Your example showed how to implement the same technique that I generally knew when dealing with missing y/x for this problem - with the missing abcd vars.

 

Thanks! :)

 

~moo

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.