# Differentiation example
This demonstrates solving the problem I found [here](http://math.stackexchange.com/questions/221197/a-tough-differential-calculus-problem), using the *SymEngine* gem.

---

The function is described as 
$$f_p(x) = \dfrac{9\sqrt{x^2+p}}{x^2+2}$$
We need to differentiate w.r.t. $x$

One solution is:
$$f_p^2(x) (x^2 + 2)^2 = 81(x^2 + p) \quad\Longrightarrow$$

$$2 f_p(x) f_p'(x)(x^2 + 2)^2 + 4x f_p^2(x^2 +2) = 162x \quad\Longrightarrow$$

\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}

We will attempt to solve this using features in *SymEngine*

---


```ruby
require 'symengine'
```




    true



Declare the symbols


```ruby
x = SymEngine::Symbol.new('x')
p = SymEngine::Symbol.new('p')
half = 1.quo(2)
```




    (1/2)



Create the expression


```ruby
fp = 9*((x**2 + p)**half)/((x**2)+2)
fp.to_s
```




    "9*(p + x**2)**(1/2)/(2 + x**2)"



Differentiate wrt $x$


```ruby
answer = fp.diff(x)
answer.to_s
```




    "-18*x*(p + x**2)**(1/2)/(2 + x**2)**2 + 9*x/((2 + x**2)*(p + x**2)**(1/2))"



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.

1e23f634297249abaeb81cf611f829ccd174abcf
ipynb
Jupyter Notebook
notebooks/differentiation_example.ipynb
Shekharrajak/symengine.rb
7ccfd5d577ce403e3c787870a3d1cc491d62993f
MIT
2015-08-26T23:07:07.000Z
2021-07-08T06:04:23.000Z
notebooks/differentiation_example.ipynb
Shekharrajak/symengine.rb
7ccfd5d577ce403e3c787870a3d1cc491d62993f
MIT
2015-10-13T14:58:42.000Z
2019-12-30T23:34:26.000Z
notebooks/differentiation_example.ipynb
Shekharrajak/symengine.rb
7ccfd5d577ce403e3c787870a3d1cc491d62993f
MIT
2015-08-25T05:11:12.000Z
2020-05-15T01:12:53.000Z
Qwen/Qwen-72B
1. YES
2. YES