%[text] # Solving a Transcendental Equation by Recursion %[text] %[text] We want to solve the equation %[text]{"align":"center"} $x = \\cos \\big(x\\big)$ %[text] The key idea is transforming the equation by recursion, obtaining a succession that converges to the solution: %[text]{"align":"center"} $x\_{n} = \\cos \\big(x\_{n-1} \\big) \\; \\Longrightarrow \\; \\big\\{ x\_1\\,,\\; x\_2\\,,\\; \\ldots x\_n\\,,\\; \\ldots \\big\\}$ %[text] where %[text]{"align":"center"} $\\displaystyle lim\_{n \\to \\infty} \\,x\_n = \\bar x \\;:\\quad \\bar x = \\cos \\big( \\bar x \\big)$ %[text]{"align":"center"} % 1st approach tic % <-- what does this command? try "help tic" x = zeros(1,20); x(1) = pi/4; % initial guess n = 1; d = 1; while d > 0.001 & n < 20 n = n+1; x(n) = cos(x(n-1)); d = abs( x(n) - x(n-1) ); end [n,x(n)] %[output:7aa2d985] toc % <-- what does this command? try "help toc" %[output:790c2803] %% %[text] A more efficient approach (allocating less RAM memory to store the intermediate results) % 2nd approach tic xold = pi/4; n = 1; d = 1; while d > 0.001 & n < 20 n = n+1; xnew = cos(xold); d = abs( xnew - xold ); xold = xnew; end [n, xnew, d] %[output:8dc10699] toc %[output:347b6c49] %[appendix]{"version":"1.0"} %--- %[metadata:view] % data: {"layout":"inline","rightPanelPercent":40} %--- %[output:7aa2d985] % data: {"dataType":"matrix","outputData":{"columns":2,"name":"ans","rows":1,"type":"double","value":[["14.0000","0.7388"]]}} %--- %[output:790c2803] % data: {"dataType":"text","outputData":{"text":"Elapsed time is 0.081278 seconds.\n","truncated":false}} %--- %[output:8dc10699] % data: {"dataType":"matrix","outputData":{"columns":3,"name":"ans","rows":1,"type":"double","value":[["14.0000","0.7388","0.0007"]]}} %--- %[output:347b6c49] % data: {"dataType":"text","outputData":{"text":"Elapsed time is 0.018065 seconds.\n","truncated":false}} %---