```python
import numpy as np 
import sympy as sp
import scipy as sc
import matplotlib.pyplot as plt 
from scipy.optimize import fsolve
import scipy.optimize as op
from mpl_toolkits.mplot3d import axes3d
%matplotlib notebook
```

$PROBLEM$ $1$

$Part$ $A$


```python
def vFunc(r):
 return sp.sin(r) + r ** 2 + 1
r=sp.Symbol('r')
#to find the expression of first, second and third differential
print(sp.diff(vFunc(r),r))
print(sp.diff(vFunc(r),r,r))
print(sp.diff(vFunc(r),r,r,r))
```

    2*r + cos(r)
    2 - sin(r)
    -cos(r)


$Part$ $B$


```python
r=np.arange(-np.pi,np.pi,0.02)
v=np.sin(r)+ r ** 2 + 1
plt.xlabel("r in radians")
plt.ylabel("v(r)")
plt.ylim(0,5)
plt.plot(r,v,'r')
plt.show()
plt.legend(['v(r)'])
```


    <IPython.core.display.Javascript object>









    <matplotlib.legend.Legend at 0x2aa833038e0>



$Part$ $C$

The Value of the "r" for which function "v" is minimun is approximately -0.48

$Part$ $D$


```python
r=np.arange(-np.pi,np.pi,0.05)
w=2*r + np.cos(r)
plt.xlabel("r in radians")
plt.ylim(0,7,0.5)
plt.plot(r,w,'b')
plt.legend(['dv(r)/dr'])
plt.show()

```

The Value of the "r" for which function "w" is 0 is approximately -0.46. This is equal with the result in Part C. Because the the dervateive of of a function solved at zero would give us the value at which the original function is minumum. In other words the Minimum values of a function can be found using by taking it's first dervative and equating it to zero.

$PROBLEM$ $2$


```python

ax=plt.axes(projection="3d")
def zFunc(x, y):
    return np.sin(x*y) + np.cos(x*y)
#x=np.linspace(-4,4,500)
#y=np.linspace(-1,1,500)
X, Y=np.meshgrid(x, y)
Z=zFunc(X, Y)
ax.plot_surface(X, Y, Z, rstride=20, cstride=20, cmap='winter', edgecolor='none')
ax.set_title('Surface', color='r');
ax.plot_wireframe(X, Y, Z, color='none')
ax.set_xlabel('X - axis', color='green')
ax.set_ylabel('Y - axis', color='purple')
ax.set_zlabel('Z - axis', color='blue')
plt.show()
```


    <IPython.core.display.Javascript object>






$PROBLEM$ $3$

$Part$ $A$


```python
x=np.arange(0,2*np.pi,0.05)
y=np.sin(x)+np.cos(x)
plt.xlabel("X in radians")
plt.ylabel("Y=sin(x)+cos(x)")
plt.plot(x,y)
plt.show()
```


    <IPython.core.display.Javascript object>






$Part$ $B$


```python
#Taylor Series approximation 
x=np.arange(0,2*np.pi,0.05)
x0=0
yacc=np.sin(x)+np.cos(x)
y2=np.sin(x0)+np.cos(x0)+(x-x0)*(np.cos(x0)-np.sin(x0))
y3=y2+((x-x0)**2)*(-np.cos(x0)-np.sin(x0))/2
y4=y3+((x-x0)**3)*(-np.cos(x0)+np.sin(x0))/6
y5=y4+((x-x0)**4)*(np.cos(x0)+np.sin(x0))/24
y6=y5+((x-x0)**5)*(np.cos(x0)-np.sin(x0))/120
plt.plot(x,yacc,x,y2,x,y3,x,y4,x,y5,x,y6)
plt.show()
```


    <IPython.core.display.Javascript object>






$PROBLEM$ $4$

$Part$ $A$


```python
x=np.arange(-6*np.pi,6*np.pi,0.05)
f=(x**3)*np.cos(x)
plt.xlabel("X in rad")
plt.ylabel("F(x)=x^3(Cos(x))")
# plt.ylim(0,5)
plt.plot(x,f)
p=np.pi
xData = np.array([-5.5*p,-4.5*p,-3.5*p,-2.5*p,-1.5*p,-0.5*p,0,0.5*p,1.5*p,2.5*p,3.5*p,4.5*p,5.5*p])
yData = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0])
plt.plot(x,np.zeros(len(x)))
plt.plot(xData, yData, 'ro')
```


    <IPython.core.display.Javascript object>









    [<matplotlib.lines.Line2D at 0x22564979a00>]



$Part$ $B$


```python
#Let's define the function F(x)=x^3(cos(x))
def func(x):
    y = (x**3)*np.cos(x)
    return y

x=fsolve(func,-18)
print(x)
x=fsolve(func,-15)
print(x)
x=fsolve(func,-12)
print(x)
x=fsolve(func,-8)
print(x)
x=fsolve(func,-4)
print(x)
x=fsolve(func,-2)
print(x)
x=fsolve(func,1)
print(x)
x=fsolve(func,2)
print(x)
x=fsolve(func,5)
print(x)
x=fsolve(func,7)
print(x)
x=fsolve(func,11)
print(x)
x=fsolve(func,14)
print(x)
x=fsolve(func,16)
print(x)
print("We see that it has found approximated the 13 roots of the function")
```

    [-17.27875959]
    [-14.13716694]
    [-10.99557429]
    [-7.85398163]
    [-4.71238898]
    [-1.57079633]
    [1.09422902e-49]
    [1.57079633]
    [4.71238898]
    [7.85398163]
    [10.99557429]
    [14.13716694]
    [17.27875959]
    We see that it has found approximated the 13 roots of the function


$PROBLEM$ $5$


```python
def f(z):
    x=z[0]
    y=z[1]
    f=np.zeros(2)
    f[0]=y+2*x+4
    f[1]=y-x**2+4
    return f
zGuess=np.array([-1,1])
z=fsolve(f,zGuess)
print ("First set of Solutions")
print(z,"\n")


zGuess=np.array([0,4])
print ("Second set of Solutions")
z=fsolve(f,zGuess)
print(z)

```

    First set of Solutions
    [-2.00000000e+00  1.66094557e-16] 
    
    Second set of Solutions
    [-1.61558713e-27 -4.00000000e+00]


00882248905265e3052b7ae71c6106f5adfb1eb9
ipynb
Jupyter Notebook
assignments/assignment02/assignment02_solution.ipynb
eyobghiday/computational-mechanics
c64aebca5d26fdf5fc93977a5ec3dbaa4e715a48
MIT
2021-06-27T06:21:23.000Z
2022-03-17T09:48:45.000Z
assignments/assignment02/assignment02_solution.ipynb
eyobghiday/computational-mechanics
c64aebca5d26fdf5fc93977a5ec3dbaa4e715a48
MIT
assignments/assignment02/assignment02_solution.ipynb
eyobghiday/computational-mechanics
c64aebca5d26fdf5fc93977a5ec3dbaa4e715a48
MIT
Qwen/Qwen-72B
1. YES
2. YES