diff --git a/lecture_07/.lecture_07.md.swo b/lecture_07/.lecture_07.md.swo
deleted file mode 100644
index 244c92b..0000000
Binary files a/lecture_07/.lecture_07.md.swo and /dev/null differ
diff --git a/lecture_08/goldmin.m b/lecture_08/goldmin.m
new file mode 100644
index 0000000..e5b0ecf
--- /dev/null
+++ b/lecture_08/goldmin.m
@@ -0,0 +1,36 @@
+function [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,varargin)
+% goldmin: minimization golden section search
+% [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,p1,p2,...):
+% uses golden section search to find the minimum of f
+% input:
+% f = name of function
+% xl, xu = lower and upper guesses
+% es = desired relative error (default = 0.0001%)
+% maxit = maximum allowable iterations (default = 50)
+% p1,p2,... = additional parameters used by f
+% output:
+% x = location of minimum
+% fx = minimum function value
+% ea = approximate relative error (%)
+% iter = number of iterations
+if nargin<3,error('at least 3 input arguments required'),end
+if nargin<4|isempty(es), es=0.0001;end
+if nargin<5|isempty(maxit), maxit=50;end
+phi=(1+sqrt(5))/2;
+iter=0;
+while(1)
+ d = (phi-1)*(xu - xl);
+ x1 = xl + d;
+ x2 = xu - d;
+ if f(x1,varargin{:}) < f(x2,varargin{:})
+ xopt = x1;
+ xl = x2;
+ else
+ xopt = x2;
+ xu = x1;
+ end
+ iter=iter+1;
+ if xopt~=0, ea = (2 - phi) * abs((xu - xl) / xopt) * 100;end
+ if ea <= es | iter >= maxit,break,end
+end
+x=xopt;fx=f(xopt,varargin{:});
\ No newline at end of file
diff --git a/lecture_08/lecture_08.ipynb b/lecture_08/lecture_08.ipynb
index 134ed8a..d99107a 100644
--- a/lecture_08/lecture_08.ipynb
+++ b/lecture_08/lecture_08.ipynb
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": 1,
"metadata": {
"collapsed": false
},
@@ -20,24 +20,6 @@
"Many problems involve finding a minimum or maximum based on given constraints. Engineers and scientists typically use energy balance equations to find the conditions of minimum energy, but this value may never go to 0 and the actual value of energy may not be of interest. "
]
},
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- },
{
"cell_type": "markdown",
"metadata": {},
@@ -64,7 +46,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 2,
"metadata": {
"collapsed": false
},
@@ -243,7 +225,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 3,
"metadata": {
"collapsed": false
},
@@ -281,7 +263,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 4,
"metadata": {
"collapsed": false
},
@@ -515,7 +497,7 @@
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 5,
"metadata": {
"collapsed": false
},
@@ -738,7 +720,7 @@
},
{
"cell_type": "code",
- "execution_count": 10,
+ "execution_count": 6,
"metadata": {
"collapsed": false
},
@@ -748,7 +730,7 @@
"output_type": "stream",
"text": [
"current_min = -0.038904\n",
- "error_app = 0\n"
+ "error_app = 0.13359\n"
]
},
{
@@ -888,30 +870,30 @@
"\tgnuplot_plot_2a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_3a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_4a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_5a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_6a\n",
"\n",
- "\t\n",
+ "\t\n",
"\t\n",
"\n",
"\n",
@@ -961,7 +943,7 @@
},
{
"cell_type": "code",
- "execution_count": 13,
+ "execution_count": 7,
"metadata": {
"collapsed": false
},
@@ -970,7 +952,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "current_min = -0.039000\n",
+ "current_min = -0.038904\n",
"error_app = 0\n"
]
},
@@ -1111,30 +1093,30 @@
"\tgnuplot_plot_2a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_3a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_4a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_5a\n",
"\n",
"\t \n",
- "\t\n",
+ "\t\n",
"\n",
"\t\n",
"\tgnuplot_plot_6a\n",
"\n",
- "\t\n",
+ "\t\n",
"\t\n",
"\n",
"\n",
@@ -1188,7 +1170,7 @@
"collapsed": true
},
"source": [
- "## Parabolc Interpolation\n",
+ "## Parabolic Interpolation\n",
"\n",
"Near a minimum/maximum, the function resembles a parabola. With three data points, it is possible to fit a parabola to the function. So, given a lower and upper bound, we can choose the midpoint and fit a parabola to the 3 x,f(x) coordinates. \n",
"\n",
@@ -1202,7 +1184,7 @@
},
{
"cell_type": "code",
- "execution_count": 31,
+ "execution_count": 8,
"metadata": {
"collapsed": false
},
@@ -1436,18 +1418,11 @@
},
{
"cell_type": "code",
- "execution_count": 32,
+ "execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "f3 = -0.038878\r\n"
- ]
- },
{
"data": {
"image/svg+xml": [
@@ -1653,18 +1628,11 @@
},
{
"cell_type": "code",
- "execution_count": 33,
+ "execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "f3 = -0.038878\r\n"
- ]
- },
{
"data": {
"image/svg+xml": [
@@ -1888,7 +1856,7 @@
},
{
"cell_type": "code",
- "execution_count": 37,
+ "execution_count": 24,
"metadata": {
"collapsed": false
},
@@ -1897,24 +1865,962 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "xmin = 3.2933\r\n"
+ "xmin = 0.32933\n",
+ "Emin = -2.7096e-04\n"
]
+ },
+ {
+ "data": {
+ "image/svg+xml": [
+ ""
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
}
],
"source": [
- "xmin = fminbnd(Au_x,2.8,6)\n",
+ "epsilon = 0.039; % kcal/mol\n",
+ "epsilon = epsilon*6.9477e-21; % J/atom\n",
+ "epsilon = epsilon*1e18; % aJ/J\n",
+ "% final units for epsilon are aJ\n",
+ "\n",
+ "sigma = 2.934; % Angstrom\n",
+ "sigma = sigma*0.10; % nm/Angstrom\n",
+ "x=linspace(2.8,6,200)*0.10; % bond length in um\n",
+ "\n",
+ "Ex = lennard_jones(x,sigma,epsilon);\n",
"\n",
- "Etotal = @(dx,F) Au_x(xmin+dx)-F.*dx;\n"
+ "%[Emin,imin]=min(Ex);\n",
+ "\n",
+ "[xmin,Emin] = fminbnd(@(x) lennard_jones(x,sigma,epsilon),0.28,0.6)\n",
+ "\n",
+ "plot(x,Ex,xmin,Emin,'o')\n",
+ "ylabel('Lennard Jones Potential (aJ/atom)')\n",
+ "xlabel('bond length (nm)')\n",
+ "\n",
+ "Etotal = @(dx,F) lennard_jones(xmin+dx,sigma,epsilon)-F.*dx;\n"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 114,
"metadata": {
- "collapsed": true
+ "collapsed": false
},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: called from\n",
+ " goldmin at line 17 column 1\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n",
+ "warning: Matlab-style short-circuit operation performed for operator |\n"
+ ]
+ },
+ {
+ "data": {
+ "image/svg+xml": [
+ ""
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "N=50;\n",
+ "dx = zeros(1,N); % [in nm]\n",
+ "F_applied=linspace(0,0.0022,N); % [in nN]\n",
+ "for i=1:N\n",
+ " optmin=goldmin(@(dx) Etotal(dx,F_applied(i)),-0.001,0.035);\n",
+ " dx(i)=optmin;\n",
+ "end\n",
+ "\n",
+ "plot(dx,F_applied)\n",
+ "xlabel('dx (nm)')\n",
+ "ylabel('Force (nN)')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "For this function, it is possible to take a derivative and compare the analytical result:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 105,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "F =\n",
+ "\n",
+ "@(dx) 4 * epsilon * 6 * (sigma ^ 6 ./ (xmin + dx) .^ 7 - 2 * sigma ^ 12 ./ (xmin + dx) .^ 13)\n",
+ "\n"
+ ]
+ },
+ {
+ "data": {
+ "image/svg+xml": [
+ ""
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "dx_full=linspace(0,0.06,50);\n",
+ "F= @(dx) 4*epsilon*6*(sigma^6./(xmin+dx).^7-2*sigma^12./(xmin+dx).^13)\n",
+ "plot(dx_full,F(dx_full),dx,F_applied)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "## Curve-fitting\n",
+ "Another example is minimizing error in your approximation of a function. If you have data (now we have Force-displacement data) we can fit this to a function, such as:\n",
+ "\n",
+ "$F(x) = K_{1}\\Delta x + \\frac{1}{2} K_{2}(\\Delta x)^{2}$\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 106,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "function SSE = sse_of_parabola(K,xdata,ydata)\n",
+ " % calculate the sum of squares error for a parabola given a function, func, and xdata and ydata\n",
+ " % output is SSE=sum of squares error\n",
+ " K1=K(1);\n",
+ " K2=K(2);\n",
+ " y_function = K1*xdata+1/2*K2*xdata.^2;\n",
+ " SSE = sum((ydata-y_function).^2);\n",
+ "end\n",
+ " "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 113,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "The nonlinear spring constants are K1=0.16 nN/nm and K2=-5.98 nN/nm^2\n",
+ "The mininum sum of squares error = 7.35e-08\n"
+ ]
+ }
+ ],
+ "source": [
+ "[K,SSE_min]=fminsearch(@(K) sse_of_parabola(K,dx,F_applied),[1,1]);\n",
+ "fprintf('\\nThe nonlinear spring constants are K1=%1.2f nN/nm and K2=%1.2f nN/nm^2\\n',K)\n",
+ "fprintf('The mininum sum of squares error = %1.2e',SSE_min)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 108,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "image/svg+xml": [
+ ""
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "plot(dx,F_applied,'o',dx,K(1)*dx+1/2*K(2)*dx.^2)"
+ ]
}
],
"metadata": {
diff --git a/lecture_08/lecture_08.md b/lecture_08/lecture_08.md
new file mode 100644
index 0000000..8a81a2a
--- /dev/null
+++ b/lecture_08/lecture_08.md
@@ -0,0 +1,626 @@
+
+
+```octave
+%plot --format svg
+```
+
+# Optimization
+
+Many problems involve finding a minimum or maximum based on given constraints. Engineers and scientists typically use energy balance equations to find the conditions of minimum energy, but this value may never go to 0 and the actual value of energy may not be of interest.
+
+The Lennard-Jones potential is commonly used to model interatomic bonding.
+
+$E_{LJ}(x)=4\epsilon \left(\left(\frac{\sigma}{x}\right)^{12}-\left(\frac{\sigma}{x}\right)^{6}\right)$
+
+Considering a 1-D gold chain, we can calculate the bond length, $x_{b}$, with no force applied to the chain and even for tension, F. This will allow us to calculate the nonlinear spring constant of a 1-D gold chain.
+
+
+
+Computational Tools to Study and Predict the Long-Term Stability of Nanowires.
+By Martin E. Zoloff Michoff, Patricio Vélez, Sergio A. Dassie and Ezequiel P. M. Leiva
+
+
+
+[Single atom gold chain mechanics](http://www.uam.es/personal_pdi/ciencias/agrait/)
+
+### First, let's find the minimum energy $\min(E_{LJ}(x))$
+
+## Brute force
+
+
+```octave
+setdefaults
+epsilon = 0.039; % kcal/mol
+sigma = 2.934; % Angstrom
+x=linspace(2.8,6,200); % bond length in Angstrom
+
+Ex = lennard_jones(x,sigma,epsilon);
+
+[Emin,imin]=min(Ex);
+
+plot(x,Ex,x(imin),Emin,'o')
+```
+
+
+
+
+
+
+```octave
+x(imin-1)
+x(imin)
+x(imin+1)
+```
+
+ ans = 3.2824
+ ans = 3.2985
+ ans = 3.3146
+
+
+## Golden Search Algorithm
+
+We can't just look for a sign change for the problem (unless we can take a derivative) so we need a new approach to determine whether we have a maximum between the two bounds.
+
+Rather than using the midpoint of initial bounds, here the problem is more difficult. We need to compare the values of 4 function evaluations. The golden search uses the golden ratio to determine two interior points.
+
+
+
+Start with bounds of 2.5 and 6 Angstrom.
+
+
+```octave
+% define Au atomic potential
+epsilon = 0.039; % kcal/mol
+sigma = 2.934; % Angstrom
+Au_x= @(x) lennard_jones(x,sigma,epsilon);
+
+% calculate golden ratio
+phi = 1/2+sqrt(5)/2;
+% set initial limits
+x_l=2.8;
+x_u=6;
+
+% Iteration #1
+d=(phi-1)*(x_u-x_l);
+
+x1=x_l+d; % define point 1
+x2=x_u-d; % define point 2
+
+
+% evaluate Au_x(x1) and Au_x(x2)
+
+f1=Au_x(x1);
+f2=Au_x(x2);
+plot(x,Au_x(x),x_l,Au_x(x_l),'ro',x2,f2,'rs',x1,f1,'gs',x_u,Au_x(x_u),'go')
+hold on;
+
+if f2x2
+ plot(x4,f4,'*',[x1,x2],[f1,f2])
+ x1=x2;
+ f1=f2;
+ else
+ plot(x4,f4,'*',[x3,x2],[f3,f2])
+ x3=x2;
+ f3=f2;
+ end
+ x2=x4; f2=f4;
+else
+ error('no minimum in bracket')
+end
+hold off
+```
+
+
+
+
+
+
+```octave
+p = polyfit([x1,x2,x3],[f1,f2,f3],2);
+x_fit = linspace(x1,x3,20);
+y_fit = polyval(p,x_fit);
+
+plot(x,Au_x(x),x_fit,y_fit,[x1,x2,x3],[f1,f2,f3],'o')
+hold on
+if f2x2
+ plot(x4,f4,'*',[x1,x2],[f1,f2])
+ x1=x2;
+ f1=f2;
+ else
+ plot(x4,f4,'*',[x3,x2],[f3,f2])
+ x3=x2;
+ f3=f2;
+ end
+ x2=x4; f2=f4;
+else
+ error('no minimum in bracket')
+end
+hold off
+
+```
+
+
+
+
+
+
+```octave
+p = polyfit([x1,x2,x3],[f1,f2,f3],2);
+x_fit = linspace(x1,x3,20);
+y_fit = polyval(p,x_fit);
+
+plot(x,Au_x(x),x_fit,y_fit,[x1,x2,x3],[f1,f2,f3],'o')
+hold on
+if f2x2
+ plot(x4,f4,'*',[x1,x2],[f1,f2])
+ x1=x2;
+ f1=f2;
+ else
+ plot(x4,f4,'*',[x3,x2],[f3,f2])
+ x3=x2;
+ f3=f2;
+ end
+ x2=x4; f2=f4;
+else
+ error('no minimum in bracket')
+end
+hold off
+
+```
+
+
+
+
+
+Parabolic interpolation does not converge in many scenarios even though it it a bracketing method. Instead, functions like `fminbnd` in Matlab and Octave use a combination of the two (Golden Ratio and Parabolic)
+
+## Using the solutions to minimization for the nonlinear spring constant
+
+Now, we have two routines to find minimums of a univariate function (Golden Ratio and Parabolic). Let's use these to solve for the minimum energy given a range of applied forces to the single atom gold chain
+
+$E_{total}(\Delta x) = E_{LJ}(x_{min}+\Delta x) - F \cdot \Delta x$
+
+
+```octave
+epsilon = 0.039; % kcal/mol
+epsilon = epsilon*6.9477e-21; % J/atom
+epsilon = epsilon*1e18; % aJ/J
+% final units for epsilon are aJ
+
+sigma = 2.934; % Angstrom
+sigma = sigma*0.10; % nm/Angstrom
+x=linspace(2.8,6,200)*0.10; % bond length in um
+
+Ex = lennard_jones(x,sigma,epsilon);
+
+%[Emin,imin]=min(Ex);
+
+[xmin,Emin] = fminbnd(@(x) lennard_jones(x,sigma,epsilon),0.28,0.6)
+
+plot(x,Ex,xmin,Emin,'o')
+ylabel('Lennard Jones Potential (aJ/atom)')
+xlabel('bond length (nm)')
+
+Etotal = @(dx,F) lennard_jones(xmin+dx,sigma,epsilon)-F.*dx;
+
+```
+
+ xmin = 0.32933
+ Emin = -2.7096e-04
+
+
+
+
+
+
+
+```octave
+N=50;
+dx = zeros(1,N); % [in nm]
+F_applied=linspace(0,0.0022,N); % [in nN]
+for i=1:N
+ optmin=goldmin(@(dx) Etotal(dx,F_applied(i)),-0.001,0.035);
+ dx(i)=optmin;
+end
+
+plot(dx,F_applied)
+xlabel('dx (nm)')
+ylabel('Force (nN)')
+```
+
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: called from
+ goldmin at line 17 column 1
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+ warning: Matlab-style short-circuit operation performed for operator |
+
+
+
+
+
+
+For this function, it is possible to take a derivative and compare the analytical result:
+
+
+```octave
+dx_full=linspace(0,0.06,50);
+F= @(dx) 4*epsilon*6*(sigma^6./(xmin+dx).^7-2*sigma^12./(xmin+dx).^13)
+plot(dx_full,F(dx_full),dx,F_applied)
+```
+
+ F =
+
+ @(dx) 4 * epsilon * 6 * (sigma ^ 6 ./ (xmin + dx) .^ 7 - 2 * sigma ^ 12 ./ (xmin + dx) .^ 13)
+
+
+
+
+
+
+
+## Curve-fitting
+Another example is minimizing error in your approximation of a function. If you have data (now we have Force-displacement data) we can fit this to a function, such as:
+
+$F(x) = K_{1}\Delta x + \frac{1}{2} K_{2}(\Delta x)^{2}$
+
+
+
+```octave
+function SSE = sse_of_parabola(K,xdata,ydata)
+ % calculate the sum of squares error for a parabola given a function, func, and xdata and ydata
+ % output is SSE=sum of squares error
+ K1=K(1);
+ K2=K(2);
+ y_function = K1*xdata+1/2*K2*xdata.^2;
+ SSE = sum((ydata-y_function).^2);
+end
+
+```
+
+
+```octave
+[K,SSE_min]=fminsearch(@(K) sse_of_parabola(K,dx,F_applied),[1,1]);
+fprintf('\nThe nonlinear spring constants are K1=%1.2f nN/nm and K2=%1.2f nN/nm^2\n',K)
+fprintf('The mininum sum of squares error = %1.2e',SSE_min)
+```
+
+
+ The nonlinear spring constants are K1=0.16 nN/nm and K2=-5.98 nN/nm^2
+ The mininum sum of squares error = 7.35e-08
+
+
+
+```octave
+plot(dx,F_applied,'o',dx,K(1)*dx+1/2*K(2)*dx.^2)
+```
+
+
+
+
diff --git a/lecture_08/lecture_08_files/lecture_08_11_0.svg b/lecture_08/lecture_08_files/lecture_08_11_0.svg
new file mode 100644
index 0000000..c61f790
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_11_0.svg
@@ -0,0 +1,168 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_12_0.svg b/lecture_08/lecture_08_files/lecture_08_12_0.svg
new file mode 100644
index 0000000..b641032
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_12_0.svg
@@ -0,0 +1,163 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_13_0.svg b/lecture_08/lecture_08_files/lecture_08_13_0.svg
new file mode 100644
index 0000000..3e467e5
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_13_0.svg
@@ -0,0 +1,163 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_16_1.svg b/lecture_08/lecture_08_files/lecture_08_16_1.svg
new file mode 100644
index 0000000..ff27c29
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_16_1.svg
@@ -0,0 +1,142 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_17_1.svg b/lecture_08/lecture_08_files/lecture_08_17_1.svg
new file mode 100644
index 0000000..1f3103b
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_17_1.svg
@@ -0,0 +1,136 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_19_1.svg b/lecture_08/lecture_08_files/lecture_08_19_1.svg
new file mode 100644
index 0000000..2a891df
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_19_1.svg
@@ -0,0 +1,135 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_23_0.svg b/lecture_08/lecture_08_files/lecture_08_23_0.svg
new file mode 100644
index 0000000..8fa3bb1
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_23_0.svg
@@ -0,0 +1,196 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_3_0.svg b/lecture_08/lecture_08_files/lecture_08_3_0.svg
new file mode 100644
index 0000000..2c38150
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_3_0.svg
@@ -0,0 +1,147 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_6_1.svg b/lecture_08/lecture_08_files/lecture_08_6_1.svg
new file mode 100644
index 0000000..ecf7a18
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_6_1.svg
@@ -0,0 +1,169 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_7_1.svg b/lecture_08/lecture_08_files/lecture_08_7_1.svg
new file mode 100644
index 0000000..e63011c
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_7_1.svg
@@ -0,0 +1,169 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_8_1.svg b/lecture_08/lecture_08_files/lecture_08_8_1.svg
new file mode 100644
index 0000000..873b04d
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_8_1.svg
@@ -0,0 +1,169 @@
+
\ No newline at end of file
diff --git a/lecture_08/lecture_08_files/lecture_08_9_1.svg b/lecture_08/lecture_08_files/lecture_08_9_1.svg
new file mode 100644
index 0000000..28fd681
--- /dev/null
+++ b/lecture_08/lecture_08_files/lecture_08_9_1.svg
@@ -0,0 +1,169 @@
+
\ No newline at end of file
diff --git a/lecture_08/octave-workspace b/lecture_08/octave-workspace
index f505b0a..990b83a 100644
Binary files a/lecture_08/octave-workspace and b/lecture_08/octave-workspace differ