# 多项式计算¶

## 计算直线¶

### 数学背景¶

$y(x, x_0, y_0, x_1, y_1)$

$y(x, x_0, y_0, x_1, y_1) = x\frac{y_1-y_0}{x_1-x_0} -\frac{x_1+x_0}{2(x_1-x_0)}\frac{y_1-y_0}{2}+\frac{y_1+y_0}{2}$

$\mathtt{Line}(x, \vec{p_0}, \vec{p_1})$

### Modelica描述¶

function Line "Compute coordinates along a line"
input Real x     "Independent variable";
input Real p0[2] "Coordinates for one point on the line";
input Real p1[2] "Coordinates for another point on the line";
output Real y    "Value of y at the specified x";
algorithm
y := x*(p1[2]-p0[2])/(p1[1]-p0[1])+
(p1[2]+p0[2]-(p1[1]+p0[1])*(p1[2]-p0[2])/
(p1[1]-p0[1]))/2.0;
end Line;


Line模型唯一不足之处在于计算y表达式的长度。如果我们可以拆分表达式，则就能改善这种不足。

### 中间变量¶

function LineWithProtected "The Line function with protected variables"
input Real x     "Independent variable";
input Real p0[2] "Coordinates for one point on the line";
input Real p1[2] "Coordinates for another point on the line";
output Real y    "Value of y at the specified x";
protected
Real m = (p1[2]-p0[2])/(p1[1]-p0[1])        "Slope";
Real b = (p1[2]+p0[2]-m*(p1[1]+p0[1]))/2.0  "Offset";
algorithm
y := m*x+b;
end LineWithProtected;


## 计算多项式结果¶

### 数学定义¶

$p(x, \vec{c})$

$p(x, \vec{c}) = \sum_{i=1}^{N} c_i x^{N-i}$

$p(x, \vec{c}) = ((c_1 x + c_2) x + c_3) x +c_4$

### Modelica定义¶

function Polynomial "Create a generic polynomial from coefficients"
input Real x     "Independent variable";
input Real c[:]  "Polynomial coefficients";
output Real y    "Computed polynomial value";
protected
Integer n = size(c,1);
algorithm
y := c[1];
for i in 2:n loop
y := y*x + c[i];
end for;
end Polynomial;


model EvaluationTest1 "Model that evaluates a polynomial"
Real yf;
Real yp;
equation
yf = Polynomial(time, {1, -2, 2});
yp = time^2-2*time+2;
end EvaluationTest1;


### 微分¶

model Differentiation1 "Model that differentiates a function"
Real yf;
Real yp;
Real d_yf;
Real d_yp;
equation
yf = Polynomial(time, {1, -2, 2});
yp = time^2-2*time+2;
d_yf = der(yf); // How to compute?
d_yp = der(yp);
end Differentiation1;


function PolynomialWithDerivative
"Create a generic polynomial from coefficients (with derivative information)"
input Real x     "Independent variable";
input Real c[:]  "Polynomial coefficients";
output Real y    "Computed polynomial value";
protected
Integer n = size(c,1);
algorithm
y := c[1];
for i in 2:n loop
y := y*x + c[i];
end for;
annotation(derivative=PolynomialFirstDerivative);
end PolynomialWithDerivative;


  annotation(derivative=PolynomialFirstDerivative);


$p(x, \vec{c}) = \sum_{i=1}^{N} c_i x^{N-i}$

$\frac{\mathrm{d}p(x, \vec{c})}{\mathrm{d}z} = \frac{\partial p}{\partial x} \frac{\mathrm{d}x}{\mathrm{d}z} + \frac{\partial p}{\partial \vec{c}} \frac{\mathrm{d}\vec{c}}{\mathrm{d}z}$

$\frac{\partial p}{\partial x} = p(x, c')$

$c'_i = (N-i)c_i$

$\frac{\partial p}{\partial c_i} = p(x, \vec{d_i})$

$df(x, \vec{c}, \frac{\mathrm{d}x}{\mathrm{d}z}, \frac{\mathrm{d}\vec{c}}{\mathrm{d}z})$

$df(x, \vec{c}, \frac{\mathrm{d}x}{\mathrm{d}z}, \frac{\mathrm{d}\vec{c}}{\mathrm{d}z}) = \frac{\mathrm{d}f}{\mathrm{d}z}$

function PolynomialFirstDerivative
"First derivative of the function Polynomial"
input Real x;
input Real c[:];
input Real x_der;
input Real c_der[size(c,1)];
output Real y_der;
protected
Integer n = size(c,1);
Real c_diff[n-1] = {(n-i)*c[i] for i in 1:n-1};
algorithm
y_der :=PolynomialWithDerivative(x, c_diff)*x_der +
PolynomialWithDerivative(x, c_der);
end PolynomialFirstDerivative;


model Differentiation2 "Model that differentiates a function using derivative annotation"
Real yf;
Real yp;
Real d_yf;
Real d_yp;
equation
yf = PolynomialWithDerivative(time, {1, -2, 2});
yp = time^2-2*time+2;
d_yf = der(yf);
d_yp = der(yp);
end Differentiation2;