Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5215
reset
<function reset at 0x7f591d340ed8>
#Exercise 1 #Let g(x)=(1/2)(x+2/x). Consider the simple iteration process x[k+1] = g(x[k]), with initial guess x[0]=a>0. #(a) Plot the graph of g(x) and the line y = x for in one figure to verify that the positive fixed point p exists. Experiment with initial guess to get a figure showing fixed point. #(b) Find algebraically the largest set for initial guess (it is called the basin of attaraction) that guarantees convergence of the iteration process. #(c) Find the positive fixed point exactly. #(d) Plot in one figure the function g(x), y=x, three iteration points (k, x[k]), k=1..3, with initial guess of your choice, and the fixed point p.
g(x)=(1/2)*(x+2/x)
#(a) f_plot=plot(g,0.5,5,figsize=[4,3]) line_plot=line([(0,0), (5,5)])# Learn syntax of the SageMath command line (f_plot+line_plot).show()
#(b) # Your work goes here #x=(sqr(2/3),2i) #(c) # Your work goes here # x^*=sqr(2) #(d) # Your work goes here #############################
#Exercise 2. Make a function simple_iteration(g, a, n) that takes function g, an initial value a, and a positive integer n and returns a list of n iteration points for the fixed point problem x[n+1]=g(x[n]). #Test your function on the problem in Exercise 1.
def simple_iteration(g,a,n): L=[a] for j in range(n): temp=g(L[j-1]).n()# Encode the iteration formula here L.append(temp) return L #My example simple_iteration(g,5,7)
[5, 2.70000000000000, 2.70000000000000, 1.72037037037037, 1.72037037037037, 1.44145536817765, 1.44145536817765, 1.41447098136777]
#Your example simple_iteration(g,2,9)
[2, 1.50000000000000, 1.50000000000000, 1.41666666666667, 1.41666666666667, 1.41421568627451, 1.41421568627451, 1.41421356237469, 1.41421356237469, 1.41421356237309]
######################################
#Demo: Recursive function for finding nth iterate of the fixed point problem def nth_iterate(f,a,n): b=a if n==0: return b else: b=f(b) return (nth_iterate(f,b,n-1)).n() nth_iterate(g,5,4)
1.41447098136777
############################
#Exercise 3. Modify the fixed point problem in Example 3(b) (see Lecture 8) to design a fixed point iteration process that converges to the root x=-1.697471881. Run the function n_iterates that you made in Exercise 2 to find ten iteration points. Make a figure similar to Fig. 8.1 with these points.
###########################
#Exercise 4. For one of the functions in Exercise 4 (see Lecture 8) (your choice), do the following. #(a) Transform the equation into a fixed point problems of the form x=g(x). #(b) Plot functions y = x, y = g(x) in one figure. Choose a range that incudes the fixed point. #(c) By visual inspection of the figure that you have made, estimate location of the fixed point and find an interval of initial guess values that ensure convergence of the simpe iteration procedure x[n+1]=g(x[n]). Choose initial guess x0=a in this interval and run the function simple_iteration(g, xini, n) to find ten iteration points. Make a figure similar to Fig. 8.1 in Lecture 8 with these points.
#(a) Your work goes here var('x') g(x)=x-sin(x)-1/2
x
#(b) Your work goes here f_plot=plot(g(x),-3,x,3)
#(c) # Your work goes here (f_plot).show() Approx=simple_iteration(g,5,10);Approx
########################
# Exercise 5: Newton's method # Your code takes a function, its derivative, and the initial guess x0=a for a root of the equation f(x)=0. Manual and mental work is needed before running the function in order to choose an initial guess that ensures convergence of the method. def my_newton(f,fprime,a,eps):#You may choose different stopping criterion # Complete the code
def my_newton(f,a,n): fbf2e-0ade-4f2a-8d9a-b3e3d1b11811
# Useful command: Finding index of an element in a list L=[1,2,3,4];2 in L
True
L.index(2)
1
#Demo (FYI): Finding cube root of x, 0<x<125, by Newton's method #Note that for x>0 the function x^3-c is growing and concave up. By the rule of thumb, given an interval of the root isolation, the initial guess is set to the right end of the interval. def cube_root(c,eps): L=[j^3 for j in range(1,6)]# List of j^3 to bracket c between two concecutive cubes if c in L: return L.index(c)+1 else: # Bracket x between two consecutive elements of L for j in range(1,5): if c<L[j]: xold=N(L[j]);xnew=xold-(xold^3-c)/(3.0*xold^2)# MR formula is encoded here # Apply Newton's method while abs(xold-xnew)>eps: xold=xnew xnew=xold-(xold^3-c)/(3.0*xold^2) return xnew
cube_root(83,10^(-3))
4.36207067174020
83.^(1/3)
4.36207067145484
#####################
# Mini project: Comparing convergence of the simple iteration method and the Newton-Raphson method #Consider the function f(x)=exp(-x)*(x^2-5*x-2)+1. # Do the following #(a) Plot the function to convince yourself that the function has two roots. Choose one of the roots and use the find_root function to obtain high accuracy value of the root. #(b) (challenging!) Convert the root-finding problem for f to a fixed point problem g(x)=x with appropriate function g. #(c) Choose an initial approximation of the root and make a list L with n=5 iterates using simple iteration and a list N with n=5 iterates using NR iterations. #(d) Make a table with 5 rows for n-values and 3 columns with the table header n, delL[n],and delN[n]. Comment on convergence of these two methods.
f(x)=exp(-x)*(x^2-5*x-2)+1 plot(f(x),-1,x,3)
root1=find_root(f,-1,0);root1
-0.23054999321338798
root2=find_root(f,1,3);root2
2.0895848494844653
#Useful SageMath command for making a table table([(x,N(sin(x), digits=2)) for x in [0..3]], header_row=["$x$", r"$\sin(x)$"], frame=True)
+-----+-----------+ | $x$ | $\sin(x)$ | +=====+===========+ | 0 | 0.00 | +-----+-----------+ | 1 | 0.84 | +-----+-----------+ | 2 | 0.91 | +-----+-----------+ | 3 | 0.14 | +-----+-----------+
#(a) plot(exp(-x)*(x^2-5*x-2)+1,0,3,figsize=[4,3])
g1(x)=(x^2+exp(x)-2)/5
line_plot1=plot(x,-1,0) g1plot=plot(g1,-1,3,fohsize=[4,3]) g1prime_plot=plot(?,color='red') (g1plot+g1prime_plot+line_plot1).show()
#(b)
#(c)
#(d)
######################
# Lab (optional): Exploration of the logistic maps #Follow the directions in Lecture 8. # Help file #//Inputs: Logistic map f_r(x),initial guess u0,number of iterations n #//Input: range for x: xmin, xmax for plotting #//Output: cobweb for f(x) def snail(f,x,u0,n,xmin,xmax): u = u0 P = plot(x, (x, xmin, xmax), color='gray',figsize=[5,3]) for i in range(n): #Learn about command "line" to undertand the plot added to P P += line([[u,u],[u,f(u)],[f(u),f(u)]], color = 'red') u = f(u) P += f.plot(x, xmin, xmax, color='blue') P.show()
reset
<function reset at 0x7f591d340ed8>
#Exercise 1 #Let g(x)=(1/2)(x+2/x). Consider the simple iteration process x[k+1] = g(x[k]), with initial guess x[0]=a>0. #(a) Plot the graph of g(x) and the line y = x for in one figure to verify that the positive fixed point p exists. Experiment with initial guess to get a figure showing fixed point. #(b) Find algebraically the largest set for initial guess (it is called the basin of attaraction) that guarantees convergence of the iteration process. #(c) Find the positive fixed point exactly. #(d) Plot in one figure the function g(x), y=x, three iteration points (k, x[k]), k=1..3, with initial guess of your choice, and the fixed point p.
g(x)=(1/2)*(x+2/x)
#(a) f_plot=plot(?,?,?,figsize=[4,3]) line_plot=line(?,?,?)# Learn syntax of the SageMath command line (f_plot+line_plot).show()
#(b) # Your work goes here #(c) # Your work goes here #(d) # Your work goes here #############################
#Exercise 2. Make a function simple_iteration(g, a, n) that takes function g, an initial value a, and a positive integer n and returns a list of n iteration points for the fixed point problem x[n+1]=g(x[n]). #Test your function on the problem in Exercise 1.
def simple_iteration(g,a,n): L=[a] temp=a for j in range(?): temp=?.n()# Encode the iteration formula here L.append(temp) return L #My example simple_iteration(g,5,4)
[5, 2.70000000000000, 1.72037037037037, 1.44145536817765, 1.41447098136777]
#Your example
######################################
#Demo: Recursive function for finding nth iterate of the fixed point problem def nth_iterate(f,a,n): b=a if n==0: return b else: b=f(b) return (nth_iterate(f,b,n-1)).n() nth_iterate(g,5,4)
1.41447098136777
############################
#Exercise 3. Modify the fixed point problem in Example 3(b) (see Lecture 8) to design a fixed point iteration process that converges to the root x=-1.697471881. Run the function n_iterates that you made in Exercise 2 to find ten iteration points. Make a figure similar to Fig. 8.1 with these points.
###########################
#Exercise 4. For one of the functions in Exercise 4 (see Lecture 8) (your choice), do the following. #(a) Transform the equation into a fixed point problems of the form x=g(x). #(b) Plot functions y = x, y = g(x) in one figure. Choose a range that incudes the fixed point. #(c) By visual inspection of the figure that you have made, estimate location of the fixed point and find an interval of initial guess values that ensure convergence of the simpe iteration procedure x[n+1]=g(x[n]). Choose initial guess x0=a in this interval and run the function simple_iteration(g, xini, n) to find ten iteration points. Make a figure similar to Fig. 8.1 in Lecture 8 with these points.
#(a) Your work goes here
#(b) Your work goes here
#(c) # Your work goes here
########################
# Exercise 5: Newton's method # Your code takes a function, its derivative, and the initial guess x0=a for a root of the equation f(x)=0. Manual and mental work is needed before running the function in order to choose an initial guess that ensures convergence of the method. def my_newton(f,fprime,a,eps):#You may choose different stopping criterion # Complete the code
# Useful command: Finding index of an element in a list L=[1,2,3,4];2 in L
True
L.index(2)
1
#Demo (FYI): Finding cube root of x, 0<x<125, by Newton's method #Note that for x>0 the function x^3-c is growing and concave up. By the rule of thumb, given an interval of the root isolation, the initial guess is set to the right end of the interval. def cube_root(c,eps): L=[j^3 for j in range(1,6)]# List of j^3 to bracket c between two concecutive cubes if c in L: return L.index(c)+1 else: # Bracket x between two consecutive elements of L for j in range(1,5): if c<L[j]: xold=N(L[j]);xnew=xold-(xold^3-c)/(3.0*xold^2)# MR formula is encoded here # Apply Newton's method while abs(xold-xnew)>eps: xold=xnew xnew=xold-(xold^3-c)/(3.0*xold^2) return xnew
cube_root(83,10^(-3))
4.36207067174020
83.^(1/3)
4.36207067145484
#####################
# Mini project: Comparing convergence of the simple iteration method and the Newton-Raphson method #Consider the function f(x)=exp(-x)*(x^2-5*x-2)+1. # Do the following #(a) Plot the function to convince yourself that the function has two roots. Choose one of the roots and use the find_root function to obtain high accuracy value of the root. #(b) (challenging!) Convert the root-finding problem for f to a fixed point problem g(x)=x with appropriate function g. #(c) Choose an initial approximation of the root and make a list L with n=5 iterates using simple iteration and a list N with n=5 iterates using NR iterations. #(d) Make a table with 5 rows for n-values and 3 columns with the table header n, delL[n],and delN[n]. Comment on convergence of these two methods.
#Useful SageMath command for making a table table([(x,N(sin(x), digits=2)) for x in [0..3]], header_row=["$x$", r"$\sin(x)$"], frame=True)
+-----+-----------+ | $x$ | $\sin(x)$ | +=====+===========+ | 0 | 0.00 | +-----+-----------+ | 1 | 0.84 | +-----+-----------+ | 2 | 0.91 | +-----+-----------+ | 3 | 0.14 | +-----+-----------+
#(a) plot(exp(-x)*(x^2-5*x-2)+1,?,?,figsize=[4,3])
#(b)
#(c)
#(d)
######################
# Lab (optional): Exploration of the logistic maps #Follow the directions in Lecture 8. # Help file #//Inputs: Logistic map f_r(x),initial guess u0,number of iterations n #//Input: range for x: xmin, xmax for plotting #//Output: cobweb for f(x) def snail(f,x,u0,n,xmin,xmax): u = u0 P = plot(x, (x, xmin, xmax), color='gray',figsize=[5,3]) for i in range(n): #Learn about command "line" to undertand the plot added to P P += line([[u,u],[u,f(u)],[f(u),f(u)]], color = 'red') u = f(u) P += f.plot(x, xmin, xmax, color='blue') P.show()