user3550036
user3550036

Reputation: 1

Curve Fit 5 points

I am trying to curve fit 5 points in C. I have used this code from a previous post (Can sombody simplify this equation for me?) to do 4 points, but now I need to add another point.

// Input data: arrays x[] and y[]
// x[1],x[2],x[3],x[4] - X values
// y[1],y[2],y[3],y[4] - Y values

// Calculations
A = 0
B = 0
C = 0
D = 0
S1 = x[1] + x[2] + x[3] + x[4]
S2 = x[1]*x[2] + x[1]*x[3] + x[1]*x[4] + x[2]*x[3] + x[2]*x[4] + x[3]*x[4]
S3 = x[1]*x[2]*x[3] + x[1]*x[2]*x[4] + x[1]*x[3]*x[4] + x[2]*x[3]*x[4]
for i = 1 to 4 loop
   C0 = y[i]/(((4*x[i]-3*S1)*x[i]+2*S2)*x[i]-S3)
   C1 = C0*(S1 - x[i])
   C2 = S2*C0 - C1*x[i]
   C3 = S3*C0 - C2*x[i]
   A = A + C0
   B = B - C1
   C = C + C2
   D = D - C3
end-loop

// Result: A, B, C, D

I have been trying to covert this to a 5 point curve fit, but am having trouble figuring out what goes inside the loop:

// Input data: arrays x[] and y[]
// x[1],x[2],x[3],x[4],x[5] - X values
// y[1],y[2],y[3],y[4],y[5] - Y values

// Calculations
A = 0
B = 0
C = 0
D = 0
E = 0
S1 = x[1] + x[2] + x[3] + x[4]
S2 = x[1]*x[2] + x[1]*x[3] + x[1]*x[4] + x[2]*x[3] + x[2]*x[4] + x[3]*x[4]
S3 = x[1]*x[2]*x[3] + x[1]*x[2]*x[4] + x[1]*x[3]*x[4] + x[2]*x[3]*x[4]
S4 = x[1]*x[2]*x[3]*x[4] + x[1]*x[2]*x[3]*[5] + x[1]*x[2]*x[4]*[5] + x[1]*x[3]*x[4]*[5] + x[2]*x[3]*x[4]*[5]

for i = 1 to 4 loop
   C0 = ??
   C1 = ??
   C2 = ??
   C3 = ??
   C4 = ??
   A = A + C0
   B = B - C1
   C = C + C2
   D = D - C3
   E = E + C4
end-loop

// Result: A, B, C, D, E

any help in filling out the C0...C4 would be appreciated. I know this has to do with the matrices but I have not been able to figure it out. examples with pseudo code or real code would be most helpful.

thanks

Upvotes: 0

Views: 997

Answers (2)

Logan
Logan

Reputation: 1682

I refuse to miss this opportunity to generalize. :)

Instead, we're going to learn a little bit about Lagrange polynomials and the Newton Divided Difference Method of their computation.

Lagrange Polynomials

Given n+1 data points, the interpolating polynomial is

Interpolating Polynomial Definition

where l_j(i) is

l_i definition.

What this means is that we can find the polynomial approximating the n+1 points, regardless of spacing, etc, by just summing these polynomials. However, this is a bit of a pain and I wouldn't want to do it in C. Let's take a look at Newton Polynomials.

Newton Polynomials

Same start, given n+1 data points, the approximating polynomial is going to be

enter image description here where each n(x) is

enter image description here with a coefficient of

enter image description here, being the divided difference.

The final form end's up looking like

enter image description here.

As you can see, the formula is pretty easy given the divided difference values. You just do each new divided difference and multiply by each point so far. It should be noted that you'll end up with a polynomial of degree n from n+1 points.

Divided Difference

All that's left is to define the divided difference which is really best explained by these two pictures:

enter image description here

and

enter image description here.

With this information, a C implementation should be reasonable to do. I hope this helps and I hope you learned something! :)

Upvotes: 3

John Alexiou
John Alexiou

Reputation: 29244

If the x values are equally spaced with x2-x1=h, x3-x2=h, x4-x3=h and x5-x4=h then

C0 = y1;
C1 = -(25*y1-48*y2+36*y3-16*y4+3*y5)/(12*h);
C2 = (35*y1-104*y2+114*y3-56*y4+11*y5)/(24*h*h);
C3 = -(5*y1-18*y2+24*y3-14*y4+3*y5)/(12*h*h*h);
C4 = (y1-4*y2+6*y3-4*y4+y5)/(24*h*h*h*h);

y(x) = C0+C1*(x-x1)+C2*(x-x1)^2+C3*(x-x1)^3+C4*(x-x1)^4
// where `^` denotes exponentiation (and not XOR).

Upvotes: 0

Related Questions