{ "cells": [ { "cell_type": "markdown", "id": "7cff2fc3-de18-46c0-8f17-0614c7b681fb", "metadata": {}, "source": [ "## For loop" ] }, { "cell_type": "code", "execution_count": null, "id": "2ea21b76-0048-4aed-98fa-f84483264ff4", "metadata": {}, "outputs": [], "source": [ "\n", "# for loop using `in` and a vector\n", "a = [1, 2, 3]\n", "for x in a\n", " println(\"iteration: $(sin(x))\") # interpolation of the value of sin(x)\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "e18bfeb7-f490-49c7-8537-cb8ca20ae0c4", "metadata": {}, "outputs": [], "source": [ "\n", "# for loop using `in` and a range\n", "inds = 1:5\n", "for x in inds\n", " println(\"iteration: $(sin(x))\")\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "caac0593-6acd-4692-946a-437c09efcadc", "metadata": {}, "outputs": [], "source": [ "\n", "# for loop using unicode sysmbol `∈`\n", "inds = 1:5\n", "for x ∈ inds\n", " println(\"iteration: $(sin(x))\")\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "59d1ceab-faf3-4f64-a656-8c5d728ca9fc", "metadata": {}, "outputs": [], "source": [ "\n", "# for loop using `=` and interpolation in the body of the loop\n", "inds = 1:5\n", "for x = inds\n", " println(\"iteration: $(sin(x))\")\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "9c868b28-eefc-432b-8818-8bbd04afbf5d", "metadata": {}, "outputs": [], "source": [ "\n", "# for loop using a range and interpolation \n", "ran = range(0.0, 2.0, 6)\n", "for x = ran\n", " println(\"iteration: $(log(x + 1.0))\")\n", "end" ] }, { "cell_type": "markdown", "id": "9f0b5d62-8f1f-4070-bcfc-eb00ed8bf3f5", "metadata": {}, "source": [ "## An example of a function" ] }, { "cell_type": "code", "execution_count": null, "id": "3ca1d206-0850-4b24-af84-6126d6e3c388", "metadata": {}, "outputs": [], "source": [ "\n", "function fun(x, y)\n", " println(\"x = $x, y = $y\")\n", " return sqrt(x^2 + y^2) # `return` here is optional\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "71dafdd4-6976-4da6-8d06-45f09af988c8", "metadata": {}, "outputs": [], "source": [ "\n", "fun(3.0, 4.0)" ] }, { "cell_type": "markdown", "id": "38999779-6430-47d2-9127-10a7d81ee74c", "metadata": {}, "source": [ "## Chebyshev polynomials" ] }, { "cell_type": "code", "execution_count": null, "id": "494581dd-b64e-4934-8ec4-9405d80db307", "metadata": {}, "outputs": [], "source": [ "\n", "T(x, n) = cos(n*acos(x)) # Generic Chebyshev Polynomial" ] }, { "cell_type": "code", "execution_count": null, "id": "8f854358-d51d-44c4-b44b-32e7c977baae", "metadata": {}, "outputs": [], "source": [ "\n", "T(0.5, 0)" ] }, { "cell_type": "code", "execution_count": null, "id": "ce406713-3966-4ef2-87d0-2c593fb87977", "metadata": {}, "outputs": [], "source": [ "\n", "T(0.0, 1)" ] }, { "cell_type": "code", "execution_count": null, "id": "6676cdfb-f118-4657-888d-1a73be59648e", "metadata": {}, "outputs": [], "source": [ "\n", "T(1.0, 2)" ] }, { "cell_type": "code", "execution_count": null, "id": "f93f6f8e-daa8-47a8-acae-b49e0fa2dc94", "metadata": {}, "outputs": [], "source": [ "\n", "t2(x) = 2*x^2 - 1.0 # Chebyshev polynomial T_2(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "021e33a6-23fc-4efe-b728-f8c1b050d293", "metadata": {}, "outputs": [], "source": [ "\n", "t2(1.0)" ] }, { "cell_type": "markdown", "id": "2ccaa1ae-34f2-445b-b021-224b95bdec91", "metadata": {}, "source": [ "## Benchmarking" ] }, { "cell_type": "code", "execution_count": null, "id": "bbb3a46d-1901-4826-9fa1-ad7e359982fe", "metadata": {}, "outputs": [], "source": [ "# ] add BenchmarkTools" ] }, { "cell_type": "code", "execution_count": null, "id": "9c3d7c2f-4b9b-43e1-8ee2-aeb9feca86b3", "metadata": {}, "outputs": [], "source": [ "\n", "using BenchmarkTools" ] }, { "cell_type": "code", "execution_count": null, "id": "e6b44be5-fc87-42fc-9b45-a29fd3f55b8f", "metadata": {}, "outputs": [], "source": [ "\n", "x = 2/3\n", "n = 2\n", "@btime T($x, $n)" ] }, { "cell_type": "code", "execution_count": null, "id": "3a25bcb5-184b-48f5-9566-3fa2715ecf8b", "metadata": {}, "outputs": [], "source": [ "\n", "@btime t2($x)" ] }, { "cell_type": "markdown", "id": "5e26e9b2-a773-427e-a6de-654c73ac4e6f", "metadata": {}, "source": [ "### First draft:" ] }, { "cell_type": "code", "execution_count": null, "id": "350e13d0-9224-4d13-bf43-75ef7ebfae37", "metadata": {}, "outputs": [], "source": [ "\n", "function mychebyshev(x, n)\n", " tp = 1.0\n", " tc = x\n", " tn = 0.0\n", " for i in 1:(n-1)\n", " tn = 2*x*tc - tp\n", " tp = tc\n", " tc = tn\n", " end\n", " return tn\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "fe0ab782-0e7e-465d-a857-40137b2b2de1", "metadata": {}, "outputs": [], "source": [ "\n", "mychebyshev(0.0, 9)" ] }, { "cell_type": "code", "execution_count": null, "id": "3f5c9cc8-7a1a-4056-a3a6-ca56a57b8398", "metadata": {}, "outputs": [], "source": [ "\n", "using PyPlot" ] }, { "cell_type": "code", "execution_count": null, "id": "311dd08b-3d91-430c-9051-70c4b649a80c", "metadata": {}, "outputs": [], "source": [ "\n", "x = range(-1.0, 1.0, 100)" ] }, { "cell_type": "code", "execution_count": null, "id": "c51c5cb1-644b-4bef-b54a-d23b31bcba66", "metadata": {}, "outputs": [], "source": [ "\n", "y = mychebyshev.(x, 5)" ] }, { "cell_type": "code", "execution_count": null, "id": "000852d5-feb7-4325-9a52-bb6f40f92c02", "metadata": {}, "outputs": [], "source": [ "\n", "plot(x, y)\n", "grid(true)" ] }, { "cell_type": "code", "execution_count": null, "id": "c2accc4c-cc56-4283-95e6-e9069c518a78", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.10.5", "language": "julia", "name": "julia-1.10" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.5" } }, "nbformat": 4, "nbformat_minor": 5 }