Co 4
Co 4
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: Shashwath Jain\n",
"USN: 1RVU23CSE426\n",
"Section: F\n",
"\n",
"\n",
"Iteration\ta\t\tb\t\tmidpoint\tf(midpoint)\n",
"1\t\t2.000000\t3.000000\t2.500000\t-3.375000\n",
"2\t\t2.500000\t3.000000\t2.750000\t0.796875\n",
"3\t\t2.500000\t2.750000\t2.625000\t-1.412109\n",
"4\t\t2.625000\t2.750000\t2.687500\t-0.339111\n",
"5\t\t2.687500\t2.750000\t2.718750\t0.220917\n",
"6\t\t2.687500\t2.718750\t2.703125\t-0.061077\n",
"7\t\t2.703125\t2.718750\t2.710938\t0.079423\n",
"8\t\t2.703125\t2.710938\t2.707031\t0.009049\n",
"9\t\t2.703125\t2.707031\t2.705078\t-0.026045\n",
"10\t\t2.705078\t2.707031\t2.706055\t-0.008506\n",
"\n",
"Approximate root after 10 iterations: 2.7060546875\n"
]
}
],
"source": [
"#Question 1\n",
"\n",
"def f(x):\n",
" return x**3 - 4*x - 9\n",
"\n",
"# Bisection Method\n",
"def bisection_method(a, b, iterations):\n",
" if f(a) * f(b) >= 0:\n",
" print(\"Bisection method fails: The function must have opposite signs at a and b.\")\n",
" return None\n",
" \n",
" print(\"Iteration\\ta\\t\\tb\\t\\tmidpoint\\tf(midpoint)\")\n",
" for i in range(iterations):\n",
" midpoint = (a + b) / 2\n",
" f_midpoint = f(midpoint)\n",
" print(f\"{i+1}\\t\\t{a:.6f}\\t{b:.6f}\\t{midpoint:.6f}\\t{f_midpoint:.6f}\")\n",
" \n",
" # Check if the midpoint is a root\n",
" if f_midpoint == 0:\n",
" print(\"Exact root found.\")\n",
" return midpoint\n",
" \n",
" # Decide the new interval\n",
" if f(a) * f_midpoint < 0:\n",
" b = midpoint\n",
" else:\n",
" a = midpoint\n",
"\n",
" # After 10 iterations\n",
" return midpoint\n",
"\n",
"a = 2\n",
"b = 3\n",
"iterations = 10\n",
"print(\"Name: Shashwath Jain\")\n",
"print(\"USN: 1RVU23CSE426\")\n",
"print(\"Section: F\\n\\n\")\n",
"root = bisection_method(a, b, iterations)\n",
"print(f\"\\nApproximate root after {iterations} iterations: {root}\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"NAME: Shashwath Jain\n",
"USN: 1RVU23CSE426\n",
"Section: F\n",
"\n",
"Root of the equation f(x) = x^3 - 4x + 1: 1.8608058531124423\n"
]
}
],
"source": [
"#Question 2\n",
"\n",
"from sympy import symbols, diff\n",
"from sympy import lambdify\n",
"\n",
"print(\"NAME: Shashwath Jain\")\n",
"print(\"USN: 1RVU23CSE426\")\n",
"print(\"Section: F\")\n",
"\n",
"x = symbols('x')\n",
"f = x**3 - 4*x + 1\n",
"f_prime = diff(f, x)\n",
"\n",
"\n",
"f_func = lambdify(x, f, 'numpy')\n",
"f_prime_func = lambdify(x, f_prime, 'numpy')\n",
"\n",
"\n",
"def newton_raphson(x0, tol=1e-6, max_iter=100):\n",
" for i in range(max_iter):\n",
" fx = f_func(x0)\n",
" fpx = f_prime_func(x0)\n",
" \n",
" if fpx == 0:\n",
" print(\"Derivative is zero. No solution found.\")\n",
" return None\n",
" \n",
" x1 = x0 - fx / fpx\n",
" \n",
"\n",
" if abs(x1 - x0) < tol:\n",
" return x1\n",
" \n",
" x0 = x1\n",
" \n",
" print(\"Did not converge within the maximum number of iterations.\")\n",
" return None\n",
"\n",
"\n",
"initial_guess = 1.5\n",
"root = newton_raphson(initial_guess)\n",
"print(\"\\nRoot of the equation f(x) = x^3 - 4x + 1:\", root)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"NAME: Shashwath Jain\n",
"USN: 1RVU23CSE426\n",
"Section: F\n",
"\n",
"Approximate integral of f(x) over [1,5] with 10 subdivisions: 69.44\n",
"\n",
"Approximate integral of f(x) over [1,5] with 20 subdivisions: 69.36\n",
"\n",
"Approximate integral of f(x) over [1,5] with 50 subdivisions: 69.33760000000001\n"
]
}
],
"source": [
"#Question 3\n",
"\n",
"import numpy as np\n",
"\n",
"\n",
"print(\"NAME: Shashwath Jain\")\n",
"print(\"USN: 1RVU23CSE426\")\n",
"print(\"Section: F\")\n",
"\n",
"\n",
"def f(x):\n",
" return x**2 + 2*x + 1\n",
"\n",
"\n",
"def trapezoidal_rule(a, b, n):\n",
" h = (b - a) / n\n",
" integral = (f(a) + f(b)) / 2\n",
" for i in range(1, n):\n",
" integral += f(a + i * h)\n",
" integral *= h\n",
" return integral\n",
"\n",
"\n",
"a, b = 1, 5\n",
"subdivisions = [10, 20, 50]\n",
"\n",
"for n in subdivisions:\n",
" result = trapezoidal_rule(a, b, n)\n",
" print(f\"\\nApproximate integral of f(x) over [{a},{b}] with {n} subdivisions:\", result)\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"NAME: Shashwath Jain\n",
"USN: 1RVU23CSE426\n",
"Section: F\n",
"\n",
"Approximate integral of f(x) over [1,5] with 10 subdivisions: 69.44\n",
"\n",
"Approximate integral of f(x) over [1,5] with 20 subdivisions: 69.36\n",
"\n",
"Approximate integral of f(x) over [1,5] with 50 subdivisions: 69.33760000000001\n"
]
}
],
"source": [
"#Question 4\n",
"\n",
"import numpy as np\n",
"\n",
"\n",
"print(\"NAME: Shashwath Jain\")\n",
"print(\"USN: 1RVU23CSE426\")\n",
"print(\"Section: F\")\n",
"\n",
"\n",
"def f(x):\n",
" return x**2 + 2*x + 1\n",
"\n",
"\n",
"def trapezoidal_rule(a, b, n):\n",
" h = (b - a) / n\n",
" integral = (f(a) + f(b)) / 2\n",
" for i in range(1, n):\n",
" integral += f(a + i * h)\n",
" integral *= h\n",
" return integral\n",
"\n",
"\n",
"a, b = 1, 5\n",
"subdivisions = [10, 20, 50]\n",
"\n",
"for n in subdivisions:\n",
" result = trapezoidal_rule(a, b, n)\n",
" print(f\"\\nApproximate integral of f(x) over [{a},{b}] with {n} subdivisions:\", result)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}