From 2cfefa0ec9b798ce3bd3363d9275baa6c44d3565 Mon Sep 17 00:00:00 2001 From: "J. Gergaud" <gergaud@ap240726-01.local> Date: Wed, 20 Nov 2024 11:23:55 +0100 Subject: [PATCH] foo --- M2/TP-Newton.ipynb | 115 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 M2/TP-Newton.ipynb diff --git a/M2/TP-Newton.ipynb b/M2/TP-Newton.ipynb new file mode 100644 index 0000000..ca3d389 --- /dev/null +++ b/M2/TP-Newton.ipynb @@ -0,0 +1,115 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Automatic differentiation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "using LinearAlgebra\n", + "using ForwardDiff\n", + "A = [-1 2;1 4]; b=[1,1]; c=1\n", + "f(x) = 0.5*x'*A*x + b'*x +c\n", + "\n", + "analytic_∇f(x) = 0.5*(A' + A)*x+b\n", + "analytic_∇²f(x) = 0.5*(A' + A)\n", + "\n", + "∇f(x) = ForwardDiff.gradient(f, x)\n", + "∇²f(x) = ForwardDiff.hessian(f, x)\n", + "\n", + "x0 = [1,-1]\n", + "println(\"∇f(x0) = \", ∇f(x0))\n", + "println(\"analytic_∇f(x0) = \", analytic_∇f(x0))\n", + "println(\"analytic_∇f(x0) - ∇f(x0) = \", analytic_∇f(x0)- ∇f(x0))\n", + "\n", + "println(\"∇²f(x0) = \", ∇²f(x0))\n", + "println(\"analytic_∇²f(x0) = \", analytic_∇²f(x0))\n", + "println(\"analytic_∇²f(x0) - ∇²f(x0) = \", analytic_∇²f(x0)- ∇²f(x0))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Newton algorithm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "using LinearAlgebra\n", + "using ForwardDiff\n", + "\n", + "\"\"\"\n", + " Solve by Newton's algorithm the optimization problem Min f(x)\n", + " Case where f is a function from R^n to R\n", + "\"\"\"\n", + "\n", + "\n", + "function algo_Newton(f,x0::Vector{<:Real};AbsTol= abs(eps()), RelTol = abs(eps()), ε=0.01, nbit_max = 0)\n", + "# to complete\n", + " \n", + " # flag = 0 if the program stop on the first criteria\n", + " # flag = 2 if the program stop on the second criteria\n", + " # ...\n", + " return xₖ, flag, fₖ, ∇fₖ , k \n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "A = [1 0 ; 0 9/2]\n", + "b = [0,0]; c=0.\n", + "f1(x) = 0.5*x'*A*x + b'*x +c\n", + "x0 = [1000,-20]\n", + "println(\"Results for Newton on f1 : \", algo_Newton(f1,x0))\n", + "println(\"eigen value of 0.5(A^T+A) = \", 0.5*eigen(A'+A).values)\n", + "using Plots;\n", + "x=range(-10,stop=10,length=100)\n", + "y=range(-10,stop=10,length=100)\n", + "f11(x,y) = f1([x,y])\n", + "p1 = plot(x,y,f11,st=:contourf)\n", + "\n", + "A = [-1 0 ; 0 3]\n", + "f2(x) = 0.5*x'*A*x + b'*x +c\n", + "println(\"Results for Newton on f2 : \", algo_Newton(f2,x0))\n", + "println(\"eigen value of 0.5(A^T+A) = \", 0.5*eigen(A'+A).values)\n", + "f21(x,y) = f1([x,y])\n", + "p2 = plot(x,y,f21,st=:contourf)\n", + "# Rosenbrock function\n", + "x=range(-1.5,stop=2,length=100)\n", + "y=range(-0.5,stop=3.,length=100)\n", + "f3(x) = (1-x[1])^2 + 100*(x[2]-x[1]^2)^2\n", + "f31(x,y) = f3([x,y])\n", + "p3 = plot(x,y,f31,st=:contourf)\n", + "x0 = [1.1,1.2]\n", + "\n", + "println(\"Results for Newton on f3 : \", algo_Newton(f3,[1.1,1.2]))\n", + "\n", + "println(\"Results for Newton on f3 : \", algo_Newton(f3,[3.,0.]))\n", + "plot(p1,p2,p3)" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} -- GitLab