{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mix/max search and sensitivity from design\n",
    "\n",
    "In this example we are going to evaluate the min and max values of the output variable of interest from a sample and to evaluate the gradient of the limit state function defining the output variable of interest at a particular point."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import math as m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the marginal distributions of the parameters\n",
    "dist_E = ot.Beta(0.93, 3.2, 2.8e7, 4.8e7)\n",
    "dist_F = ot.LogNormalMuSigma(30000, 9000, 15000).getDistribution()\n",
    "dist_L = ot.Uniform(250, 260)\n",
    "dist_I = ot.Beta(2.5, 4.0, 3.1e2, 4.5e2)\n",
    "marginals = [dist_E, dist_F, dist_L, dist_I]\n",
    "distribution = ot.ComposedDistribution(marginals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Sample inputs\n",
    "sampleX = distribution.getSample(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the model\n",
    "model = ot.SymbolicFunction(['E', 'F', 'L', 'I'], ['F*L^3/(3*E*I)'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Evaluate outputs\n",
    "sampleY = model(sampleX)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min: y= [5.9225]  with x= [4.18618e+07,20181.7,251.839,433.395]\n",
      "max: y= [31.3208]  with x= [2.9115e+07,62844,255.189,381.749]\n"
     ]
    }
   ],
   "source": [
    "# Get min and max\n",
    "minY = sampleY.getMin()\n",
    "minX = sampleX[sampleY.find(minY)]\n",
    "print('min: y=', minY, ' with x=', minX)\n",
    "maxY = sampleY.getMax()\n",
    "maxX = sampleX[sampleY.find(maxY)]\n",
    "print('max: y=', maxY, ' with x=', maxX)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<p>[[ -1.41477e-07 ]<br>\n",
       " [  0.000293459 ]<br>\n",
       " [  0.070551    ]<br>\n",
       " [ -0.0136654   ]]<br>\n",
       "</p>"
      ],
      "text/plain": [
       "class=Matrix implementation=class=MatrixImplementation name=Unnamed rows=4 columns=1 values=[-1.41477e-07,0.000293459,0.070551,-0.0136654]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get sensitivity at min\n",
    "model.gradient(minX)"
   ]
  }
 ],
 "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": 1
}
