Skip to content

Instantly share code, notes, and snippets.

@yudhastyawan
Last active May 1, 2021 21:08
Show Gist options
  • Save yudhastyawan/aa7be20d7792397c77626a7e660c1590 to your computer and use it in GitHub Desktop.
Save yudhastyawan/aa7be20d7792397c77626a7e660c1590 to your computer and use it in GitHub Desktop.
Jupyter notebooks and labs
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "9ff0ec85-58cb-40c1-af87-7a096637d28b",
"metadata": {},
"source": [
"# 4.\tPerforming interpolation and derivative using the variant length of the grid points\n",
"\n",
"In this part, it performs the behavior of interpolation and derivative results with the variation of grid length. Order 0 means the interpolation and the otherwise is derivative. Figure 22 shows the code that being used to obtain the results and Figures 23-25 are the results of 3, 4, and 5 length of the grid, respectively. Comparing figs. 23, 24, and 25, they show that the higher length of data will give the better accuracy of the result such as in data length of 3 gives low accuracy of the results that might be caused of being not optimally clear for y = x^3 in this length whereas in data length of 4 and 5 give high accuracy of the results."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "87749da7-dcd7-48e1-9cbd-e8c1c6f68256",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"data: length = 3\n",
"x = [1. 2. 3.]\n",
"y = [ 1. 8. 27.]\n",
"seek y, y', and y'' for x = 1.5\n",
"y\t= x^3\n",
"y'\t= 3x^2\n",
"y''\t= 6x\n",
"order = 0\n",
"\tweights = [ 0.375 0.75 -0.125]\n",
"\ty_exact = 3.375 and y_by_finite_diff = 3.0\n",
"order = 1\n",
"\tweights = [-1. 1. 0.]\n",
"\ty_exact = 6.75 and y_by_finite_diff = 7.0\n",
"order = 2\n",
"\tweights = [ 1. -2. 1.]\n",
"\ty_exact = 9.0 and y_by_finite_diff = 12.0\n",
"________________________________________________________________________________ \n",
"\n",
"data: length = 4\n",
"x = [1. 2. 3. 4.]\n",
"y = [ 1. 8. 27. 64.]\n",
"seek y, y', and y'' for x = 1.5\n",
"y\t= x^3\n",
"y'\t= 3x^2\n",
"y''\t= 6x\n",
"order = 0\n",
"\tweights = [ 0.3125 0.9375 -0.3125 0.0625]\n",
"\ty_exact = 3.375 and y_by_finite_diff = 3.375\n",
"order = 1\n",
"\tweights = [-0.95833333 0.875 0.125 -0.04166667]\n",
"\ty_exact = 6.75 and y_by_finite_diff = 6.750000000000002\n",
"order = 2\n",
"\tweights = [ 1.5 -3.5 2.5 -0.5]\n",
"\ty_exact = 9.0 and y_by_finite_diff = 9.0\n",
"________________________________________________________________________________ \n",
"\n",
"data: length = 5\n",
"x = [1. 2. 3. 4. 5.]\n",
"y = [ 1. 8. 27. 64. 125.]\n",
"seek y, y', and y'' for x = 1.5\n",
"y\t= x^3\n",
"y'\t= 3x^2\n",
"y''\t= 6x\n",
"order = 0\n",
"\tweights = [ 0.2734375 1.09375 -0.546875 0.21875 -0.0390625]\n",
"\ty_exact = 3.375 and y_by_finite_diff = 3.375\n",
"order = 1\n",
"\tweights = [-0.91666667 0.70833333 0.375 -0.20833333 0.04166667]\n",
"\ty_exact = 6.75 and y_by_finite_diff = 6.750000000000001\n",
"order = 2\n",
"\tweights = [ 1.79166667 -4.66666667 4.25 -1.66666667 0.29166667]\n",
"\ty_exact = 9.0 and y_by_finite_diff = 9.0\n",
"________________________________________________________________________________ \n",
"\n"
]
}
],
"source": [
"from finitediff import get_weights\n",
"import numpy as np\n",
"\n",
"# data\n",
"xint = np.array([1.,2.,3.,4.,5.,6.])\n",
"for j in range(3):\n",
" x = xint[:(-3+j)]\n",
" y = np.power(x,3) # y = x^3\n",
" print('data: length = {}'.format(len(x)))\n",
" print('x = {}'.format(x))\n",
" print('y = {}'.format(y))\n",
" # seek y, y', and y'' for x = 1.5\n",
" # y' = 3x^2\n",
" # y'' = 6x\n",
" x0 = 1.5\n",
" print(\"seek y, y', and y'' for x = {}\".format(x0))\n",
" print(\"y\\t= x^3\\ny'\\t= 3x^2\\ny''\\t= 6x\")\n",
" c = get_weights(x, x0, maxorder=2)\n",
" for i in range(len(c[0,:])):\n",
" yexact = 0\n",
" if i == 0:\n",
" yexact = x0**3\n",
" elif i == 1:\n",
" yexact = 3*x0**2\n",
" elif i == 2:\n",
" yexact = 6*x0\n",
" print('order = ',i)\n",
" print('\\tweights = {}'.format(c[:,i]))\n",
" yest = np.sum(y*c[:,i])\n",
" print('\\ty_exact = {} and y_by_finite_diff = {}'.format(yexact, yest))\n",
" print(\"\".join(['_' for l in range(80)]),'\\n')"
]
},
{
"cell_type": "markdown",
"id": "2226b7eb-1b57-4b0d-9891-667662170f33",
"metadata": {},
"source": [
"Figure 22. The code for testing interpolation (order = 0) and derivative (order != 0) by using finite difference\n",
"\n",
"Figure 23. The result by using the length of 3 points\n",
"\n",
"Figure 24. The result by using the length of 4 points\n",
"\n",
"Figure 25. The result by using the length of 5 points"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a064bea9-c892-40ce-8616-58a6be6937c2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment