【代码】Numerical_Analysis

基于 UIC 金融数学的课程 Numerical Analysis 中代码的复习笔记;

The Bisection Method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function sol = Bisection(a,b,tol,n0)
%input:
% interval [a,b]
% function fx
% tolerance tol
% maximum number of iterations n0
i = 1;
fa = fx(a);
if fx(a)*fx(b) > 0
fprintf("Please change the interval [a,b].\n")
else
while i <= n0
p = a+(b-a)/2;
fp = fx(p);
if fp == 0 || (b-a)/2 < tol
sol = p;
break
else
i = i+1;
fprintf("a:"+a+" b:"+b+" p:"+p+"\n")
if fa*fp > 0
a=p;
fa=fp;
else
b=p;
end
end
end
end

Fixed-point Iteration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function sol =  FixedPoint(p0,tol,n0)
%input:
% initial approximation p0
% function fx
% tolerance tol
% maximum number of iterations n0
i = 1;
while i <= n0
fprintf("p"+(i-1)+":"+p0+"\n")
p = fx(p0);
fprintf("|p-p0|:"+abs(p-p0)+"\n")
if abs(p-p0) < tol
sol = p;
break
else
i = i+1;
p0 = p;
end
end

Newton’s Method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function sol =  Newton(p0,tol,n0)
%input:
% initial approximation p0
% function fx
% tolerance tol
% maximum number of iterations n0
fx = @(x) x-0.8-0.2*sin(x);
dfx = @(x) 1-0.2*cos(x);
i=1;
while i <= n0
fprintf("p"+(i-1)+":"+p0+"\n")
p = p0-fx(p0)/dfx(p0);
fprintf("|p-p0|:"+abs(p-p0)+"\n")
if abs(p-p0) < tol
sol = p;
break
else
i = i+1;
p0 = p;
end
end

nth Lagrange Interpolation Polynomials

如果我们已知 n+1 个点的值

存在一个多项式

通过所有点

Code of Degree one

1
2
3
4
5
6
7
8
function sol = Degree1_Interpolation(x0,x1,X)
%input:
% distinct nodes x0,x1
% function fx
L0 = @(x) (x-x1)/(x0-x1);
L1 = @(x) (x-x0)/(x1-x0);
sol = L0(X)*fx(x0)+L1(X)*fx(x1);
end

Code of Degree two

1
2
3
4
5
6
7
8
9
function sol = Degree2_Interpolation(x0,x1,x2,X)
%input:
% distinct nodes x0,x1,x2
% function fx
L0 = @(x) (x-x1)*(x-x2)/((x0-x1)*(x0-x2));
L1 = @(x) (x-x0)*(x-x2)/((x1-x0)*(x1-x2));
L2 = @(x) (x-x0)*(x-x1)/((x2-x0)*(x2-x1));
sol = L0(X)*fx(x0)+L1(X)*fx(x1)+L2(X)*fx(x2);
end

注意:预测的点应该在已知点的范围之间

Neville’s Iterated Interpolation

如果一个多项式建立在 k 个确定的点上 $x_{m_1},x_{m_2},…,x_{m_k}$。那么,此多项式可以表示为 $P_{m_1,m_2,…,m_k}$,$Q_{i,j}=P_{i-j,i-j+1,…,i-1,i}$

