{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**This is an example of finite differences**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider an equally space grid $x_j=x_0+hj$ where $h>0$."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x0=-1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h=0.3"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"J=12"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12-element Array{Int64,1}:\n",
" 1\n",
" 2\n",
" 3\n",
" 4\n",
" 5\n",
" 6\n",
" 7\n",
" 8\n",
" 9\n",
" 10\n",
" 11\n",
" 12"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"js=[1:J;]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12-element Array{Float64,1}:\n",
" -0.7\n",
" -0.4\n",
" -0.10000000000000009\n",
" 0.19999999999999996\n",
" 0.5\n",
" 0.7999999999999998\n",
" 1.1\n",
" 1.4\n",
" 1.6999999999999997\n",
" 2.0\n",
" 2.3\n",
" 2.5999999999999996"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xs=x0.+h*js"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define $f_j=f(x_j)$ where\n",
"$$\n",
" f(x)={1\\over 1+x^2}.\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"f (generic function with 1 method)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x)=1/(1+x^2)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12-element Array{Float64,1}:\n",
" 0.6711409395973155\n",
" 0.8620689655172413\n",
" 0.9900990099009901\n",
" 0.9615384615384615\n",
" 0.8\n",
" 0.6097560975609757\n",
" 0.45248868778280543\n",
" 0.33783783783783783\n",
" 0.25706940874035994\n",
" 0.2\n",
" 0.15898251192368842\n",
" 0.12886597938144334"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fs=f.(xs)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"using Plots"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scatter(xs,fs,size=[400,200])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a table of finite differences such that\n",
"$$\n",
" T_{j,k+1}=\\Delta^k f_j\n",
"$$\n",
"for $k=0,\\ldots,n$ and $j=1,\\ldots,J-k$."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n=5"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"T=zeros(J,n+1);"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"T[:,1]=fs;"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12×6 Array{Float64,2}:\n",
" 0.671141 0.0 0.0 0.0 0.0 0.0\n",
" 0.862069 0.0 0.0 0.0 0.0 0.0\n",
" 0.990099 0.0 0.0 0.0 0.0 0.0\n",
" 0.961538 0.0 0.0 0.0 0.0 0.0\n",
" 0.8 0.0 0.0 0.0 0.0 0.0\n",
" 0.609756 0.0 0.0 0.0 0.0 0.0\n",
" 0.452489 0.0 0.0 0.0 0.0 0.0\n",
" 0.337838 0.0 0.0 0.0 0.0 0.0\n",
" 0.257069 0.0 0.0 0.0 0.0 0.0\n",
" 0.2 0.0 0.0 0.0 0.0 0.0\n",
" 0.158983 0.0 0.0 0.0 0.0 0.0\n",
" 0.128866 0.0 0.0 0.0 0.0 0.0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"T"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"for k=1:n\n",
" for j=1:J-k\n",
" T[j,k+1]=T[j+1,k]-T[j,k]\n",
" end\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Newton's forward difference formula for the interpolating polynomial is\n",
"$$\n",
" f(x_j+\\theta h)\\approx\\sum_{k=0}^n {\\theta\\choose k}\\Delta^k f_j\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"fp (generic function with 1 method)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function fp(j,theta)\n",
" global n\n",
" r=0.0\n",
" b=1.0\n",
" for k=0:n\n",
"# println(\"k=$k\")\n",
" r=r+b*T[j,k+1]\n",
" b=b*(theta-k)/(k+1)\n",
" end\n",
" return r\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7577457729408739"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fp(2,-1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6711409395973155"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fs[1]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"p (generic function with 1 method)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p(x)=fp(1,(x-xs[1])/h)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"xvals=[xs[1]:0.1:xs[7];];"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"yvals=p.(xvals);"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot!(xvals,yvals)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.5.1",
"language": "julia",
"name": "julia-1.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}