对于 f ( x ) = a 0 x n + a 1 x n − 1 . . . a n − 1 x + a n f(x)=a_0x^n+a_1x^{n-1}...a_{n-1}x+a_n f(x)=a0xn+a1xn−1...an−1x+an
计算顺序按表格从上往下,从左往右
a 0 a_0 a0 | a 1 a_1 a1 | a 2 a_2 a2 | … | a n − 1 a_{n-1} an−1 | a n a_n an | |
---|---|---|---|---|---|---|
x = x 0 x=x_0 x=x0 | b 0 x 0 b_0x_0 b0x0 | b 1 x 0 b_1x_0 b1x0 | … | b n − 2 x 0 b_{n-2}x_0 bn−2x0 | b n − 1 x 0 b_{n-1}x_0 bn−1x0 | |
b 0 b_0 b0 | b 1 b_1 b1 | b 2 b_2 b2 | … | b n − 1 b_{n-1} bn−1 | b n = f ( x 0 ) b_n=f(x_0) bn=f(x0) |
例如, x 0 = 2 , f ( x ) = 3 x 4 − x 2 + x + 2 x_0=2,f(x)=3x^4-x^2+x+2 x0=2,f(x)=3x4−x2+x+2
3 | 0 | -1 | 1 | 2 | |
---|---|---|---|---|---|
x 0 = 2 x_0=2 x0=2 | 6 | 12 | 22 | 46 | |
3 | 6 | 11 | 23 | 48 |
伪代码示例:
input a,b//满足f(a)f(b)<0
input 精度要求e
do{
x=(a+b)/2;
if (f(a)f(x)<0)
b=x
else
a=x
}while(b-a>e);
output x
真实代码:求一个数的三次方根
#include
#include
double n;
using namespace std;
int main() {
cin >> n;
double l, r, mid;
l = -2147483648;
r = 2147483647;
while (r - l >= 1e-7) {
mid = (l + r) / 2;
if (mid * mid * mid >= n)
r = mid;
else l = mid;
}
cout << fixed << setprecision(6) << mid;
}
迭代格式 x = ϕ ( x ) x=\phi(x) x=ϕ(x), ϕ ( x ) \phi(x) ϕ(x)在 [ a , b ] [a,b] [a,b]上具有连续一阶导数, x ∈ [ a , b ] x\in[a,b] x∈[a,b]时 ϕ ( x ) ∈ [ a , b ] \phi(x)\in[a,b] ϕ(x)∈[a,b], ∃ L < 1 \exist L<1 ∃L<1使得 ∀ x ∈ [ a , b ] , ∣ ϕ ′ ( x ) ∣ ≤ L \forall x\in[a,b],|\phi '(x)|\le L ∀x∈[a,b],∣ϕ′(x)∣≤L,则有 x ∗ = ϕ ( x ∗ ) x^*=\phi(x^*) x∗=ϕ(x∗)
将方程改写为等价形式 x = ϕ ( x ) x=\phi(x) x=ϕ(x),得到迭代格式 x k + 1 = ϕ ( x k ) x_{k+1}=\phi(x_k) xk+1=ϕ(xk),取 x 0 ∈ [ a , b ] x_0\in[a,b] x0∈[a,b],得到序列 x 0 , x 1 , . . . , x n x_0,x_1,...,x_n x0,x1,...,xn
事后估计式: ∣ x k − x ∗ ∣ ≤ L 1 − L ∣ x k − x k − 1 ∣ |x_k-x^*|\le \frac{L}{1-L}|x_k-x_{k-1}| ∣xk−x∗∣≤1−LL∣xk−xk−1∣,终止条件 ∣ x k − x k − 1 ∣ ≤ ϵ |x_k-x_{k-1}|\le \epsilon ∣xk−xk−1∣≤ϵ
事前估计式: ∣ x k − x ∗ ∣ ≤ L k 1 − L ∣ x 1 − x 0 ∣ |x_k-x^*|\le \frac{L^k}{1-L}|x_1-x_0| ∣xk−x∗∣≤1−LLk∣x1−x0∣
渐进估计式: lim k → 0 x k + 1 − x ∗ x k − x ∗ = ϕ ′ ( x ∗ ) \lim\limits_{k\to 0}\frac{x_{k+1}-x^*}{x_k-x^*}=\phi'(x^*) k→0limxk−x∗xk+1−x∗=ϕ′(x∗)
[ a , b ] [a,b] [a,b]内存在根 x ∗ x^* x∗, x ∈ [ a , b ] x\in[a,b] x∈[a,b]时 ∣ ϕ ′ ( x ) ≥ 1 ∣ |\phi'(x)\ge 1| ∣ϕ′(x)≥1∣,则发散
局部收敛:在 x ∗ x^* x∗邻域 S S S内对 ∀ x 0 ∈ S , x k + 1 = ϕ ( x k ) \forall x_0\in S,x_{k+1}=\phi(x_k) ∀x0∈S,xk+1=ϕ(xk)收敛,则局部收敛。即: ∣ ϕ ′ ( x ∗ ) ∣ < 1 |\phi'(x^*)|<1 ∣ϕ′(x∗)∣<1,局部收敛。 ∣ ϕ ′ ( x ) ∣ > 1 |\phi'(x)|>1 ∣ϕ′(x)∣>1,发散
收敛速度: e k = x k − x ∗ , lim k → ∞ e k + 1 e k p = c e_k=x_k-x^*,\lim _{k\to\infty}\frac{e_{k+1}}{e_k^p}=c ek=xk−x∗,limk→∞ekpek+1=c,则 p p p阶收敛。若 p = 1 p=1 p=1且 0 < ∣ c ∣ < 1 0<|c|<1 0<∣c∣<1,则线性收敛。若 p > 1 p>1 p>1,则超线性收敛。若 p = 2 p=2 p=2,则平方收敛。简单迭代法当 ϕ ′ ( x ∗ ) ≠ 0 \phi'(x^*)\ne 0 ϕ′(x∗)=0时线性收敛
普通的高斯消元法:输入一个包含 n n n 个方程 n n n 个未知数的线性方程组。方程组中的系数为实数。求解这个方程组。
高斯消元法分两个步骤:1.消元 2.回代。在列主元高斯消元法中,每一次消元都需要交换行,选择
∣
a
t
,
k
(
k
)
∣
=
max
k
≤
i
≤
n
∣
a
i
,
k
(
k
)
∣
|a_{t,k}^{(k)}|=\max_{k\le i\le n}|a_{i,k}^{(k)}|
∣at,k(k)∣=k≤i≤nmax∣ai,k(k)∣
伪代码描述:
for k=1 to n-1 do
for i=k+1 to n do
{
l=-a[i][k]/a[k][k];
for j = 1 to n do
a[i][j]=a[i][j]+l*a[k][j]
b[i]=b[i]+l*b[k];
}
for k = n downto 1 do
{
s=0;
for j=k+1 to n do
s=s+a[k][j]*x[j];
x[k]=(b[k]-s)/a[k][k];
}
真实代码:
#include
#include
#include
using namespace std;
const double ep=1e-6;
double a[105][105];
int n;
int main(){
cin>>n;
for(int i=0;i>a[i][j];
int c,r;
for(c=0,r=0;cfabs(a[t][c]))
t=i;
}
if (fabs(a[t][c])=c;i--)
a[r][i]/=a[r][c];
for(int i=r+1;iep)
for(int j=n;j>=c;j--)
a[i][j]-=a[r][j]*a[i][c];
r++;
}
if (rep){
puts("No solution");
return 0;
}
puts("Infinite group solutions");
return 0;
}
else{
for(int i=n-1;i>=0;i--)
for(int j=i+1;j<=n;j++)
a[i][n]-=a[j][n]*a[i][j];
for (int i = 0; i < n; i ++ )
printf("%.2lf\n", a[i][n]);
}
return 0;
}
对
f
(
x
k
)
=
y
k
f(x_k)=y_k
f(xk)=yk
L
n
(
x
)
=
∑
k
=
0
n
y
k
l
k
(
x
)
=
∑
k
=
0
n
(
∏
i
=
0
i
≠
k
x
−
x
i
x
k
−
x
i
)
y
k
L_n(x)=\sum_{k=0}^ny_kl_k(x)=\sum_{k=0}^n(\prod_{i=0i≠k}\frac{x-x_i}{x_k-x_i})y_k
Ln(x)=k=0∑nyklk(x)=k=0∑n(i=0i=k∏xk−xix−xi)yk
R n ( x ) = f ( x ) − L n ( x ) = f ( n + 1 ) ( ξ ) ( n + 1 ) ! W n + 1 ( x ) R_n(x)=f(x)-L_n(x)=\frac{f^{(n+1)}(\xi)}{(n+1)!}W_{n+1}(x) Rn(x)=f(x)−Ln(x)=(n+1)!f(n+1)(ξ)Wn+1(x)
(3)为插值多项式余项。其中,
W
n
+
1
(
x
)
=
(
x
−
x
0
)
(
x
−
x
1
)
.
.
.
(
x
−
x
n
)
W_{n+1}(x)=(x-x_0)(x-x_1)...(x-x_n)
Wn+1(x)=(x−x0)(x−x1)...(x−xn)
若
f
(
x
)
f(x)
f(x)为次数不超过
n
n
n的多项式,则
R
n
(
x
)
=
0
R_n(x)=0
Rn(x)=0.
f
(
x
)
≡
1
⇒
∑
j
=
0
n
l
j
(
x
)
≡
1
f(x)\equiv 1\Rightarrow \sum_{j=0}^nl_j(x)\equiv 1
f(x)≡1⇒j=0∑nlj(x)≡1
f
n
+
1
(
ξ
)
f^{n+1}(\xi)
fn+1(ξ)不能具体求出。但可以求出误差限:
max
a
≤
x
≤
b
∣
f
(
n
+
1
)
(
x
)
∣
=
M
n
+
1
\max_{a\le x\le b}|f^{(n+1)}(x)|=M_{n+1}
a≤x≤bmax∣f(n+1)(x)∣=Mn+1
∣ R n ( x ) ∣ ≤ M n + 1 ( n + 1 ) ! ∣ W n + 1 ( x ) ∣ |R_n(x)|\le \frac{M_{n+1}}{(n+1)!}|W_{n+1}(x)| ∣Rn(x)∣≤(n+1)!Mn+1∣Wn+1(x)∣
P
(
x
)
=
a
0
+
a
1
x
+
a
2
x
2
.
.
.
a
m
x
m
P(x)=a_0+a_1x+a_2x^2...a_mx^m
P(x)=a0+a1x+a2x2...amxm
(
∑
i
=
1
n
x
i
0
∑
i
=
1
n
x
i
1
.
.
.
∑
i
=
1
n
x
i
m
∑
i
=
1
n
x
i
1
∑
i
=
1
n
x
i
2
.
.
.
∑
i
=
1
n
x
i
m
+
1
.
.
.
.
.
.
.
.
.
.
.
.
∑
i
=
1
n
x
i
m
∑
i
=
1
n
x
i
m
+
1
.
.
.
∑
i
=
1
n
x
i
2
m
)
(
a
0
a
1
.
.
.
a
m
)
=
(
∑
i
=
1
n
y
i
∑
i
=
1
n
x
i
y
i
.
.
.
∑
i
=
1
n
x
i
m
y
i
)
(n∑i=1x0in∑i=1x1i...n∑i=1xmin∑i=1x1in∑i=1x2i...n∑i=1xm+1i............n∑i=1xmin∑i=1xm+1i...n∑i=1x2mi)(a0a1...am)=(n∑i=1yin∑i=1xiyi...n∑i=1xmiyi)
i=1∑nxi0i=1∑nxi1...i=1∑nximi=1∑nxi1i=1∑nxi2...i=1∑nxim+1............i=1∑nximi=1∑nxim+1...i=1∑nxi2m
a0a1...am
=
i=1∑nyii=1∑nxiyi...i=1∑nximyi
S n ( f ) = h 6 [ f ( x 0 ) + 2 ∑ k = 1 n − 1 f ( x k ) + f ( x n ) + 4 ∑ k = 0 n − 1 f ( x k + 1 2 ) ] S_n(f)=\frac h6[f(x_0)+2\sum_{k=1}^{n-1}f(x_k)+f(x_n)+4\sum_{k=0}^{n-1}f(x_{k+\frac 12})] Sn(f)=6h[f(x0)+2k=1∑n−1f(xk)+f(xn)+4k=0∑n−1f(xk+21)]
h = ( b − a ) / n , x 1 = a , x 2 = a + h 2 ; y 1 = 0 ; y 2 = f ( x 2 ) h=(b-a)/n,x_1=a,x_2=a+\frac h2;y_1=0;y_2=f(x_2) h=(b−a)/n,x1=a,x2=a+2h;y1=0;y2=f(x2)
伪代码:
for (i=1;i<=n-1;i++){
x1=x1+h;
x2=x2+h;
y1=y1+f(x1);
y2=y2+f(x2);
}
S=(f(a)+f(b)+2*y1+4*y2)*h/6
output S
lim h → 0 I ( f ) − I n ( f ) h p = c , h = b − a n \lim_{h\to 0}\frac{I(f)-I_n(f)}{h^p}=c,h=\frac{b-a}n h→0limhpI(f)−In(f)=c,h=nb−a
该公式 p p p阶收敛
复化梯形:2阶。复化辛卜生:4阶。复化柯特斯:6阶
复化求积公式误差: I ( f ) − I n ( f ) = c h p , I ( f ) − I 2 n ( f ) ≈ 1 2 p c h p I(f)-I_n(f)=ch^p,I(f)-I_{2n}(f)\approx\frac 1{2^p}ch^p I(f)−In(f)=chp,I(f)−I2n(f)≈2p1chp, ∣ I ( f ) − I 2 n ( f ) ∣ ≈ 1 2 p − 1 ∣ I 2 n ( f ) − I n ( f ) ∣ ≤ ϵ |I(f)-I_{2n}(f)|\approx\frac{1}{2^p-1}|I_{2n}(f)-I_n(f)|\le \epsilon ∣I(f)−I2n(f)∣≈2p−11∣I2n(f)−In(f)∣≤ϵ
变步长梯形公式:
T
n
(
f
)
=
∑
k
=
0
n
−
1
h
2
[
f
(
x
k
)
+
f
(
x
k
+
1
)
]
T
2
n
(
f
)
=
1
2
T
n
+
h
2
∑
k
=
0
n
−
1
f
(
x
k
+
1
2
)
T_n(f)=\sum_{k=0}^{n-1}\frac h2[f(x_k)+f(x_{k+1})]\\ T_{2n}(f)=\frac 12 T_n+\frac h2 \sum_{k=0}^{n-1}f(x_{k+\frac 12})
Tn(f)=k=0∑n−12h[f(xk)+f(xk+1)]T2n(f)=21Tn+2hk=0∑n−1f(xk+21)
其中,
h
h
h为二分前的步长
I
(
f
)
−
I
2
n
(
f
)
≈
1
2
p
[
(
I
(
f
)
−
I
n
(
f
)
]
<
1
I(f)-I_{2n}(f)\approx \frac 1{2^p}[(I(f)-I_n(f)]<1
I(f)−I2n(f)≈2p1[(I(f)−In(f)]<1
即:每二分一次,截断误差缩小2倍,变步长求积公式稳定。
∣
I
(
f
)
−
I
2
n
(
f
)
I
(
f
)
−
I
n
(
f
)
∣
=
1
2
p
<
1
|\frac{I(f)-I_{2n}(f)}{I(f)-I_n(f)}|=\frac 1{2^p}<1
∣I(f)−In(f)I(f)−I2n(f)∣=2p1<1
复化梯形公式:
T
n
(
f
)
=
∑
k
=
0
n
−
1
h
2
[
f
(
x
k
)
+
f
(
x
k
+
1
)
]
,
S
n
(
f
)
=
∑
k
=
0
n
−
1
h
6
[
f
(
x
k
)
+
4
f
(
x
k
+
1
2
)
+
f
(
x
k
+
1
)
]
,
C
n
(
f
)
=
∑
k
=
0
n
−
1
h
90
[
7
f
(
x
k
)
+
32
f
(
x
k
+
1
4
)
+
12
f
(
x
k
+
1
2
)
+
32
f
(
x
k
+
3
4
)
+
7
f
(
x
k
+
1
)
]
T_n(f)=\sum_{k=0}^{n-1}\frac h2[f(x_k)+f(x_{k+1})],\\S_n(f)=\sum_{k=0}^{n-1}\frac h6[f(x_k)+4f(x_{k+\frac 12})+f(x_{k+1})],\\C_n(f)=\sum_{k=0}^{n-1}\frac h{90}[7f(x_k)+32f(x_{k+\frac 14})+12f(x_{k+\frac 12})+32f(x_{k+\frac 34})+7f(x_{k+1})]
Tn(f)=k=0∑n−12h[f(xk)+f(xk+1)],Sn(f)=k=0∑n−16h[f(xk)+4f(xk+21)+f(xk+1)],Cn(f)=k=0∑n−190h[7f(xk)+32f(xk+41)+12f(xk+21)+32f(xk+43)+7f(xk+1)]
例如:计算
I
=
∫
0
1
4
1
+
x
2
d
x
I=\int_0^1\frac{4}{1+x^2}dx
I=∫011+x24dx
T
1
=
1
−
0
2
[
f
(
0
)
+
f
(
1
)
]
=
3
T
2
=
1
2
T
1
+
1
2
f
(
1
2
)
=
3.1
T
4
=
1
2
T
2
+
1
2
∗
1
2
∗
[
f
(
1
4
)
+
f
(
3
4
)
]
=
3.13118
T
8
=
1
2
T
4
+
1
2
∗
1
4
∗
[
f
(
1
8
)
+
f
(
3
8
)
+
f
(
5
8
)
+
f
(
7
8
)
]
=
3.13899...
T_1=\frac{1-0}{2}[f(0)+f(1)]=3\\T_2=\frac 12T_1+\frac 12f(\frac 12)=3.1\\T_4=\frac 12T_2+\frac 12*\frac 12 *[f(\frac 14)+f(\frac 34)]=3.13118\\ T_8=\frac 12T_4+\frac 12*\frac 14*[f(\frac 18)+f(\frac 38)+f(\frac 58)+f(\frac 78)]=3.13899...
T1=21−0[f(0)+f(1)]=3T2=21T1+21f(21)=3.1T4=21T2+21∗21∗[f(41)+f(43)]=3.13118T8=21T4+21∗41∗[f(81)+f(83)+f(85)+f(87)]=3.13899...
对于精度
ϵ
\epsilon
ϵ,
1
2
p
−
1
∣
I
2
n
(
f
)
−
I
n
(
f
)
∣
<
ϵ
\frac 1{2^p-1}|I_{2n}(f)-I_n(f)|<\epsilon
2p−11∣I2n(f)−In(f)∣<ϵ时,
I
(
f
)
−
I
2
n
(
f
)
≈
1
2
p
−
1
∣
I
2
n
(
f
)
−
I
n
(
f
)
∣
≤
ϵ
I(f)-I_{2n}(f)\approx \frac 1{2^p-1}|I_{2n}(f)-I_n(f)|\le \epsilon
I(f)−I2n(f)≈2p−11∣I2n(f)−In(f)∣≤ϵ
D ( h ) = f ( x 0 + h ) − f ( x 0 − h ) 2 h f ′ ( x ) = 1 2 h [ − f ( x 0 ) + f ( x 2 ) − h 2 6 f ′ ′ ′ ( ξ 2 ) ] D 1 ( h ) = 4 3 D ( h 2 ) − 1 3 D ( h ) D(h)=\frac{f(x_0+h)-f(x_0-h)}{2h}\\f'(x)=\frac 1{2h}[-f(x_0)+f(x_2)-\frac{h^2}6f'''(\xi _2)]\\ D_1(h)=\frac 43D(\frac h2)-\frac 13D(h) D(h)=2hf(x0+h)−f(x0−h)f′(x)=2h1[−f(x0)+f(x2)−6h2f′′′(ξ2)]D1(h)=34D(2h)−31D(h)
单步隐式公式
{
y
i
+
1
=
y
i
+
h
ϕ
(
x
i
,
y
i
,
y
i
+
1
,
h
)
y
0
=
η
{yi+1=yi+hϕ(xi,yi,yi+1,h)y0=η
{yi+1=yi+hϕ(xi,yi,yi+1,h)y0=η
ϕ ( x , y , y ‾ , h ) = 1 2 [ f ( x , y ) + f ( x + h , y ~ ) ] y i + 1 = y i + h 2 [ f ( x i , y i ) + f ( x i + 1 , y i + 1 ) ] y ′ = f ( x , y ) , y ( a ) = η \phi(x,y,\overline y,h)=\frac 12[f(x,y)+f(x+h,\tilde y)]\\ y_{i+1}=y_i+\frac h2[f(x_i,y_i)+f(x_{i+1},y_{i+1})]\\ y'=f(x,y),y(a)=\eta ϕ(x,y,y,h)=21[f(x,y)+f(x+h,y~)]yi+1=yi+2h[f(xi,yi)+f(xi+1,yi+1)]y′=f(x,y),y(a)=η
例如
y
′
=
−
2
x
y
(
0
≤
x
≤
1.8
)
,
y
(
0
)
=
1
,
h
=
0.1
y
i
+
1
=
y
i
+
h
2
[
f
(
x
i
,
y
i
)
+
f
(
x
i
+
1
,
y
i
+
1
)
]
y
i
+
1
=
y
i
−
h
x
i
y
i
−
h
x
i
+
1
y
i
+
1
,
其中
y
i
+
1
=
y
i
−
h
x
i
y
i
1
+
h
x
i
+
1
y'=-2xy(0\le x \le 1.8),y(0)=1,h=0.1\\ y_{i+1}=y_i+\frac h2[f(x_i,y_i)+f(x_{i+1},y_{i+1})]\\ y_{i+1}=y_i-hx_iy_i-hx_{i+1}y_{i+1},\\其中y_{i+1}=\frac{y_i-hx_iy_i}{1+hx_{i+1}}
y′=−2xy(0≤x≤1.8),y(0)=1,h=0.1yi+1=yi+2h[f(xi,yi)+f(xi+1,yi+1)]yi+1=yi−hxiyi−hxi+1yi+1,其中yi+1=1+hxi+1yi−hxiyi
x_i | y_i |
---|---|
0 | 1 |
0.1 | 0.9901 |
0.2 | 0.96098 |
0.3 | 0.9143 |
0.4 | 0.8527 |