{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%plot gnuplot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$f'(x_0) \\approx \\frac{f(x_0 + h) -f(x_0)}{h} + O(h)$\n", "\n", "error = $|f'(x_0) - \\frac{f(x_0 + h) -f(x_0)}{h}| \\approx O(h)$ \n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c1 =\n", "\n", " 1.002263431950136 -0.740850628132612\n", "\n" ] } ], "source": [ "clear all\n", "format long\n", "f = @(x) sin(x);\n", "x0=1.2;\n", "f0=sin(x0);\n", "fp=cos(x0);\n", "i=-6:0.5:0;\n", "h=10.^i;\n", "err=abs(fp-(sin(x0+h)-f0)./h);\n", "subplot(1,2,1)\n", "loglog(h,err,'-*');\n", "xlabel('h')\n", "ylabel('error = |f(x_0) (exact) - f(x_0) (numeric)|')\n", "c1 = polyfit(log(h),log(abs(err)),1)\n", "hold on\n", "subplot(1,2,2)\n", "\n", "plot(log(h), c1(1)*log(h)+c1(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
c1(1) = 1.002263431950136 means the order of error is $\\approx 1$.
\n", "