Quadratic Lagrange interpolation

asked 2024-02-11 17:09:54 +0100

Andr gravatar image

updated 2024-02-12 11:26:31 +0100

FrédéricC gravatar image

Lagrange quadratic polynomials are described in http://mathonline.wikidot.com/quadrat... How to compute A,B and C in school formula Ax^2+Bx+C = 0 ?

solution:

x, x0, x1, x2, y0, y1, y2 = var('x x0 x1 x2 y0 y1 y2')
expr = y0*(x-x1)*(x-x2)/((x0-x1)*(x0-x2))+y1*(x-x0)*(x-x2)/((x1-x0)*(x1-x2))+y2*(x-x0)*(x-x1)/((x2-x0)*(x2-x1))
expanded_expr = expand(expr)
collected_expr = expanded_expr.collect(x)
A = collected_expr.coefficient(x^2)
B = collected_expr.coefficient(x)
C = collected_expr.coefficient(x, 0)

result:

sage: A
y0/((x0 - x1)*(x0 - x2)) - y1/((x0 - x1)*(x1 - x2)) + y2/((x0 - x2)*(x1 - x2))
sage: B
-x1*y0/((x0 - x1)*(x0 - x2)) - x2*y0/((x0 - x1)*(x0 - x2)) + x0*y1/((x0 - x1)*(x1 - x2)) + x2*y1/((x0 - x1)*(x1 - x2)) - x0*y2/((x0 - x2)*(x1 - x2)) - x1*y2/((x0 - x2)*(x1 - x2))
sage: C
x1*x2*y0/((x0 - x1)*(x0 - x2)) - x0*x2*y1/((x0 - x1)*(x1 - x2)) + x0*x1*y2/((x0 - x2)*(x1 - x2))

Can be substituted in C/C++ programs:

double dx0 = x1 - x0;
double dx1 = x2 - x1;
double dx2 = x0 - x2;
double m01 = dx0 * dx1;
double m12 = dx1 * dx2;
double m02 = dx0 * dx2;

result:

A = -(y0 / m02 + y1 / m01 + y2 / m12);
B = (y0 * (x1 + x2) / m02) + (y1 * (x0 + x2) / m01) + (y2 * (x0 + x1) / m12);
C = -(x1*x2*y0/m02 + x0*x2*y1/m01 + x0*x1*y2/m12);
edit retag flag offensive close merge delete

Comments

Given what? Please formulate your problem comprehensively.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-02-11 17:59:15 +0100 )edit

You may look up the subs method of symbolic expressions (and its various syntaxes). Hints :

sage: var("a, b")
(a, b)
sage: foo=(a+2*b) ; foo
a + 2*b
sage: foo.subs(b=2)
a + 4
sage: foo.subs(b==2*a).subs(a=3)
15

But :

sage: foo.subs([b==2*a, a==3])
4*a + 3

(Hint : why ? )

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-02-12 12:56:54 +0100 )edit