Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Numerical Methods Homework

Views: 706
Kernel: SageMath (stable)
def bisection(f,a,b,tol): i = 0 if(f(a)*f(b)>0): print("No root found in interval.") else: while(b-a/2.0>tol): midpoint = (a+b/2.0) if f(midpoint) == 0: return midpoint elif f(a)*f(midpoint)<0: b = midpoint else: a = midpoint i = i + 1 return(midpoint) print(i + "is the number of steps")

Jupyter Kernel terminated: This might be caused by running out of memory or hitting a bug in some library (e.g., forking too many processes, trying to access invalid memory, etc.). Consider restarting or upgrading your project or running the relevant code directly in a terminal to track down the cause, as explained here.

#1 f1(x) = x^3-x-9 f2(x) = 3*x^2+x^2-x-5 f3(x) = (cos(x)^2)+6-x a = 2 b = 3 tol = .09 print(bisection (f1,a,b,tol)) #print(bisection(f2,0,1.50,tol)) #print(bisection(f3,0,10,tol))

Jupyter Kernel terminated: This might be caused by running out of memory or hitting a bug in some library (e.g., forking too many processes, trying to access invalid memory, etc.). Consider restarting or upgrading your project or running the relevant code directly in a terminal to track down the cause, as explained here.

#2 tol = .000000009 a = -100 b = 100 f1(x) = x^5+x-1 f2(x) = sin(x) - 6*x-5 f3(x) = ln(x)+x^2-3 bisection(f1,a,b,tol) bisection(f2,a,b,tol) bisection(f3,a,b,tol)
#3 f1(x) = 2x^3-6*x-1 plot(f1,(x,-10,10)) f2(x) = e^(x-2)+x^3-x plot(f2,(x,-10,10)) f3(x) = 1+5*x -6*x^3-e^(2*x) plot(f3,(x,-10,10)) #more to do, pg 31
#4 f(x,a) = x^2-A a1 = 2 a2 = 3 a3 = 5 a = -100 b = 100 tol = .000000009 f1 = f(x,a1) f2 = f(x,a2) f3 = f(x,a3) bisection(f1,a,b,tol) bisection(f2,a,b,tol) bisection(f3,a,b,tol)
#6 f = cos(x)-sin(x) a = 0 b = 1 tol = .0000009 bisection (f, a, b, tol)
#Page 43 def fixedp(f,x0,tol=9e-9,max=10): e = 1 itr = 0 xlist = [] while(e > tol and itr < max): x = f(x0) e = norm(x0-x) x0 = x xlist.append(x0) itr = itr + 1 return x,xlist #1 f1(x) = x^3-2*x-2 f2(x) = e^x+x-7 #f3(x) = e^x+sin(x) print("a") fixedp(f1,0,tol=9e-9,max=1) print("b") #fixedp(f2,0,tol=9e-9,max=100) print("C") #fixedp(f3,0,tol=9e-9,max=100)
#1 f1(x) = x^3-2*x-2 f2(x) = e^x+x-7 #f3(x) = e^x+sin(x) print("a") fixedp(f1,0,tol=9e-9,max=1) print("b") #fixedp(f2,0,tol=9e-9,max=100) print("C") #fixedp(f3,0,tol=9e-9,max=100)

Jupyter Kernel terminated: This might be caused by running out of memory or hitting a bug in some library (e.g., forking too many processes, trying to access invalid memory, etc.). Consider restarting or upgrading your project or running the relevant code directly in a terminal to track down the cause, as explained here.

#2 f1(x) = x^5+x-1 f2(x) = sin(x) - 6*x-5 f3(x) = ln(x)+x^2-3
#3