1
2
3
4
5
6
7
8
function sol = Qfunction(x0,x1,y0,y1,X)
%input:
% distinct nodes (x0,y0),(x1,y1)
% function fx
L0 = @(x) (x-x1)/(x0-x1);
L1 = @(x) (x-x0)/(x1-x0);
sol = L0(X)*y0+L1(X)*y1;
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function sol = NevilleIterated(x,x_box,y_box)
Q = zeros(3,3);
for i = 1:3
Q(i,1) = Qfunction(x_box(i),x_box(i+1),y_box(i),y_box(i+1),x);
end
%[ p12 , p123 , p1234
%
% p23 , p234
%
% p34 ,
for j = 2:3
for i = 1:4-j
Q(i,j) = ((x-x_box(i))*Q(i+1,j-1)-(x-x_box(i+j))*Q(i,j-1))/(x_box(i+j)-x_box(i));
end
end
sol = Q(1,3);
end

Numerical Differentiation

  • The (n+1) -point formula to approximate $f^{\prime}\left(x_{j}\right)$
  • Forward-Difference and Backward-Difference Formula

where $ \xi(x)$ lies between $x_{0}$ and $x_{0}+h$ . When $h>0$ , it is the forward-difference formula; The backward-difference formula if $h<0$ .

  • Three-Point Endpoint Formula

where $\xi_{0}$ lies between $x_{0}$ and $x_{0}+2 h$ .

  • Three-Point Midpoint Formula

where $\xi_{1}$ lies between $x_{0}-h$ and $x_{0}+h$ .

  • Five-Point Endpoint Formula

where $\xi_{0}$ lies between $x_{0}$ and $x_{0}+4 h$ .

  • Five-Point Midpoint Formula

where $\xi_{1}$ lies between $x_{0}-2 h$ and $x_{0}+2 h$ .

  • Second Derivative Midpoint Formula (To approximate $f^{\prime \prime}\left(x_{0}\right)$)

where $\xi$ lies between $x_{0}-h$ and $x_{0}+h$ .

Numerical Integration

The $(n+1)$ -point Closed Newton-Cotes Formulas:

Use $x_{i}=x_{0}+i h$ , for $i= 0,1, \cdots, n$ , where $x_{0}=a, x_{n}=b$ and $h=(b-a) / n$ . The endpoints of the closed interval [a, b] are included as nodes.

where $a_{i}=\int_{a}^{b} L_{i}(x) \mathrm{d} x$ .

  • Trapezoidal Rule (n=1)
  • Simpson’s Rule (n=2)
  • Simpson’s Three-Eights Rule (n=3)

The $(n+1)$-point Open Newton-Cotes Formulas:

Use $x_{i}=x_{0}+i h$, for $i=$ $0,1, \cdots, n$, where $h=(b-a) /(n+2), x_{0}=a+h$ and $x_{n}=b-h$. Label the endpoints as $x_{-1}=a$ and $x_{n+1}=b$. Open formulas contain all the nodes within the open interval $(a, b)$.

where $a_{i}=\int_{a}^{b} L_{i}(x) \mathrm{d} x$.

  • Midpoint Rule $(n=0)$

Composite Numerical Integration

  • Composite Simpsons rule: Let $f \in C^{4}[a, b], n$ be even, $h=(b-a) / n$, and $x_{j}=a+j h$, for each $j=0,1, \cdots, n$. There exists a $\mu \in(a, b)$ for which the Composite Simpsons rule for $n$ subintervals can be written with its error term as
  • Composite Trapezoidal rule: Let $f \in C^{2}[a, b], h=(b-a) / n$, and $x_{j}=a+j h$, for each $j=0,1, \cdots, n$. There exists a $\mu \in(a, b)$ for which the Composite Trapezoidal rule for $n$ subintervals can be written with its error term as
  • Composite Midpoint rule: Let $f \in C^{2}[a, b], n$ be even, $h=(b-a) /(n+2)$, and $x_{j}=a+(j+1) h$, for each $j=-1,0, \cdots, n+1$. There exists a $\mu \in(a, b)$ for which the Composite Midpoint rule for $n+2$ subintervals can be written with its error term as
1
2
3
4
5
6
7
8
function sol = ComTrapezoidal(a,b,n)
h = (b-a)/n;
sum = 0;
for i = 1:(n-1)
sum = sum+fx(a+i*h);
end
sol = h/2*(fx(a)+fx(b)+2*sum);
end
1
2
3
4
5
6
7
8
9
10
function sol = ComSimpsons(a,b,n)
h = (b-a)/n;
sum1 = 0;
sum2 = dfx(a+(n-1)*h);
for i = 1:(n/2-1)
sum1 = sum1+dfx(a+2*i*h);
sum2 = sum2+dfx(a+(2*i-1)*h);
end
sol = h/3*(dfx(a)+dfx(b)+2*sum1+4*sum2);
end

Initial Value Problems for ODEs

Approximate the solution $y(t)$ to an initial-value problem

Euler’s Method

  • Forward Euler’s Method:
  • Backward Euler’s Method:

Higher-Order Taylor Methods

  • Taylor method of order $n$ :where
  • Forward Euler’s Method is Taylor’s method of order one.

Runge-Kutta Method

  • Runge-Kutta methods of order two (RK2):

    • Midpoint method: with local truncation error $O\left(h^{2}\right)$

      for each $i=0,1, \cdots, N-1$.

    • Modified Euler method: with local truncation error $O\left(h^{2}\right)$

      for each $i=0,1, \cdots, N-1$.

  • Runge-Kutta methods of order four (RK4):

    for each $i=0,1, \cdots, N-1$.


【代码】Numerical_Analysis
http://achlier.github.io/2022/03/13/Numerical_Analysis/
Author
Hailey
Posted on
March 13, 2022
Licensed under