{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculus and Matlab" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Defining Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### inline()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using inline(), we can define custom functions that perform operations we define. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "func1 =\n", " Inline function:\n", " func1(x) = sqrt(x)\n" ] } ], "source": [ "% lets create a function that takes x as an argument and computes the square root\n", "format compact\n", "func1 = inline('sqrt(x)','x') % the 1st parameter is the function we want and the 2nd is the argument " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 2\n", "ans =\n", " 2.8284\n" ] } ], "source": [ "% now we can use it\n", "func1(4)\n", "func1(8)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c =\n", " Inline function:\n", " c(a,b) = sqrt(a^2+b^2)\n" ] } ], "source": [ "% we can do more complex functions with more arguments\n", "c = inline('sqrt(a^2+b^2)','a','b')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 5\n" ] } ], "source": [ "% now we can find the hypotenuse\n", "c(4,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Differentiation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### diff()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f3 =\n", " Inline function:\n", " f3(x) = x^2\n" ] } ], "source": [ "% first we'll define a custom function\n", "f3 = inline('x^2','x')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% x must be a symbolic\n", "x = sym('x');" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "2*x\n" ] } ], "source": [ "% next, we pass the function to diff()\n", "diff(f3(x),x)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "derivative =\n", " Inline function:\n", " derivative(x) = x.*2.0\n" ] } ], "source": [ "% if we are using a pre-defined function, we dont use single-quotes\n", "derivative = inline(diff(f3(x),x),'x')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "4*x\n" ] } ], "source": [ "derivative(2*x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Symbolic Differentiation" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "func =\n", "x^3\n" ] } ], "source": [ "% prep, declare symbolic var, declare a function\n", "clear all\n", "x = sym('x');\n", "func = x^3" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "3*x^2\n" ] } ], "source": [ "% we can esily take the derivative\n", "diff(func,x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### int()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f1 =\n", " Inline function:\n", " f1(x) = x\n" ] } ], "source": [ "clear all\n", "f1 = inline('x','x')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", " 4\n" ] } ], "source": [ "% f1 is simply a linear function\n", "f1(4)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "integral =\n", "x^2/2\n" ] } ], "source": [ "x = sym('x');\n", "integral = int(f1(x),x)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "x\n", "--\n", " 2\n" ] } ], "source": [ "pretty(integral)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Limits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### limit()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f1 =\n", " Inline function:\n", " f1(x) = sin(x)/x\n", "ans =\n", "1\n" ] } ], "source": [ "% the limit of x as x approaches 0\n", "clear all\n", "x = sym('x');\n", "f1 = inline('sin(x)/x','x')\n", "limit(f1(x),x,0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Partial Derivatives" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f1 =\n", " Inline function:\n", " f1(x,y) = x^2+2*y^3\n" ] } ], "source": [ "clear all\n", "x = sym('x');y = sym('y');\n", "f1 = inline('x^2+2*y^3','x','y')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d1 =\n", "2*x\n" ] } ], "source": [ "% partial derivative wrt x\n", "d1 = diff(f1(x,y),x)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "6*y^2\n" ] } ], "source": [ "% partial wrt y\n", "diff(f1(x,y),y)" ] } ], "metadata": { "kernelspec": { "display_name": "Matlab", "language": "matlab", "name": "matlab" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-matlab", "name": "matlab", "version": "0.8.0" } }, "nbformat": 4, "nbformat_minor": 0 }