# AMATH 301 - HW0
University of Washington\
Due 10/6/2021


```python
# Import block
import numpy as np
from sympy import *
init_printing() # Format outputs to LaTeX pretty print where possible
import matplotlib.pyplot as plt
%matplotlib inline
```

Problem 1: Install Matlab/Python. Done.


```python
# Problem 2

# Symbolic functions
t = symbols('t')
f = t*sin(3*t) - exp(t)
df = diff(f)
display(f,df)
```


```python
# Problem 2

# Define domain
dx = 0.1
x = np.arange(-10,10+dx,dx)

# Define Function
f = lambda x: x*np.sin(3*x) - np.exp(x)
flabel = 'f(x) = xsin(3x) - exp(x)'

# Define 1st Derivative of Function
df = lambda x: 3*x*np.cos(3*x) - np.exp(x) + np.sin(3*x)
dflabel = 'df/dx = 3*x*cos(3*x) - exp(x) + sin(3*x)'

# Plot function to verify
plt.style.use('dark_background') # Comment out for light background
plt.figure(figsize=(15,8))
plt.plot(x,f(x),label=flabel)
plt.plot(x,df(x),label=dflabel)
# plt.xlim(x[0],x[len(x)-1])
# plt.ylim(-15,15)
plt.axis([x[0],x[len(x)-1],-15,15])
plt.legend();
```


```python
def nrsolv(fc,dfc,x0,tol):
    """
    Newton-Raphson Method
    x0 = initial guess
    fc = defined function
    dfc = 1st derivative of defined function
    tol = tolerance
    """
    # Run max 100 iterations
    for j in range(1, 101):
        if ( abs(fc(x0)) < 10e-6 ):
            display("Convergence at x = " + str(x0))
            display("Iterations: " + str(j))
            break
        elif j == 100:
            display("No convergence!")
        else:
            x0 = x0 - fc(x0)/dfc(x0)
```


```python
nrsolv(f,df,1,10e-6)
```


    'Convergence at x = -0.8857712789927595'



    'Iterations: 6'



```python
# Problem 3

A = Matrix([[1,2],
           [-1,1]])
B = 2*eye(2)
C = B.col_insert(2,Matrix([-3,-1]))
D = Matrix([[1,2],
           [2,3],
           [-1,0]])
x = Matrix([1,0])
y = Matrix([0,1])
z = Matrix([1,2,-1])
```


```python
# 3a
display(A+B)

#3b
display(3*x-4*y)

#4b
```


$\displaystyle \left[\begin{matrix}3 & 2\\-1 & 3\end{matrix}\right]$



$\displaystyle \left[\begin{matrix}3\\-4\end{matrix}\right]$


78f724f819b64a88482f9b560d007b2861c66341
ipynb
Jupyter Notebook
scientific_computing_UW-AMATH301/HW_Python/AMATH301_HW0.ipynb
jot33/learn_data_science
8324672acff2523bc7a98eacaec96dd97335af09
MIT
scientific_computing_UW-AMATH301/HW_Python/AMATH301_HW0.ipynb
jot33/learn_data_science
8324672acff2523bc7a98eacaec96dd97335af09
MIT
scientific_computing_UW-AMATH301/HW_Python/AMATH301_HW0.ipynb
jot33/learn_data_science
8324672acff2523bc7a98eacaec96dd97335af09
MIT
Qwen/Qwen-72B
1. YES
2. YES