{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Plots.PlotlyBackend()" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Plots\n", "plotly()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "F (generic function with 1 method)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Generated using the Calculus package\n", "#Here are functions for the polynomial and its 5 next derivatives. \n", "#They are combined into a function F that generates a vector of these functions.\n", "#Because the root finder code uses all of these derivatvies, use F(x)[ind] to be the function your root-finding for.\n", "f(x) = -(1/720)+x/36-(31 *x^2)/144+(29* x^3)/36-(29* x^4)/20+x^5\n", "fp(x) = ((((0.027777777777777776 - (31 * (2x)) / 144) + (29 * (3 * x ^ 2)) / 36) - (29 * (4 * x ^ 3)) / 20) + 5 * x ^ 4)\n", "fpp(x) = (((-0.4305555555555556 + (29 * (3 * (2x))) / 36) - (29 * (4 * (3 * x ^ 2))) / 20) + 5 * (4 * x ^ 3))\n", "fppp(x) = ((4.833333333333333 - (29 * (4 * (3 * (2x)))) / 20) + 5 * (4 * (3 * x ^ 2)))\n", "fpppp(x) = (-34.8 + 5 * (4 * (3 * (2x))))\n", "fppppp(x) = 120\n", "F(x) = [f(x), fp(x), fpp(x), fppp(x), fpppp(x), fppppp(x)]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " \n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Here is a visual on the function\n", "X = 0:0.0001:1\n", "plot(X,f.(X), xaxis=[0,0.6], yaxis=[-0.001,0.001])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dekker (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Dekker Method\n", "n = 100000 #maximum number of iterations permitted without finding a root.\n", "epsilon_f = 1e-10 #epsilon of F value.\n", "epsilon_x = 1e-10 #epsilon of X value\n", "# Insert your code into this function. \n", "# Use F(x)[ind] to call f (this format is required by the roots function later)\n", "# l is the lower part of the range\n", "# r is the upper part of the range\n", "function Dekker(ind, l, r)\n", " \n", "end" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "roots (generic function with 1 method)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Call: roots(6,0,1)\n", "function roots(Level, Low, High)\n", " if(Level==1)\n", " #println(\"Range: \", Low,\" \",High)\n", " end\n", " if(Level==0)\n", " return\n", " end\n", " root = Dekker(Level, Low, High) \n", " #println(root, \" \", Level, \" \", Low, \" \", High)\n", " if(Level == 1)\n", " if(root==nothing || abs(root - High) < epsilon_x || abs(root-Low) < epsilon_x)\n", " \n", " else\n", " println(root)\n", " end\n", " end\n", " if(root == nothing || abs(root - High) < epsilon_x || abs(root-Low) < epsilon_x)\n", " roots(Level-1, Low, High)\n", " else\n", " roots(Level-1, Low, root)\n", " roots(Level-1, root, High)\n", " end\n", " \n", "end" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#This function prints out the roots found by using your Dekker function.\n", "roots(6,0,1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#This function returns a single root of the original polynomial in the range 0.1 and 0.8\n", "Dekker(1,0.1,0.8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.6.2", "language": "julia", "name": "julia-0.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }