{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Differentiation example\n", "This demonstrates solving the problem I found [here](http://math.stackexchange.com/questions/221197/a-tough-differential-calculus-problem), using the *SymEngine* gem.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function is described as \n", "$$f_p(x) = \\dfrac{9\\sqrt{x^2+p}}{x^2+2}$$\n", "We need to differentiate w.r.t. $x$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One solution is:\n", "$$f_p^2(x) (x^2 + 2)^2 = 81(x^2 + p) \\quad\\Longrightarrow$$\n", "\n", "$$2 f_p(x) f_p'(x)(x^2 + 2)^2 + 4x f_p^2(x^2 +2) = 162x \\quad\\Longrightarrow$$\n", "\n", "\\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}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will attempt to solve this using features in *SymEngine*\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'symengine'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Declare the symbols" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1/2)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = SymEngine::Symbol.new('x')\n", "p = SymEngine::Symbol.new('p')\n", "half = Rational('1/2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the expression" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"9*(p + x**2)**(1/2)/(2 + x**2)\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fp = 9*((x**2 + p)**half)/((x**2)+2)\n", "fp.to_s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Differentiate wrt $x$" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"-18*x*(p + x**2)**(1/2)/(2 + x**2)**2 + 9*x/((2 + x**2)*(p + x**2)**(1/2))\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "answer = fp.diff(x)\n", "answer.to_s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.2.0", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.2.0" } }, "nbformat": 4, "nbformat_minor": 0 }