{ "cells": [ { "cell_type": "markdown", "id": "febd0a9d-bea2-4bcb-974d-86e5a31fb52a", "metadata": {}, "source": [ "# Legendre polynomial" ] }, { "cell_type": "markdown", "id": "14fa18ac-a4f2-4e75-b54a-4a28f05abc66", "metadata": {}, "source": [ "**Legendre polynomials** is a particular kind of *special\n", "functions* that are important in physics and applied mathematics.\n", " \n", "Legendre polynomials are defined as follows:\n", "\n", "$$P_n(x)= \\frac{1}{2^n \\, n!} \\, \\frac{\\mathrm{d}^n}{\\mathrm{d}x^n} \\, \\left(x^2 - 1\\right)^n, \n", "\\qquad n = 0, 1, \\ldots .$$\n", "\n", "The first few Legendre polynomials are as follows:\n", " \n", "$$P_{0}(x) = 1, \\quad P_{1}(x) = x , \\quad P_2(x) = \\frac{1}{2}\\left(3x^2 - 1\\right),\n", " \\quad P_3(x) = \\frac{1}{2}\\left(5x^3 - 3x\\right) .$$\n", "\n", "The polynomials are named after the mathematician Adrien-Marie Legendre.\n", "\n", "Legendre polynomials $P_n(x)$ satisfy the following recurrence relations:\n", "\n", "$$(n + 1) \\, P_{n+1}(x) = (2n + 1) \\, x \\, P_{n}(x) - n \\, P_{n-1}(x), \\quad n = 1, 2, \\ldots$$" ] }, { "cell_type": "code", "execution_count": null, "id": "52056ccb", "metadata": {}, "outputs": [], "source": [ "\n", "using PyPlot" ] }, { "cell_type": "code", "execution_count": null, "id": "b32a3c0e", "metadata": {}, "outputs": [], "source": [ "\n", "\"\"\"\n", " mylegendre(n, x)\n", "\n", "Calculate Legendre polynomial P_n(x) using the recurrence relation \n", "(n+1)P_{n+1}(x) = (2*n+1)*x*P_n(x) - n*P_{n-1}(x), and P_0(x) = 1, P_1(x) = x\n", "\"\"\"\n", "function mylegendre(n, x)\n", "\n", " # P_0\n", " pp = 1.0\n", " if n == 0\n", " return pp\n", " end\n", "\n", " # P_1\n", " pc = x\n", " if n == 1\n", " return pc\n", " end\n", "\n", " # Iteration loop\n", " for i = 1:(n-1)\n", " pn = ((2*i + 1)*x*pc - i*pp)/(i + 1)\n", " # Prepare for the next iteration\n", " pp = pc\n", " pc = pn\n", " # pc, pp = ((2*i + 1)*x*pc - i*pp)/(i + 1), pc # shorter version\n", " end\n", " \n", " return pc\n", " \n", "end" ] }, { "cell_type": "markdown", "id": "31072c78-ae72-4547-9ae0-31d8587914f5", "metadata": {}, "source": [ "### Test code sanity" ] }, { "cell_type": "code", "execution_count": null, "id": "d1f1ee0f-0e80-47df-843a-8866830f02b8", "metadata": {}, "outputs": [], "source": [ "\n", "@show mylegendre(3, 0.0); # must be 0\n", "@show mylegendre(3, -1.0); # must be -1" ] }, { "cell_type": "markdown", "id": "b7def47f-78c0-4386-97cc-cb6bf62b0e25", "metadata": {}, "source": [ "### Measure code performance" ] }, { "cell_type": "code", "execution_count": null, "id": "1281caf3-1646-425f-a362-0b376b50ee71", "metadata": {}, "outputs": [], "source": [ "\n", "@time mylegendre(1, 1.0)" ] }, { "cell_type": "code", "execution_count": null, "id": "0965db0a-ca2a-4aba-9ada-c3d3c98d34e4", "metadata": {}, "outputs": [], "source": [ "@time mylegendre(51, -1.0)" ] }, { "cell_type": "markdown", "id": "f4aedd8f-0b2c-479a-841c-a7ae3c8803dc", "metadata": {}, "source": [ "### Plot polynomials graphs" ] }, { "cell_type": "code", "execution_count": null, "id": "16b63744-e4ae-4d96-995a-d8d6b410af80", "metadata": {}, "outputs": [], "source": [ "\n", "np = 100\n", "x = range(-1.0, 1.0, np);" ] }, { "cell_type": "code", "execution_count": null, "id": "ec0e816b", "metadata": {}, "outputs": [], "source": [ "\n", "for n in [2, 3, 5]\n", " y = mylegendre.(n, x);\n", " plot(x, y, label=L\"P_{%$n}(x)\")\n", "end\n", "\n", "grid(true)\n", "legend()\n", "title(\"Legendre polynomials\")\n", "xlabel(\"x\")\n", "ylabel(\"y\");" ] }, { "cell_type": "code", "execution_count": null, "id": "dfe33294", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.11.6", "language": "julia", "name": "julia-1.11" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }