{ "cells": [ { "cell_type": "markdown", "id": "1972fa2c", "metadata": {}, "source": [ "Quiz 6 MCS 471, on Friday 30 September 2022, due 10:50am." ] }, { "cell_type": "markdown", "id": "3717cd15", "metadata": {}, "source": [ "Consider $p = x^2 - 5 x + 2$.\n", "\n", "1. Compute the table of divided differences to interpolate\n", " at $(-2,p(-2))$, $(-1,p(-1))$, $(+1,p(+1))$, and $p(+2,p(+2))$.\n", "\n", "2. Explain why the highest order divided difference $f[-2,-1,+1,+2]$ is zero." ] }, { "cell_type": "markdown", "id": "e9d38a6f", "metadata": {}, "source": [ "# answer to question 1" ] }, { "cell_type": "code", "execution_count": 1, "id": "5ed3b16e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "p (generic function with 1 method)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p(x) = x^2 - 5*x + 2" ] }, { "cell_type": "markdown", "id": "2eddebed", "metadata": {}, "source": [ "We grab the code from lecture 15." ] }, { "cell_type": "code", "execution_count": 2, "id": "c9d731d9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "divdif" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", " divdif(x::Array{Float64,1},f::Array{Float64,1})\n", "\n", "Returns the vector of divided differences for the\n", "Newton form of the interpolating polynomial.\n", "\n", "On entry are x and f;\n", "x contains the abscisses, given as a column vector;\n", "f contains the ordinates, given as a column vector.\n", "\n", "On return are the divided differences.\n", "\"\"\"\n", "function divdif(x::Array{Float64,1},f::Array{Float64,1})\n", " n = length(x)\n", " d = deepcopy(f)\n", " for i=2:n\n", " for j=1:i-1\n", " d[i] = (d[j] - d[i])/(x[j] - x[i])\n", " end\n", " end\n", " return d\n", "end" ] }, { "cell_type": "code", "execution_count": 3, "id": "61549cbf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Vector{Float64}:\n", " -2.0\n", " -1.0\n", " 1.0\n", " 2.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [-2.0, -1.0, 1.0, 2.0]" ] }, { "cell_type": "code", "execution_count": 4, "id": "b26b38b4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Vector{Float64}:\n", " 16.0\n", " 8.0\n", " -2.0\n", " -4.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = [p(z) for z in x]" ] }, { "cell_type": "code", "execution_count": 5, "id": "bdea33c9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4-element Vector{Float64}:\n", " 16.0\n", " -8.0\n", " 1.0\n", " -0.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = divdif(x, y)" ] }, { "cell_type": "markdown", "id": "ac0dfa50", "metadata": {}, "source": [ "# answer to question 2" ] }, { "cell_type": "markdown", "id": "edfcc43e", "metadata": {}, "source": [ "The last divided difference is zero because $p(x)$ is a quadratic polynomial and we use four points to interpolate $p(x)$. \n", "\n", "1. Because the interpolation problem has a unique solution, the interpolating polynomial must equal $p(x)$.\n", "\n", "2. Because $p(x)$ equals the interpolating polynomial, the interpolating polynomial is also a quadratic and its leading term $f[-2, -1, 1, 2](x+2)(x+1)(x-1)$ must be zero, otherwise the interpolating polynomial would be a cubic." ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.8.0", "language": "julia", "name": "julia-1.8" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.8.0" } }, "nbformat": 4, "nbformat_minor": 5 }