Pertemuan2 Ipynb
Pertemuan2 Ipynb
"cells": [
{
"cell_type": "markdown",
"id": "32257b94-6932-46cd-b955-4eed195b11fe",
"metadata": {},
"source": [
"# Modified Newton-Raphson (Polynomial Problems)\n",
"\n",
"$$x_{i+1}=x_i-\\frac{f(x_1)f'(x_i)}{[f'(x_i)]^2-f(x_i)f''(x_i)}$$"
]
},
{
"cell_type": "markdown",
"id": "73d42349-2060-429a-91eb-325b1929cb3d",
"metadata": {},
"source": [
"**Contoh Soal**\n",
"\n",
"Diketahui suatu fungsi $f(x)=x^3-7x+6$. Dengan mengunakan metode NR, Secant,
dan NR termodifikasi. Temukan solusi dari $f(x)=0$!"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "849ee520-2886-48b3-a072-292bb704983e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"xi= 1.0000000000000002 iterasi ke 5\n",
"xi= 0.9999989844460864 iterasi ke 9\n",
"xi= 1.0 iterasi ke 5\n"
]
}
],
"source": [
"# Definisi fungsi\n",
"def f(x):\n",
" return x**3-7*x+6\n",
"def ff(x):\n",
" return 3*x**2-7\n",
"def fff(x):\n",
" return 6*x\n",
"\n",
"# Metode NR\n",
"x,tol,er,n=0,.00001,1,0\n",
"while er>tol:\n",
" xi=x-f(x)/ff(x)\n",
" n+=1\n",
" er=abs((xi-x)/xi)\n",
" x=xi\n",
"print('xi=',xi,' iterasi ke ',n)\n",
"\n",
"# Metode Secant\n",
"x,tol,er,n=0,.00001,1,0\n",
"x0=x+.5\n",
"while er>tol:\n",
" xi=x-(f(x)*(x0-x))/(f(x0)-f(x))\n",
" n+=1\n",
" er=abs((xi-x)/xi)\n",
" x=xi\n",
"print('xi=',xi,' iterasi ke ',n)\n",
"\n",
"# Metode NR termodifikasi\n",
"x,tol,er,n=0,.00001,1,0\n",
"while er>tol:\n",
" xi=x-(f(x)*ff(x))/(ff(x)**2-f(x)*fff(x))\n",
" n+=1\n",
" er=abs((xi-x)/xi)\n",
" x=xi\n",
"print('xi=',xi,' iterasi ke ',n)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "651ea49c-48f8-4bbf-a733-b740dbdf4817",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 5. 4. -3. 2. -1.]\n"
]
}
],
"source": [
"from numpy import array,polyval,roots\n",
"\n",
"a=array([1,-7,-3,79,-46,-120])\n",
"polyval(a,6)\n",
"print(roots(a))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2c20174d-5092-4969-8d26-d9262d63cde6",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{Poly}{\\left( x^{5} - 7 x^{4} - 3 x^{3} + 79
x^{2} - 46 x - 120, x, domain=\\mathbb{Z} \\right)}$"
],
"text/plain": [
"Poly(x**5 - 7*x**4 - 3*x**3 + 79*x**2 - 46*x - 120, x, domain='ZZ')"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sympy as sy\n",
"x=sy.symbols('x')\n",
"f=(x+1)*(x-4)*(x-5)*(x+3)*(x-2)\n",
"sy.poly(f)\n",
"# f.subs({x:6})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de352001-0a87-4bea-84dd-b1aac9bc1c4e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}