Lagrange quadratic polynomials are described in http://mathonline.wikidot.com/quadratic-lagrange-interpolating-polynomials How to compute A,B and C in school formula Ax^2+Bx+C = 0 ?
1 | initial version |
Lagrange quadratic polynomials are described in http://mathonline.wikidot.com/quadratic-lagrange-interpolating-polynomials How to compute A,B and C in school formula Ax^2+Bx+C = 0 ?
Lagrange quadratic polynomials are described in http://mathonline.wikidot.com/quadratic-lagrange-interpolating-polynomials 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);
Lagrange quadratic polynomials are described in http://mathonline.wikidot.com/quadratic-lagrange-interpolating-polynomials 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);