{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using ForwardDiff\n", "using Plots\n", "using Calculus\n", "plotly()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#number of degrees of freedom\n", "n = 10\n", "\n", "left = 0 #x_L default. never changed\n", "right = 11 #x_R default. to be changed later\n", "#number of springs\n", "num_springs = n+1\n", "#lengths of each spring\n", "L = ones(num_springs,1);\n", "#spring constants:\n", "k_spring = ones(num_springs,1)\n", "for i in 1:num_springs\n", " if(i %2 == 0)\n", " k_spring[i] = 0.5\n", " end\n", "end\n", "#Construct spring system matrix: \n", "cols = n+2\n", "rows = num_springs\n", "A = eye(rows,cols)\n", "for i = 1:rows\n", " A[i,i+1] = -1\n", "end\n", "#define PE\n", "PE(x) = sum(1/2*k_spring.*(abs.(A*[left;x;right])-L).^2)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "K = 10000 #maximum number of iterations\n", "epsilon = 1e-8 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#f is a function (PE or rosenbrock). x0 is the initial condition.\n", "function myGradientDescent(f, x0)\n", " #YOUR CODE HERE\n", " #Return optimal x vector\n", "end" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#f is a function (PE or rosenbrock). x0 is the initial condition.\n", "function myNewtonMethod(f, x0)\n", " #YOUR CODE HERE\n", " #Return optimal x vector\n", "end" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#rosenbrock function and initial condition.\n", "rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2\n", "rosenbrockx0 = [0; 0];" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Part 1\n", "println(\"Gradient Descent\")\n", "res = myGradientDescent(rosenbrock, [0;0])\n", "println(res)\n", "println(\"Newton's Method\")\n", "res = myNewtonMethod(rosenbrock, [0,0])\n", "println(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Problem 2, try to solve on large boundary.\n", "left = 0\n", "right = 11\n", "x0 = linspace(1, right-1, n)*0.9" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Part 3, try to solve on smaller boundary. \n", "left = 0\n", "right = 5\n", "x0 = linspace(1, right-1, n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Part 4, Compress the system to get to a smaller boundary.\n", "left = 0\n", "right = 11\n", "x0 = linspace(1, right-1, n)\n", "while right > 1\n", " right = right-0.1\n", " res = myNewtonMethod(PE,x0)\n", " dres = diff([left;res;right])\n", " #println(dres)\n", " if(any(x->x<0, dres))\n", " println(\"FAILURE when right= \", right)\n", " println(res)\n", " break;\n", " end\n", " x0 = res\n", "end\n", "println(left, \" \", right, \" \", x0)" ] } ], "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 }