From e599ffd68404544ba620b5ba55a2705897d44ffd Mon Sep 17 00:00:00 2001 From: Frederic Weidling <fweidli@client61.num.math.uni-goettingen.de> Date: Fri, 9 Dec 2016 12:28:49 +0100 Subject: [PATCH] blatt7 --- 07_Newtonverfahren.ipynb | 227 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 07_Newtonverfahren.ipynb diff --git a/07_Newtonverfahren.ipynb b/07_Newtonverfahren.ipynb new file mode 100644 index 0000000..41c3e81 --- /dev/null +++ b/07_Newtonverfahren.ipynb @@ -0,0 +1,227 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Aufgabe 27\n", + "## Teil 1\n", + "Schreiben Sie ein allgemeines Newtonverfahren, dass Funktion und Ableitung übergeben bekommt und testen Sie an $f(x)=x^2-4$" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nullstelle: 2.000e+00, Funktionswert 0.000e+00, Iterationen 8\n" + ] + } + ], + "source": [ + "from numpy.linalg import norm, solve\n", + "\n", + "def newton(func,jacobian,x0,tol):\n", + " k=0\n", + " delta_x=solve(jacobian(x0),-func(x0))\n", + " x=x0+delta_x\n", + " \n", + " while True:\n", + " k=k+1\n", + " delta_x_new=solve(jacobian(x),-func(x))\n", + " x=x+delta_x_new\n", + " q=norm(delta_x_new)/norm(delta_x)\n", + " if q>=1:\n", + " print('Verfahren scheint nicht zu konvergieren')\n", + " return x,k\n", + " if q/(1-q)*norm(delta_x_new)<=tol:\n", + " return x,k\n", + " else:\n", + " delta_x=delta_x_new.copy()\n", + "\n", + "from numpy import array \n", + " \n", + "f=lambda t: array([t[0]**2-4])\n", + "df=lambda t: array([[2*t[0]]])\n", + "\n", + "x,k=newton(f,df,array([50.]),1e-8)\n", + "print('Nullstelle: {0:1.3e}, Funktionswert {1:1.3e}, Iterationen {2}'.format(float(x),float(f(x)),k) ) #" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false + }, + "source": [ + "## Teil 2\n", + "Nun für die zweite gegebene Funktion" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from numpy import zeros,eye\n", + "\n", + "def f_matrix_eval(x,A):\n", + " y=zeros(x.shape)\n", + " n=max(x.shape)\n", + " y[0:n-1]=A@x[0:n-1]-x[n-1]*x[0:n-1]\n", + " y[n-1]=x[0:n-1]@x[0:n-1]-1\n", + " return y\n", + "\n", + "def f_matrix_jacobi(x,A):\n", + " n=max(x.shape)\n", + " J=zeros([n,n])\n", + " J[0:n-1,0:n-1]=A-x[n-1]*eye(n-1)\n", + " J[0:n-1,n-1]=-x[0:n-1]\n", + " J[n-1,0:n-1]=2*x[0:n-1]\n", + " return J\n", + "\n", + "def f_poisson_eval(x):\n", + " P=array([[2.0,-1,0],[-1,2,-1],[0,-1,2]])\n", + " f=f_matrix_eval(x,P)\n", + " return f\n", + "\n", + "def f_poisson_jacobi(x):\n", + " P=array([[2.0,-1,0],[-1,2,-1],[0,-1,2]])\n", + " J=f_matrix_jacobi(x,P)\n", + " return J" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finden der ersten Nullstelle bei $x=(\\frac{1}{2},\\frac{1}{\\sqrt{2}},\\frac{1}{2},2-\\sqrt{2})$" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nullstelle: [ 0.5 0.70710678 0.5 0.58578644],\n", + "Abstand: 1.0114613123884174e-15,\n", + "Funktionswert: [ 3.88578059e-16 6.66133815e-16 3.88578059e-16 1.33226763e-15], \n", + "Iterationen: 4\n" + ] + } + ], + "source": [ + "x,k=newton(f_poisson_eval,f_poisson_jacobi,array([1.,1.0,0.,0]),1e-8)\n", + "xtrue=array([0.5,2**(-1./2),0.5,2-2**(1./2)])\n", + "print('Nullstelle: {0},\\nAbstand: {1},\\nFunktionswert: {2}, \\nIterationen: {3}'.format(x,norm(x-xtrue),f_poisson_eval(x),k) ) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finden der zweiten Nullstelle bei $x=(\\frac{1}{\\sqrt{2}},0,-\\frac{1}{\\sqrt{2}},2)$" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nullstelle: [ 0.70710678 0. -0.70710678 2. ],\n", + "Abstand: 1.1276404038266872e-12,\n", + "Funktionswert: [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 2.25530705e-12], \n", + "Iterationen: 3\n" + ] + } + ], + "source": [ + "x,k=newton(f_poisson_eval,f_poisson_jacobi,array([1.0,0,-1.,2.]),1e-8)\n", + "xtrue=array([2**(-1./2),0,-2**(-1./2),2.])\n", + "print('Nullstelle: {0},\\nAbstand: {1},\\nFunktionswert: {2}, \\nIterationen: {3}'.format(x,norm(x-xtrue),f_poisson_eval(x),k) ) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finden der dritten Nullstelle bei $x=(\\frac{1}{2},-\\frac{1}{\\sqrt{2}},\\frac{1}{2},2+\\sqrt{2})$" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nullstelle: [ 0.5 -0.70710678 0.5 3.41421356],\n", + "Abstand: 0.0,\n", + "Funktionswert: [ 0. 0. 0. 0.], \n", + "Iterationen: 4\n" + ] + } + ], + "source": [ + "x,k=newton(f_poisson_eval,f_poisson_jacobi,array([1.,-1,1,3.]),1e-8)\n", + "xtrue=array([1./2,-2**(-1./2),1./2,2+2**(1./2)])\n", + "print('Nullstelle: {0},\\nAbstand: {1},\\nFunktionswert: {2}, \\nIterationen: {3}'.format(x,norm(x-xtrue),f_poisson_eval(x),k) ) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} -- GitLab