




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Solution:
function [x,count] = FalsePos(F,a,b,er_final) % Performs bracketing test with the False Positive algorithm to find a root % of the functin F(x) over the interval [a,b] within the relative error % tolerence er.
% Check if [a,b] is a true bracket if F(a)*F(b) >= 0 error([’The interval [’, num2str(a),’,’,num2str(b),’] is not a bracket!’]) end
% Define an initial relative error er = 1;
% Define an ititial guess of the root location x_old = F(b);
% Setup iteration counter count = 0;
while abs(er) > er_final % Define the false positive step x = b - (F(b)*(a-b))/(F(a)-F(b));
% if F(new point) has different sign than F(b) then make new bracket % [new_point,b], else make new bracket [a,new_point] if abs(F(x))<1e-14, % in case x is exactly at the root, to exit the loop break; elseif F(x)*F(b) < 0 a = x; else b = x; end
% Calculate the reative error er = (x - x_old)/x;
% Redefine the old point as the newly calculated point for er % calculation on next iteration x_old = x;
% Iterate the counter. count = count + 1; end
Verifying the solution as requested: f ( x ) = x^2 − 5 over the interval [ 0 , 4 ] (and with error 10−^6 )
[x,count] = FalsePos(@(x) x.ˆ2-5,0,4,1e-6)
x =
count =
13
We can clearly see convergence toward
Solution:
function [x] = find_roots(F,A,B,Ns,er_final) % find_roots performs a successivly incremental search on the function F % over the interval [A,B] with Ns sub-sets. If a possible location is % found, the FalsePos function is called to find the root within a % relative tolerence of er_final.
% Break the interval [A,B] into Ns subspaces; l = linspace(A,B,Ns+1);
% Pre-define the output vector as a null x = [];
% Check for a bracket for index = 1:length(l)- a = l(index) b = l(index+1);
10 ≥ x =
1 + 2 n ⇒ 1 + 2 n ≥ 2 ⇒ n ≥
⇒ n ≥ 1
[x] = find_roots(@(x) cos(10*pi./x),0.1,10,50,1e-6) There were 9 roots found!
x =
0.7407 1.1765 1.3333 1.5385 1.8182 2.2222 2.8571 4.0000 6.
Using a variety of different segment numbers Ns = 50, Ns = 200, Ns = 1000, Ns = 5000, and Ns = 10000 we can find something like:
There were 9 roots found! With 50 segments used
There were 19 roots found! With 200 segments used
There were 41 roots found! With 1000 segments used
There were 83 roots found! With 5000 segments used
There were 99 roots found! With 10000 segments used
If Ns is too small, we loose the fidelity of the zero crossings in the area close to the origin. The disadvantage of taking Ns very large is that we’re sampling lots of segments where there are no crossings (specifically as x grows). The number of roots found is a decaying exponential, and the amount of time spent finding them grows (about) linearly. Not a good trade off if we want all the roots and just keep segmenting. We can augment our system to make it smarter. Rather than having evenly spaced roots, using the linspace command, we can make the segments skewed since we have previous knowledge of the zero crossing locations. If we can make it denser in the smaller values of x, and then wider as x grows it would be possible to find more crossings while evaluating few segments. There are many possible answers, one possibility is to take yi equispaced on [ 0 .1 10] and define xi as 1/ yi. Then, more points are concentrated near the origin.
ex 3
⇒ g ′− ( x ) = − ex 6
ex 3
(^00 1000 2000 3000 4000 5000 6000 7000 8000 9000 )
10
20 30
40
50
60 70
80
90
100 Number of roots found vs. Segment Number
Number of Segments
Numer of Roots Found
0.005 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
0.055^ Computational Time Spent Finding Roots
Numer of Segments
Comutational Time [s]
Figure 1: Comutational Time and Roots
−10 −1 0 1 2 3 4 5
0
10
20
30
40
50
60
70
80 e x (^) − 3x (^2) w/ 0−line Overlay
Figure 2: Plot of Function
⇒ g ′−
e (−^
e (−^
⇒ | g ′−
| < 1 ⇒ Convergence around x =
x0=[-1/2,1,4]; err_max=1e-6; Nmax=200; roots=[];
% Loop to find each of the roots for j=1:3, x=x0(j); xo=x; err=1; iter=0; while (err>err_max & iter<Nmax) x=x-f(x)./df(x); err=abs((x-xo)./x); xo=x; iter=iter+1; end if(iter<Nmax), roots=[roots;x]; end end
display([’Number of roots found ’,int2str(length(roots))]) roots
With the results:
prob Number of roots found 3
roots =
-0.
π h^2 ( 3 R − h ) 3
V (^) ⇒= (^30 30) = π h^2 (^3 R^ −^ h ) 3
⇒ 0 = π h^2 ( 9 − h ) = 90 ⇒ f ( h ) = π h^2 ( 9 − h ) − 90
Therefore f ′^ ( h ) = 18 π h − 3 π h^2.
Using an initial guess h (^0 )^ = R = 3, the Newton-Raphson algorithm takes the form
h ( i +^1 )^ = h ( i )^ −
π h ( i )^2
9 − h ( i )
18 π h ( i )^ − 3 π h ( i )^2 We can perform three iterations of the Newton-Raphson method with a calculator:
− First iteration:
h (^0 )^ = 3 , h (^1 )^ = h (^0 )^ −
π h (^0 )^2
9 − h (^0 )
18 π h (^0 )^ − 3 π h (^0 )^2
⇒ h (^1 )^ = 2. 0610
− Second iteration:
h (^1 )^ = 2. 06 , h (^2 )^ = h (^1 )^ −
π h (^1 )^2
9 − h (^1 )
18 π h (^1 )^ − 3 π h (^1 )^2
⇒ h (^2 )^ = 2. 0270
− Third iteration:
h (^2 )^ = 2. 02 , h (^3 )^ = h (^2 )^ −
π h (^2 )^2
9 − h (^2 )
18 π h (^2 )^ − 3 π h (^2 )^2
⇒ h (^3 )^ = 2. 0269
Using a better precision (ie keeping more significant digits) and performing more iterations would have shown that h (^3 )^ is actually a very good approximation as it is valid up to 7 significant digits! Notice that only 3 iterations were necessary to achieve such a precision, illustrating the particularly rapid convergence of the Newton-Raphson technique.