You are on page 1of 15

Numerical Analysis Homework 1 9/27/19, 11(29 AM

Question 1

In [54]: import numpy as np


import matplotlib.pyplot as plt

In [55]: fig, ax = plt.subplots(figsize = (7,7))


plt.axis([-1, 5, -1, 5])
plt.grid()

#set x for y(x)=x


x1 = np.arange(-1, 5, 0.1)
#set y for y(x)=x
y1 = x1

#set x for y = g(x)


x2 = np.arange(-1, 5, 0.1)
#set y for y = g(x)
y2 = 2 * (np.log(x2 + 1))

#plot
plt.plot(x1, y1, label = 'y = x', color = 'r')
plt.plot(x2, y2, label = 'y = g(x)', color = 'b')
plt.xlabel('x')
plt.ylabel('y', rotation = 0)
plt.title('Question 1b')
plt.legend()
plt.show()

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 1 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

/Users/feihuyan/anaconda3/lib/python3.7/site-packages/ipykernel_laun
cher.py:13: RuntimeWarning: divide by zero encountered in log
del sys.path[0]

In [71]: Question 3

File "<ipython-input-71-dbc7a56d6a70>", line 1


Question 3
^
SyntaxError: invalid syntax

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 2 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

In [56]: fig, ax = plt.subplots(figsize = (7,7))


plt.axis([-1, 5, -1, 5])
plt.grid()

x1 = np.arange(-1, 5, 0.1)
y1 = x1

x2 = np.arange(-1, 5, 0.1)
y2 = x2 + 0.8 * np.sin(x2)

plt.plot(x1, y1, label = 'y = x', color = 'r')


plt.plot(x2, y2, label = 'y = g(x)', color = 'b')
plt.xlabel('x')
plt.ylabel('y', rotation = 0)
plt.title('Question 3')
plt.legend()
plt.show()

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 3 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

In [57]: def fixedpointiter(x0, epsilon):


#initialize error bigger than epsilon
error = 1
#initialize iteration count
iteration = 0
#while error is bigger than epsilon
while np.absolute(error) > epsilon:
#function
x1 = x0 + 0.8 * np.sin(x0)
#adjust new error
error = np.absolute(x0) - np.absolute(x1)
#report
print('In iteration ' + str(iteration) +', FP is: ' + str(x1)
)
#update new x0
x0 = x1
#adjust iteration count
iteration += 1
print('Got the answer in iteration: ' + str(iteration))
print('Answer is: ' + str(x1))

In [58]: fixedpointiter(1,0.000001)

In iteration 0, FP is: 1.6731767878463173


In iteration 1, FP is: 2.468987745287213
In iteration 2, FP is: 2.967408274430382
In iteration 3, FP is: 3.1060522074572763
In iteration 4, FP is: 3.1344785791458385
In iteration 5, FP is: 3.140169790695297
In iteration 6, FP is: 3.1413080806268083
In iteration 7, FP is: 3.1415357389941234
In iteration 8, FP is: 3.1415812706706348
In iteration 9, FP is: 3.1415903770059614
In iteration 10, FP is: 3.141592198273027
In iteration 11, FP is: 3.14159256252644
Got the answer in iteration: 12
Answer is: 3.14159256252644

In [72]: Question 4

File "<ipython-input-72-90d76f9a0420>", line 1


Question 4
^
SyntaxError: invalid syntax

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 4 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

In [59]: #Plot the function give in question 4


fig, ax = plt.subplots(figsize = (7,7))
plt.axis([-1, 5, -1, 5])
plt.grid()

x = np.arange(-1,5,0.01)
y = np.power(x, 3) - (3 * np.power(x, 2)) + 3

plt.plot(x, y)
plt.show()

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 5 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

In [60]: #define function for secant method


def SecantMethod(x0, x1, epsilon):
#define function
def f(x):
fx = np.power(x, 3) - (3 * np.power(x, 2)) + 3
return fx
#initialize iteration count
iterations = 0
#compute fx
fx0 = f(x0)
fx1 = f(x1)
#while root is bigger than tolerance
while np.absolute(fx1) > epsilon:
#secant method definition
x2 = (((fx1 * x0) - (fx0 * x1))/(fx1 - fx0))
#update new x0, x1 values
x0 = x1
x1 = x2
#update fx2
fx2 = f(x2)
#update iteration
iterations += 1
#report
print('In iteration ' + str(iterations) +', root is: ' + str(
x2))
#update
fx0 = fx1
fx1 = fx2
print('Got the answer in iteration: ' + str(iterations))
print('Answer is: ' + str(x2))

In [61]: SecantMethod(1, 2.5, 0.0001)

In iteration 1, root is: 2.3333333333333335


In iteration 2, root is: 2.5412844036697244
In iteration 3, root is: 2.5295785978913865
In iteration 4, root is: 2.532062797353477
In iteration 5, root is: 2.532088960885232
Got the answer in iteration: 5
Answer is: 2.532088960885232

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 6 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

In [62]: #define newton's method function


def NewtonsMethod(x0, epsilon):
#define f(x)
def f(x):
fx = np.power(x, 3) - (3 * np.power(x, 2)) + 3
return fx
#define f'(x)
def df(x):
dfx = (3 * np.power(x, 2)) - (6 * x)
return dfx
#initialize iteration
iterations = 0
#compute f for variables
fx0 = f(x0)
dfx0 = df(x0)
#while root not reached
while np.absolute(fx0) > epsilon:
#newton's definition
x1 = x0 - (fx0/dfx0)
#update x0
x0 = x1
#update iteration
iterations += 1
print('In iteration ' + str(iterations) +', root is: ' + str(
x1))
#compute new f and new f'
fx0 = f(x1)
dfx0 = df(x1)
print('Got the answer in iteration: ' + str(iterations))
print('Answer is: ' + str(x1))

In [63]: NewtonsMethod(1.5, 0.000001)

In iteration 1, root is: 1.3333333333333333


In iteration 2, root is: 1.347222222222222
In iteration 3, root is: 1.347296353163868
Got the answer in iteration: 3
Answer is: 1.347296353163868

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 7 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

In [64]: NewtonsMethod(2.1, 0.000001)

In iteration 1, root is: 3.6380952380952394


In iteration 2, root is: 2.997908899750493
In iteration 3, root is: 2.665738100120939
In iteration 4, root is: 2.548406077463979
In iteration 5, root is: 2.5323828383309896
In iteration 6, root is: 2.5320889844442256
Got the answer in iteration: 6
Answer is: 2.5320889844442256

Question 8

In [65]: def SecantMethodEllipse(x0, x1, epsilon):


#equation of R plugged into the ellipse
def f(x):
fx = ((x/4)**2) + ((2-0.3*x)/2)**2 - 1
return fx
#initialize iteration count
iterations = 0
#compute f for variables
fx0 = f(x0)
fx1 = f(x1)
#while root not reached
while np.absolute(fx1) > epsilon:
#secant definition
x2 = (((fx1 * x0) - (fx0 * x1))/(fx1 - fx0))
#update x0 and x1
x0 = x1
x1 = x2
fx2 = f(x2)
#update iterations
iterations += 1
print('In iteration ' + str(iterations) +', root is: ' + str(
x2))
#compute new fx0, fx1
fx0 = fx1
fx1 = fx2
print('Got the answer in iteration: ' + str(iterations))
print('Answer is: ' + str(x2))
return x2

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 8 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

In [66]: SecantMethodEllipse(2, 3, 0.000001)

In iteration 1, root is: 4.08


In iteration 2, root is: 3.447316103379722
In iteration 3, root is: 3.5181056149169483
In iteration 4, root is: 3.52968189950075
In iteration 5, root is: 3.529410896639288
Got the answer in iteration: 5
Answer is: 3.529410896639288

Out[66]: 3.529410896639288

In [67]: #I plugged in 3.529 into the equation to


#get 0.941 for the intersection point (3.529, 0.941)

from matplotlib.patches import Ellipse


import matplotlib as mpl

fig, ax = plt.subplots(figsize = (7,7))


plt.axis([-5, 5, -4, 4])
plt.grid()

ellipse = mpl.patches.Ellipse(xy=(0,0), width=8, height=4,fill=False)


ax.add_patch(ellipse)

plt.xlabel('x')
plt.ylabel('y', rotation = 0)

plt.plot([0, 3.5294], [2, 0.941], '-')

plt.title('Question 8')
plt.show()

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 9 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

Question 9

In [68]: def SecantMethodCausticEllipse(x0, y0, u0, v0, epsilon, loops):


#define R plugged into the ellipse equation
def f(t):
ft = (((x0 + t*u0)/4)**2) + (((y0 + t*v0)/2)**2) - 1
return ft
#set up the ellipse plot
fig,ax = plt.subplots(figsize = (7,7))
plt.axis([-5, 5, -4, 4])
plt.grid()
plt.xlabel('x')
plt.xticks()
plt.ylabel('y', rotation = 0)
plt.yticks()
plt.title('Question 9, Ellipse Caustic')
ellipse = mpl.patches.Ellipse(xy=(0,0), width=8, height=4,fill=Fal
se)
ax.add_patch(ellipse)
#initialize loop count
loop = 0

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 10 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

#set up for loop


for x in range(loops):
#initialize guesses
g0 = 2
g1 = 3
#initialize iteration count
iterations = 0
#compute f for initial guesses
fg0 = f(g0)
fg1 = f(g1)
#while root not reached
while np.absolute(fg1) > epsilon:
#secant definition
g2 = (((fg1 * g0) - (fg0 * g1))/(fg1 - fg0))
#update new g0, g1
g0 = g1
g1 = g2
#compute f for g2
fg2 = f(g2)
#update iteration
iterations += 1
#print('In iteration ' + str(iterations) +', root is: ' +
str(g2))
#update new fg0, fg1
fg0 = fg1
fg1 = fg2
#update loop count
loop += 1
#compute the reflection ray
rx = x0 + (g2 * u0)
ry = y0 + (g2 * v0)
nk1x = 1/2*rx
nk1y = 2*ry
llnk1ll = np.sqrt((nk1x**2) + (nk1y)**2)
nk1xn = nk1x/llnk1ll
nk1yn = nk1y/llnk1ll
wk = np.sqrt((u0**2)+(v0**2))
wku = u0/wk
wkv = v0/wk
dot = np.dot([wku, wkv], [nk1xn, nk1yn])
u1 = wku - (2*dot*nk1xn)
v1 = wkv - (2*dot*nk1yn)
#report
print('Variable t for loop ' + str(loop) + ' is: ' + str(g
2))
print('Intersection for this loop is: ' + '['+str(rx)+','+st
r(ry)+']')
#plot on graph
plt.plot([x0, rx], [y0, ry], '-', 't')
#update starting point

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 11 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

x0 = rx
y0 = ry
#update direction vector
u0 = u1
v0 = v1
print('Finished')

In [70]: SecantMethodCausticEllipse(x0=0, y0=2, u0=1, v0=-0.3, epsilon=0.00001,


loops=50)

Variable t for loop 1 is: 3.529410896639288


Intersection for this loop is: [3.529410896639288,0.94117673100821
35]
Variable t for loop 2 is: 1.2645687439444313
Intersection for this loop is: [3.9700903234189315,-0.244123125979
1905]
Variable t for loop 3 is: 1.96346906008326
Intersection for this loop is: [2.5102355549092428,-1.557148184675
2393]
Variable t for loop 4 is: 4.58205086784877
Intersection for this loop is: [-2.069211281747692,-1.711604734508
7386]
Variable t for loop 5 is: 2.2818651730104875
Intersection for this loop is: [-3.925412999898144,-0.384418293448
74456]
Variable t for loop 6 is: 1.1856882419913222
Intersection for this loop is: [-3.6859355623755947,0.776834118438
5701]
Variable t for loop 7 is: 3.286379667136096
Intersection for this loop is: [-0.6259007371952627,1.975365797306
1467]
Variable t for loop 8 is: 4.044348214475126
Intersection for this loop is: [3.3251502100185077,1.1116824685048
394]
Variable t for loop 9 is: 1.3908330106547673
Intersection for this loop is: [3.9942161135157352,-0.107648200470
74353]
Variable t for loop 10 is: 1.6993378601052147
Intersection for this loop is: [2.8780036830230404,-1.388983040705
9418]
Variable t for loop 11 is: 4.459746629548867
Intersection for this loop is: [-1.5586835562857093,-1.84191387463
9104]
Variable t for loop 12 is: 2.646457351510687
Intersection for this loop is: [-3.857034566642147,-0.529923346259
229]
Variable t for loop 13 is: 1.1513401188491676
Intersection for this loop is: [-3.802855421065527,0.6201412995134
352]
Variable t for loop 14 is: 2.8811029319064647

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 12 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

Intersection for this loop is: [-1.2236761881575156,1.904115048768


4844]
Variable t for loop 15 is: 4.3326251245775955
Intersection for this loop is: [3.064538333434066,1.28536090782710
44]
Variable t for loop 16 is: 1.567743510489401
Intersection for this loop is: [3.999635985439782,0.02702387193607
2696]
Variable t for loop 17 is: 1.490304366222743
Intersection for this loop is: [3.1766349441696264,-1.215423869440
1236]
Variable t for loop 18 is: 4.227230720931756
Intersection for this loop is: [-0.9883847913260912,-1.93798194386
76362]
Variable t for loop 19 is: 3.0429556850897286
Intersection for this loop is: [-3.760123163817894,-0.682177143798
6164]
Variable t for loop 20 is: 1.1599490824767436
Intersection for this loop is: [-3.8876228892738776,0.470743362544
8558]
Variable t for loop 21 is: 2.495285303973793
Intersection for this loop is: [-1.7715668478279198,1.793148587008
0854]
Variable t for loop 22 is: 4.523047408344963
Intersection for this loop is: [2.739013091535247,1.45754991485537
52]
Variable t for loop 23 is: 1.7987110921795137
Intersection for this loop is: [3.9868549208137933,0.1620761164972
6417]
Variable t for loop 24 is: 1.3343114451872018
Intersection for this loop is: [3.4133658415662067,-1.042704904577
2359]
Variable t for loop 25 is: 3.9070831256391263
Intersection for this loop is: [-0.3768635393968891,-1.99110344440
00868]
Variable t for loop 26 is: 3.4486955451562347
Intersection for this loop is: [-3.6284203479190777,-0.84180831949
37545]
Variable t for loop 27 is: 1.211830601749199
Intersection for this loop is: [-3.9459471732500075,0.327683075314
72707]
Variable t for loop 28 is: 2.148310409617597
Intersection for this loop is: [-2.2544376750724924,1.652082272553
4514]
Variable t for loop 29 is: 4.597094729506348
Intersection for this loop is: [2.3425529526200704,1.6211449009491
015]
Variable t for loop 30 is: 2.0846513366510644
Intersection for this loop is: [3.9548432710190644,0.2996718120827
6394]

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 13 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

Variable t for loop 31 is: 1.227572940692154


Intersection for this loop is: [3.597212852910292,-0.8746517416122
677]
Variable t for loop 32 is: 3.528591997733519
Intersection for this loop is: [0.25155181955417705,-1.99604094093
22566]
Variable t for loop 33 is: 3.834974012144019
Intersection for this loop is: [-3.454116957634769,-1.008597480481
4928]
Variable t for loop 34 is: 1.3091013234901772
Intersection for this loop is: [-3.98203538814662,0.18933757864718
87]
Variable t for loop 35 is: 1.851497071274104
Intersection for this loop is: [-2.6655430241087172,1.491216705281
638]
Variable t for loop 36 is: 4.547572205155356
Intersection for this loop is: [1.873658098199832,1.76701759783703
15]
Variable t for loop 37 is: 2.4223110861338957
Intersection for this loop is: [3.901236289528002,0.44168078175684
733]
Variable t for loop 38 is: 1.1668174054440552
Intersection for this loop is: [3.736806268880777,-0.7134926363744
518]
Variable t for loop 39 is: 3.1242350531280336
Intersection for this loop is: [0.8686578897004349,-1.952270074899
6726]
Variable t for loop 40 is: 4.169360024667402
Intersection for this loop is: [-3.228676689448017,-1.180640395708
3282]
Variable t for loop 41 is: 1.454976340182183
Intersection for this loop is: [-3.998549824613491,0.0539654143543
0979]
Variable t for loop 42 is: 1.6096398230918019
Intersection for this loop is: [-3.0047478256720352,1.320179435356
6734]
Variable t for loop 43 is: 4.3794385346697835
Intersection for this loop is: [1.3381455746186277,1.8847667117475
098]
Variable t for loop 44 is: 2.8014548843441744
Intersection for this loop is: [3.822234523408226,0.58960202588041
11]
Variable t for loop 45 is: 1.1496558462045354
Intersection for this loop is: [3.84005050609037,-0.55991576670626
15]
Variable t for loop 46 is: 2.7242006981876017
Intersection for this loop is: [1.4484159131568042,-1.864275156556
0522]
Variable t for loop 47 is: 4.421329615542853
Intersection for this loop is: [-2.9434028233126535,-1.35429108300

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 14 of 15
Numerical Analysis Homework 1 9/27/19, 11(29 AM

85289]
Variable t for loop 48 is: 1.65290605100209
Intersection for this loop is: [-3.9967586862039335,-0.08050152442
677838]
Variable t for loop 49 is: 1.422214899033044
Intersection for this loop is: [-3.277572023803815,1.1464727066218
405]
Variable t for loop 50 is: 4.1091983699325
Intersection for this loop is: [0.7493626145157286,1.9645855730150
8]
Finished

In [ ]:

http://localhost:8888/nbconvert/html/Numerical%20Analysis%20Homework%201.ipynb?download=false Page 15 of 15

You might also like