Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

cs154

Project: cs154
Views: 31
Kernel: Octave
# Approximating f(x) = sqrt(a) # Using Newton # Author: Fabian with additional tweaks from CS154 classmates # Parameters tol = 1e-8; x = 1.5; # define the function function y = f(x) y = (x.^2+log(x)-2); endfunction fun = @f; root = fzero(fun, 1); # define the first derivative of f’(x) = 2x here: function y = ff(x) y = (2*x+(1/x)); endfunction err = 3*tol; i= 0; while err > tol i += 1; z = f(x)/ff(x); err = abs(z); x = x - z; printf("current approx: %e \n", x) printf("At Iter [%d]: the rel. error is:%e \n",i,(root-x)/root) endwhile printf("the approximated value of x is: %e \n", x)
current approx: 1.321237e+00 At Iter [1]: the rel. error is:-5.433378e-03 current approx: 1.314107e+00 At Iter [2]: the rel. error is:-8.131713e-06 current approx: 1.314097e+00 At Iter [3]: the rel. error is:-1.821527e-11 current approx: 1.314097e+00 At Iter [4]: the rel. error is:0.000000e+00 the approximated value of x is: 1.314097e+00
# Bisection method # Edited by Mano, from Prof's notes function s = fa(x) s = (x.^2+log(x)-2); endfunction lb = 1; ub = 2; tol = 1e-8; maxit = 15; function [c, v_err] = bisection2(lb, ub, tol, maxit) # Checking the trap interval condition for the Bisection method if (lb >= ub) printf("Invalid trapping interval for bisection a[%10.8f]:b[%10.8f] \n", lb, ub); else # We store the errors at every iteration to be plotted at the end. v_err = zeros(1, maxit+1); v_err(1) = (ub - lb)/2; i = 0; printf("Initial approx. err:%e and input tolerance: %e\n",v_err(1),tol); # To monitor the improvement per iteration, we will compute the approximate # order of convergence, gamma, described in the Holmes textbook Eq. 2.15 # (Subsection 2.4.1). Gamma computes the ratio of the natural logarithms # of the current error and the error in the previous iteration. We can # only compute this after the first iteration. lnerr = log(v_err(1)); while (v_err(i+1) > tol) && (i < maxit) i++; c = (lb + ub)/2; if fa(c) != 0 if (fa(lb) * fa(c)) < 0 ub = c; else lb = c ; endif v_err(i+1) = (ub - lb)/2; lnerr1 = log(v_err(i+1)); endif c = (lb + ub)/2; printf("x_bar [ %d ] = %e and error = %e \n",i,c, v_err(i+1)); lnerr = lnerr1; endwhile endif endfunction bisection2(lb, ub, tol, maxit)
Initial approx. err:5.000000e-01 and input tolerance: 1.000000e-08 x_bar [ 1 ] = 1.250000e+00 and error = 2.500000e-01 x_bar [ 2 ] = 1.375000e+00 and error = 1.250000e-01 x_bar [ 3 ] = 1.312500e+00 and error = 6.250000e-02 x_bar [ 4 ] = 1.343750e+00 and error = 3.125000e-02 x_bar [ 5 ] = 1.328125e+00 and error = 1.562500e-02 x_bar [ 6 ] = 1.320312e+00 and error = 7.812500e-03 x_bar [ 7 ] = 1.316406e+00 and error = 3.906250e-03 x_bar [ 8 ] = 1.314453e+00 and error = 1.953125e-03 x_bar [ 9 ] = 1.313477e+00 and error = 9.765625e-04 x_bar [ 10 ] = 1.313965e+00 and error = 4.882812e-04 x_bar [ 11 ] = 1.314209e+00 and error = 2.441406e-04 x_bar [ 12 ] = 1.314087e+00 and error = 1.220703e-04 x_bar [ 13 ] = 1.314148e+00 and error = 6.103516e-05 x_bar [ 14 ] = 1.314117e+00 and error = 3.051758e-05 x_bar [ 15 ] = 1.314102e+00 and error = 1.525879e-05 ans = 1.3141
#Secant method #Mano # Input parameters, incl. tolerance and original interval for secant line tol = 1e-8; a = 1; b = 2; # define the function function y = f(x) y = (x.^2+log(x)-2); endfunction fun = @f; root = fzero(fun, 1); err = 3*tol; i= 0; while err > tol z = (f(a)*(a-b))/(f(a)-f(b)); err = abs(z); b = a; a = a - z; printf("current approx: %e \n", a) i += 1; printf("At Iter [%d]: the rel. error is:%e \n", i, (root-a)/root) endwhile printf("the approximated value of a is: %e \n", a)
fun = @f root = 1.3141 current approx: 1.270772e+00 At Iter [1]: the rel. error is:3.296944e-02 current approx: 1.316883e+00 At Iter [2]: the rel. error is:-2.120151e-03 current approx: 1.314072e+00 At Iter [3]: the rel. error is:1.925183e-05 current approx: 1.314097e+00 At Iter [4]: the rel. error is:1.124360e-08 current approx: 1.314097e+00 At Iter [5]: the rel. error is:-5.964686e-14 current approx: 1.314097e+00 At Iter [6]: the rel. error is:0.000000e+00 the approximated value of a is: 1.314097e+00