Numerical Methods Group No.:__________________________________ Date Performed: _________________________
Rating:__________________________ Date Submitted: __________________
Numerical Methods ROOTS OF EQUATIONS: BRACKETING METHODS Experiment No. 3 I. OBJECTIVES 1. Develop an algorithm which will perform incremental search for intervals containing roots of a function in a given region, which will serve as initial guesses for bracketing methods of locating roots. 2. Develop algorithms for root location using bisection, false-position and modified false-position methods. II. MACHINE PROBLEMS 1. Write a pseudocode that will perform an incremental search of intervals with possible real roots of a user-defined function. The algorithm must allow the user to define the endpoints of the region for which the incremental search will be performed, must return all the possible intervals that contain the root, and graphs the function in the given region. This can be used to determine the initial guesses for the bisection and false-position methods. Implement the pseudocode in VBA and MathScript. 2. Implement a modified false-position algorithm in VBA and MathScript. This will check when one of the bounds is stuck twice in the iteration, and would divide the function in half if it does. Test this, and compare this with bisection and the false-position methods, by finding the root of f ( x )=x 10−1 between x=0 and x=1.3 with a stopping criterion of e s=0.01 . 3. Determine the real roots of
f ( x )=x 5−8 x 4 + 44 x 3−91 x 2+ 85 x−26
using graphical,
bisection, false-position and modified false-position methods. Compare the speed at which each e =0.2 method (except graphical) converges. Use s . 4. Locate the first nontrivial root of
sin x=x
3
,
x
is in radians. Use graphical, bisection and
false-position methods. The root must be correct to 6 significant figures.
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 1
Numerical Methods 5. How much interest (in annual terms) is being paid if an item, whose present worth is $25,000, is being paid at $500 monthly for 6 years? Use bracketing methods to determine the effective rate.
III. METHODOLOGY Machine Problem 3.1 Code 3.1.1 – Pseudocode for Problem 3.1 iter=0 fl=f(xl) fu=f(xu) DO xrold=xr xr=xu-fu*(xl-xu)/(fl-fu) fr=f(xr) iter=iter+1 IF xr<> 0 THEN ea=Abs((xr-xrold)/xr)*100 END IF test=fl*fr IF test<0 THEN xu=xr fu=f(xu) iu=0 il=il+1 IF il>=2 THEN fl=fl/2 ELSE IF test>0 THEN xl=xr fr=f(xl) il=0 iu=iu+1 IF iu>=2 THEN fu=fu/2 ELSE ea=0 END IF IF ea< es OR iter>=imax THEN EXIT END DO MODFALSEPOS =xr Code 3.1.2 – VBA Code for Problem 3.1 Sub machineproblem3_1() Dim n, y, h As Double Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 2
Numerical Methods Dim i As Integer n = InputBox("Upper Boundary") Range("B4").Value = n s = InputBox("Lower Boundary") Range("B5").Value = s h = InputBox("Interval") Range("B6").Value = h For i = 1 To n Cells(i + 2, 5).Value = i x = f(i - 1) * f((i - 1) + 1) Cells(i + 2, 6).Value = f(i - 1) If x < 0 Then Cells(i + 2, 3).Value = (i - 1) + (h / 2) Cells(i + 2, 4).Value = (i - 1) - (h / 2) End If Next i For t = 1 To (-1) * s Cells(t + 2, 7).Value = (-1) * y = f((-1) * (t - 1)) * f((-1) Cells(t + 2, 8).Value = f((-1) If y < 0 Then Cells(t + 2, 3).Value = (t Cells(t + 2, 4).Value = (t End If Next t End Sub
(t - 1) * t - 2) * (t - 1)) - 1) + (h / 2) - 1) - (h / 2)
Function f(x) f = (x ^ 5) - 8 * (x ^ 4) + 44 * (x ^ 3) - 91 * (x ^ 2) + (85 * x) - 26 Range("B3").Value = f End Function
Code 3.1.3 – Mathscript for Problem 3.1 n=input('Upper Boundary') s=input('Lower Boundary') h=input('Interval') for i=1:n a=exp(0.12534*i)+i+5; b=exp(0.12534*(i+1))+(i+1)+5; x= a*b Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 3
Numerical Methods if x<0 xu=i+(h/2) xl=i-(h/2) end end for t=1:(-1)*s c=exp(0.12534*(-1)*t)+((-1)*t)+5; d=exp(0.12534*(-1)*t-1)+((-1)*t-1)+5; y = c*d if y<0 xu=[(-1)*t]+(h/2) xl=[(-1)*t]-(h/2) end end %graph of the function (n) i=1:n a=exp(0.12534*i)+i+5; subplot(2,1,1); plot(i,a); title('Graph for Upper Boundary'); xlabel('Iteration, [i]'); ylabel('Function of n,f[n]'); grid; %graph of the function (s) t=1:(-1)*s c=exp(0.12534*(-1)*t)+((-1)*t)+5; subplot(2,1,2); plot((-1)*t,c); title('Graph for Lower Boundary'); xlabel('Iteration, [t]'); ylabel('Function of t,f[t]'); grid; Machine Problem 3.2 Code 3.2.1 – Pseudocode for Problem 3.2 iter=0 fl=f(xl) fu=f(xu) DO xrold=xr xr=xu-fu*(xl-xu)/(fl-fu) fr=f(xr) Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 4
Numerical Methods iter=iter+1 IF xr<> 0 THEN ea=Abs((xr-xrold)/xr)*100 END IF test=fl*fr IF test<0 THEN xu=xr fu=f(xu) iu=0 il=il+1 IF il>=2 THEN fl=fl/2 ELSE IF test>0 THEN xl=xr fr=f(xl) il=0 iu=iu+1 IF iu>=2 THEN fu=fu/2 ELSE ea=0 END IF IF ea< es OR iter>=imax THEN EXIT END DO MODFALSEPOS =xr Code 3.2.2 – VBA Code for Problem 3.2 Sub machineproblem3_2() Dim i, xl, xu, es, imax, xr, iter, ea As Double i = 1 iter = 0 xu = InputBox("Set value for the Upper Limit") xl = InputBox("Set value for the Lower Limit") imax = InputBox("Maximum Iteration") fl = f(xl) fu = f(xu) es = 0.01 Range("a3:z100").Clear Do Cells(i Cells(i Cells(i Cells(i Cells(i xrold = xr = xu Cells(i
+ 2, + 2, + 2, + 2, + 2, xr - fu + 2,
1).Value 3).Value 2).Value 6).Value 5).Value
= = = = =
i xu xl fu fl
* (xl - xu) / (fl - fu) 4).Value = xr
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 5
Numerical Methods fr = f(xr) Cells(i + 2, 7).Value = fr iter = iter + 1 If xr <> 0 Then ea = Abs((xr - xrold) / xr) * 100 Cells(i + 2, 8).Value = ea End If test = fl * fr If test < 0 Then xu = xr fu = f(xu) iu = 0 il = il + 1 If il >= 2 Then fl = fl / 2 End If ElseIf test > 0 Then xl = xr fl = f(xl) il = 0 iu = iu + 1 If iu >= 2 Then fu = fu / 2 End If Else ea = 0 End If If ea < es Then Exit Sub End If If i = imax Then Exit Sub End If i = i + 1 Loop End Sub Function f(x) f = x ^ 10 - 1 End Function
Code 3.2.3 – Mathscript for Problem 3.2 xu=1.3; Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 6
Numerical Methods xl=0; table=ones(20,5); xr=0;e i=0; ea=1; xunew=1; xlnew=1; while ea>=.01 fxl=(xl^10)-1; fxu=(xu^10)-1; if (xu==xunew) xrnew=(xu+xl)/2; elseif (xl==xlnew) xrnew=(xu+xl)/2; else xrnew=xu-(((fxu)*(xl-xu))/(fxl-fxu)); end i=i+1; ea=abs(((xrnew-xr)/(xrnew))*100); xr=xrnew; xlnew=xl; xunew=xu; table(i,1)=i; table(i,2)=xunew; table(i,3)=xlnew; table(i,4)=xr; table(i,5)=ea; mul=fxl*fxu; if mul>0 xl=xr; else xu=xr; end end disp(num2str(table))
Machine Problem 3.3 Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 7
Numerical Methods Code 3.3.1 – Pseudocode for Problem 3.3 INPUT n,s,h IF R = 0 THEN 'Graphical Method DO DISPLAY i DISPLAY f(i) IF i = iter THEN EXIT DO ENDIf ENDDO ElseIf R = 1 Then 'Bisection Method Z = 0 DO DISPLAY q DISPLAY xu DISPLAY xl xr = (xu * 0.5) + (xl * 0.5) DISPLAY xr T = xr e = Abs((T - Z) / T) * 100 DISPLAY e Z = xr IF f(xl) * f(xr) < 0 THEN xu = xr ELSE xl = xr ENDIF If e <= tol Then Exit Do ENDIF ENDDO ELSEIF R = 2 THEN 'False Position Method Z = 0 Do DISPLAY q DISPLAY xu DISPLAY xl xr = xu - (f(xu) * (xl - xu)) / (f(xl) - f(xu)) DISPLAY xr Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 8
Numerical Methods T = xr e = Abs((T - Z) / T) * 100 DISPLAY e Z = xr IF f(xl) * f(xr) < 0 THEN xu = xr ELSE xl = xr ENDIF IF e <= tol THEN EXIT DO ENDIF ENDDO 'Modified False position Method Else fl = f(xl) fu = f(xu) i = 1 Do DISPLAY i DISPLAY xu DISPLAY xl xrold = xr xr = xu - fu * (xl - xu) / (fl - fu) DISPLAY xr fr = f(xr) iter = iter + 1 If xr <> 0 Then ea = Abs((xr - xrold) / xr) * 100 DISPLAY ea ENDIF test = fl * fr IF test < 0 THEN xu = xr fu = f(xu) iu = 0 il = il + 1 If il >= 2 THEN fl = fl / 2 ENDIF ELSEIF test > 0 THEN xl = xr fl = f(xl) il = 0 iu = iu + 1 If iu >= 2 Then Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 9
Numerical Methods fu = fu / 2 ENDIF ELSE ea = 0 ENDIF IF ea <= tol THEN EXIT ENDIF If i = imax Then Exit Sub ENDIF i = i + 1 ENDDO ENDIF Function f(x) f = (x ^ 5) - 8 * (x ^ 4) + 44 * (x ^ 3) - 91 * (x ^ 2) + (85 * x) - 26 End Function
Code 3.3.2 – VBA Code for Problem 3.3 Function f(x) f = (x ^ 5) - 8 * (x ^ 4) + 44 * (x ^ 3) - 91 * (x ^ 2) + (85 * x) - 26 End Function Sub machineproblem3_bisection() Dim xu, xl, xr As Double Dim q As Integer xu = 1 xl = 0 q = 1 Z = 0 es = 0.2 Do Cells(q + 2, 1).Value = q Cells(q + 2, 2).Value = xu Cells(q + 2, 3).Value = xl xr = (xu * 0.5) + (xl * 0.5) Cells(q + 2, 4).Value = xr T = xr e = Abs((T - Z) / T) * 100 Cells(q + 2, 5) = e Z = xr Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 10
Numerical Methods If f(xl) * f(xr) < 0 Then xu = xr Else xl = xr End If If e <= es Then Exit Do End If q = q + 1 Loop MsgBox ("The root is " & xr) End Sub Sub machineproblem3_falseposition() Dim xu, xl, xr As Double Dim q As Integer xu = 1 xl = 0 q = 1 Z = 0 tol = 0.2 Do Cells(q + 2, 1).Value = q Cells(q + 2, 2).Value = xu Cells(q + 2, 3).Value = xl xr = xu - (f(xu) * (xl - xu)) / (f(xl) - f(xu)) Cells(q + 2, 4).Value = xr T = xr e = Abs((T - Z) / T) * 100 Cells(q + 2, 5) = e Z = xr If f(xl) * f(xr) < 0 Then xu = xr Else xl = xr End If If e <= tol Then Exit Do End If q = q + 1 Loop MsgBox ("The root is " & xr) End Sub Sub machineproblem3_modfalse() xu = 1 Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 11
Numerical Methods xl = 0 q = 1 i = 1 Z = 0 tol = 0.2 fl = f(xl) fu = f(xu) xrold = 0 Do Cells(i + 2, 1).Value = q Cells(i + 2, 2).Value = xu Cells(i + 2, 3).Value = xl xrold = xr xr = xu - fu * (xl - xu) / (fl - fu) Cells(i + 2, 4).Value = xr fr = f(xr) i = i + 1 q = q + 1 If xr <> 0 Then ea = Abs((xr - xrold) / xr) * 100 Cells(i + 1, 5).Value = ea End If test = fl * fr If test < 0 Then xu = xr fu = f(xu) iu = 0 il = il + 1 If il >= 2 Then fl = fl / 2 End If ElseIf test > 0 Then xl = xr fl = f(xl) il = 0 iu = iu + 1 If iu >= 2 Then fu = fu / 2 End If Else ea = 0 End If If ea <= tol Then Exit Do End If Loop MsgBox ("The root is " & xr) Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 12
Numerical Methods End Sub
Code 3.3.3 – Mathscript for Problem 3.3 xu=.6; xl=.1; table=ones(1,5); xr=0; i=0; ea=1; while ea>=.2 xrnew=(xu+xl)/2; fxl=xl^5-(8*xl^4)+(44*xl^3)-(91*xl^2)+(85*xl)-26; fxr=xrnew^5-(8*xrnew^4)+(44*xrnew^3)-(91*xrnew^2)+(85*xrnew)26; i=i+1; ea=abs(((xrnew-xr)/(xrnew))*100); xr=xrnew; table(i,1)=i; table(i,2)=xu; table(i,3)=xl; table(i,4)=xr; table(i,5)=ea; if fxl*fxr>0 xl=xr; else xu=xr; end end disp(num2str(table)) xu=.6; xl=.1; table=ones(1,5); xr=0; i=0; ea=1; while ea>=.2 fxl=xl^5-(8*xl^4)+(44*xl^3)-(91*xl^2)+(85*xl)-26; fxu=xu^5-(8*xu^4)+(44*xu^3)-(91*xu^2)+(85*xu)-26; xrnew=xu-(((fxu)*(xl-xu))/(fxl-fxu)); Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 13
Numerical Methods i=i+1; ea=abs(((xrnew-xr)/(xrnew))*100); xr=xrnew; table(i,1)=i; table(i,2)=xu; table(i,3)=xl; table(i,4)=xr; table(i,5)=ea; mul=fxl*fxu; if mul>0 xl=xr; else xu=xr; end end disp(num2str(table)) xu=.1; xl=.6; table=ones(1,5); xr=0; i=0; ea=1; xunew=1; xlnew=1; while ea>=.2 fxl=xl^5-(8*xl^4)+(44*xl^3)-(91*xl^2)+(85*xl)-26; fxu=xu^5-(8*xu^4)+(44*xu^3)-(91*xu^2)+(85*xu)-26; if (xu==xunew) xrnew=(xu+xl)/2; elseif (xl==xlnew) xrnew=(xu+xl)/2; else xrnew=xu-(((fxu)*(xl-xu))/(fxl-fxu)); end i=i+1; ea=abs(((xrnew-xr)/(xrnew))*100); xr=xrnew; xlnew=xl; xunew=xu; Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 14
Numerical Methods table(i,1)=i; table(i,2)=xunew; table(i,3)=xlnew; table(i,4)=xr; table(i,5)=ea; mul=fxl*fxu; if mul>0 xl=xr; else xu=xr; end end disp(num2str(table))
Machine Problem 3.4 Code 3.4.1 – Pseudocode for Problem 3.4 INPUT n,s,h IF R = 0 THEN 'Graphical Method Do DISPLAY i DISPLAY f(i) IF i = iter THEN EXIT DO ENDIF i = i + inter ENDDO ELSEIF R = 1 Then 'False Position Method Z = 0 Do DISPLAY q DISPLAY xu DISPLAY xl xr = xu - (f(xu) * (xl - xu)) / (f(xl) - f(xu)) DISPLAY xr T = xr e = Abs((T - Z) / T) * 100 DISPLAY e Z = xr Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 15
Numerical Methods IF f(xl) * f(xr) < 0 THEN xu = xr ELSE xl = xr ENDIF IF e <= tol THEN EXIT DO ENDIF q = q + 1 ENDDO DISPLAY xr ENDIF End Sub Function f(x) f = Sin(x) - x ^ 3 End Function Code 3.4.2 – VBA Code for Problem 3.4 Function f(x) f = Sin(x) - x ^ 3 End Function Sub clear() Range("A3:E1000").clear End Sub Sub machineproblem4_bisection() Dim xr As Single Dim e As Single xu = InputBox("Upper Boundary") xl = InputBox("Lower Boundary") tol = 0.5 * (10 ^ -4) q = 1 Z = 0 Do Cells(q + 2, 1).Value Cells(q + 2, 2).Value Cells(q + 2, 3).Value xr = (xu * 0.5) + (xl Cells(q + 2, 4).Value T = xr
= = = * =
q xu xl 0.5) xr
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 16
Numerical Methods e = Abs((T - Z) / T) * 100 Cells(q + 2, 5) = e Z = xr If f(xl) * f(xr) < 0 Then xu = xr Else xl = xr End If If e <= tol Then Exit Do End If q = q + 1 Loop MsgBox ("The root is " & xr) End Sub
Code 3.4.3 – Mathscript for Problem 3.4 xl=input('Lower Initial Guess') xu=input('Upper Initial Guess') n=input('siginificant digit') r=input('(0)for bisection,(1)False Position,(2)Graphical Method') i=input('interval') inter=input('maximum interval') tol=0.5*10^(2-n) fl=sin(xl)-(xl)^3 fu=sin(xu)-(xu)^3 if r==0 %bisection method Z = 0 while(1) xr=(xu*0.5)+(xl*0.5) fr=sin(xr)-(xr)^3 T=xr e=Abs((T-Z)/T)*100 Z=xr if fl*fr<0 xu=xr else xl=xr end Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 17
Numerical Methods if e<=tol break,end end elseif r==1 %False Position Method i=1 Z = 0 while(2) xr=xu-(((fu)*(xl-xu))/((fl)-(fu))) fr=sin(xr)-(xr)^3 T=xr e=Abs((T-Z)/T)*100 Z=xr if fl*fr<0 xu=xr else xl=xr end if e<=tol break,end end else %graphical method p=i:inter z=sin(p)-p.^3 plot(p,z); title('Graphical Method'); xlabel('iteration, [p]'); ylabel('function of z,z[p]'); end Machine Problem 3.5 Code 3.5.1 – Pseudocode for Problem 3.5 INPUT xu, xl tol = 1 i = 1 Z = 0 DO DISPLAY xu DISPLAY xl xr = (xu * 0.5) + (xl * 0.5) Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 18
Numerical Methods Display xr T = xr e = Abs((T - Z) / T) * 100 DISPLAY e Z = xr DISPLAY f(xu) DISPLAY f(xl) DISPLAY f(xr) If f(xl) * f(xr) < 0 Then xu = xr Else xl = xr End If If e <= tol Then Exit Do End If i = i + 1 Loop End Sub Function f(x) a = x / 12 f = ((((a + 1) ^ 72) - 1) / (((a + 1) ^ 72) * a)) - 50 End Function Code 3.4.2 – VBA Code for Problem 3.5 Sub machineproblem3_5 Dim xu, xl, xr As Double Dim T, Z As Double xu = InputBox("value for the upper limit") xl = InputBox("value for the lower limit") tol = 1 i = 1 Z = 0 Do Cells(i + 1, 1).Value = xu Cells(i + 1, 2).Value = xl xr = (xu * 0.5) + (xl * 0.5) Cells(i + 1, 3).Value = xr T = xr e = Abs((T - Z) / T) * 100 Cells(i + 1, 7) = e Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 19
Numerical Methods Z = xr If f(xl) * f(xr) < 0 Then xu = xr Else xl = xr End If If e <= tol Then Exit Do End If i = i + 1 Loop End Sub Function f(x) a = x / 12 'used for interest per period f = ((((a + 1) ^ 72) - 1) / (((a + 1) ^ 72) * a)) - 50 End Function Code 3.5.3 – Mathscript for Problem 3.5 xu=input('value for the upper limit') xl=input('value for the lower limit') tol=1 i=1 Z=0 fu=(((((xu/12)+1)^72)-1)/((((xu/12)+1)^72) * (xu/12)))-50 fl=(((((xl/12)+1)^72)-1)/((((xl/12)+1)^72)*(xl/12)))-50 while(1) xr=(xu*0.5)+(xl*0.5) fr=(((((xr/12)+1)^72)-1)/((((xr/12)+1)^72)*(xr/12)))-50 T=xr e=abs((T-Z)/T)*100 Z=xr if fl*fr<0 xu=xr else xl=xr end if e <= tol break,end i=i+1 end Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 20
Numerical Methods
IV. RESULTS AND INTERPRETATION MACHINE PROBLEM 1 VBA
MATHSCRIPT
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 21
Numerical Methods
MACHINE PROBLEM NO. 2 VBA
MATHSCRIPT
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 22
Numerical Methods
MACHINE PROBLEM NO. 3 VBA
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 23
Numerical Methods
MATHSCRIPT
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 24
Numerical Methods
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 25
Numerical Methods
MACHINE PROBLEM NO. 4 VBA
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 26
Numerical Methods
MATHSCRIPT
V. CONCLUSIONS AND RECOMMENDATIONS Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 27
Numerical Methods After doing the machine problem we were able to derive codes or programs that will approximately solve for the value of a root through the bracketing methods such as using bisection, false-position and modified false-position methods. The following statements are the conclusions of the group that are generated after performing this activity namely; 1. The group obtained the objectives of the said activity in terms of developing an algorithm to be used in making program that performs incremental search for intervals containing roots of a function in a given region. These intervals will be the initial guesses for root location. 2. The group obtained the objectives of the said activity in terms of developing an algorithm to be used in making program that locates the roots of a given function using different methods namely Bisection Method, False-Position Method, and Modified False-Position Method. VI. REFERENCES
Chapra, S., & Canale, R. (2013). Numerical Methods. New York City: Cengage Learning.
Machine Problem No.4 – Roots of Equations: Bracketing Methods
Page 28