|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Differentiation example\n", |
| 8 | + "This demonstrates solving the problem I found [here](http://math.stackexchange.com/questions/221197/a-tough-differential-calculus-problem), using the *SymEngine* gem.\n", |
| 9 | + "\n", |
| 10 | + "---" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "The function is described as \n", |
| 18 | + "$$f_p(x) = \\dfrac{9\\sqrt{x^2+p}}{x^2+2}$$\n", |
| 19 | + "We need to differentiate w.r.t. $x$" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "One solution is:\n", |
| 27 | + "$$f_p^2(x) (x^2 + 2)^2 = 81(x^2 + p) \\quad\\Longrightarrow$$\n", |
| 28 | + "\n", |
| 29 | + "$$2 f_p(x) f_p'(x)(x^2 + 2)^2 + 4x f_p^2(x^2 +2) = 162x \\quad\\Longrightarrow$$\n", |
| 30 | + "\n", |
| 31 | + "\\begin{align}f_p'(x) &= \\frac{162x - 4x f_p^2(x)(x^2+2)}{2f_p(x)(x^2+2)^2}\\\\ \\\\ &= \\frac{162x - 324x\\frac{x^2 + p}{x^2 +2}}{18(x^2 +2)\\sqrt{x^2+p}}\\\\ \\\\ &= \\frac{9 x (x^2+2) - 18x(x^2+p)}{(x^2 + 2)^2 \\sqrt{x^2 + p}}\\\\ \\\\ &= \\frac{9x(2-2p-x^2)}{(x^2 + 2)^2\\sqrt{x^2 + p}}\\end{align}" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "We will attempt to solve this using features in *SymEngine*\n", |
| 39 | + "\n", |
| 40 | + "---" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 1, |
| 46 | + "metadata": { |
| 47 | + "collapsed": false |
| 48 | + }, |
| 49 | + "outputs": [ |
| 50 | + { |
| 51 | + "data": { |
| 52 | + "text/plain": [ |
| 53 | + "true" |
| 54 | + ] |
| 55 | + }, |
| 56 | + "execution_count": 1, |
| 57 | + "metadata": {}, |
| 58 | + "output_type": "execute_result" |
| 59 | + } |
| 60 | + ], |
| 61 | + "source": [ |
| 62 | + "require 'symengine'" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "Declare the symbols" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 3, |
| 75 | + "metadata": { |
| 76 | + "collapsed": false |
| 77 | + }, |
| 78 | + "outputs": [ |
| 79 | + { |
| 80 | + "data": { |
| 81 | + "text/plain": [ |
| 82 | + "(1/2)" |
| 83 | + ] |
| 84 | + }, |
| 85 | + "execution_count": 3, |
| 86 | + "metadata": {}, |
| 87 | + "output_type": "execute_result" |
| 88 | + } |
| 89 | + ], |
| 90 | + "source": [ |
| 91 | + "x = SymEngine::Symbol.new('x')\n", |
| 92 | + "p = SymEngine::Symbol.new('p')\n", |
| 93 | + "half = Rational('1/2')" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "Create the expression" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 5, |
| 106 | + "metadata": { |
| 107 | + "collapsed": false |
| 108 | + }, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "data": { |
| 112 | + "text/plain": [ |
| 113 | + "\"9*(p + x**2)**(1/2)/(2 + x**2)\"" |
| 114 | + ] |
| 115 | + }, |
| 116 | + "execution_count": 5, |
| 117 | + "metadata": {}, |
| 118 | + "output_type": "execute_result" |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "fp = 9*((x**2 + p)**half)/((x**2)+2)\n", |
| 123 | + "fp.to_s" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "Differentiate wrt $x$" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 8, |
| 136 | + "metadata": { |
| 137 | + "collapsed": false |
| 138 | + }, |
| 139 | + "outputs": [ |
| 140 | + { |
| 141 | + "data": { |
| 142 | + "text/plain": [ |
| 143 | + "\"-18*x*(p + x**2)**(1/2)/(2 + x**2)**2 + 9*x/((2 + x**2)*(p + x**2)**(1/2))\"" |
| 144 | + ] |
| 145 | + }, |
| 146 | + "execution_count": 8, |
| 147 | + "metadata": {}, |
| 148 | + "output_type": "execute_result" |
| 149 | + } |
| 150 | + ], |
| 151 | + "source": [ |
| 152 | + "answer = fp.diff(x)\n", |
| 153 | + "answer.to_s" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "markdown", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "Which is indeed correct! If you simplify the second last answer that we got from our solution. You will find the exact same answer. Using *SymEngine* for solving problems is as simple as that." |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "metadata": { |
| 165 | + "kernelspec": { |
| 166 | + "display_name": "Ruby 2.2.0", |
| 167 | + "language": "ruby", |
| 168 | + "name": "ruby" |
| 169 | + }, |
| 170 | + "language_info": { |
| 171 | + "file_extension": "rb", |
| 172 | + "mimetype": "application/x-ruby", |
| 173 | + "name": "ruby", |
| 174 | + "version": "2.2.0" |
| 175 | + } |
| 176 | + }, |
| 177 | + "nbformat": 4, |
| 178 | + "nbformat_minor": 0 |
| 179 | +} |
0 commit comments