Experiment No. 3 - Roots of Equations Bracket Methods
-
Upload
cedric-dela-cruz -
Category
Documents
-
view
6 -
download
3
description
Transcript of Experiment No. 3 - Roots of Equations Bracket Methods
Numerical Methods
Group No.:__________________________________ Rating:__________________________Date Performed: _________________________ Date Submitted: __________________
Numerical Methods
ROOTS OF EQUATIONS: BRACKETING METHODSExperiment 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 )=x10−1 between x=0 and x=1.3 with a stopping criterion of es=0.01%.
3. Determine the real roots of f ( x )=x5−8x 4+44 x3−91x2+85 x−26 using graphical, bisection, false-position and modified false-position methods. Compare the speed at which each method (except graphical) converges. Use es=0.2%.
4. Locate the first nontrivial root of sin x=x3, x is in radians. Use graphical, bisection and false-position methods. The root must be correct to 6 significant figures.
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.
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 1
Numerical Methods
III. METHODOLOGY
Machine Problem 3.1
Code 3.1.1 – Pseudocode for Problem 3.1iter=0fl=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 EXITEND DOMODFALSEPOS =xr
Code 3.1.2 – VBA Code for Problem 3.1Sub machineproblem3_1()
Dim n, y, h As DoubleDim i As Integer
n = InputBox("Upper Boundary")Range("B4").Value = ns = InputBox("Lower Boundary")Range("B5").Value = s
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 2
Numerical Methods
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 IfNext i
For t = 1 To (-1) * s Cells(t + 2, 7).Value = (-1) * (t - 1) y = f((-1) * (t - 1)) * f((-1) * t - 2) Cells(t + 2, 8).Value = f((-1) * (t - 1)) If y < 0 Then Cells(t + 2, 3).Value = (t - 1) + (h / 2) Cells(t + 2, 4).Value = (t - 1) - (h / 2) End IfNext tEnd Sub
Function f(x)f = (x ^ 5) - 8 * (x ^ 4) + 44 * (x ^ 3) - 91 * (x ^ 2) + (85 * x) - 26Range("B3").Value = fEnd Function
Code 3.1.3 – Mathscript for Problem 3.1n=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 if x<0 xu=i+(h/2) xl=i-(h/2) endend
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 3
Numerical Methods
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) endend
%graph of the function (n) i=1:na=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)*sc=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.2iter=0fl=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
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 4
Numerical Methods
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 EXITEND DOMODFALSEPOS =xr
Code 3.2.2 – VBA Code for Problem 3.2Sub machineproblem3_2()Dim i, xl, xu, es, imax, xr, iter, ea As Doublei = 1iter = 0xu = 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.01Range("a3:z100").Clear
Do Cells(i + 2, 1).Value = i Cells(i + 2, 3).Value = xu Cells(i + 2, 2).Value = xl Cells(i + 2, 6).Value = fu Cells(i + 2, 5).Value = fl xrold = xr xr = xu - fu * (xl - xu) / (fl - fu) Cells(i + 2, 4).Value = xr fr = f(xr) Cells(i + 2, 7).Value = fr iter = iter + 1 If xr <> 0 Then ea = Abs((xr - xrold) / xr) * 100
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 5
Numerical Methods
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 IfElseIf 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 + 1LoopEnd Sub Function f(x)f = x ^ 10 - 1End Function
Code 3.2.3 – Mathscript for Problem 3.2xu=1.3;xl=0;table=ones(20,5);xr=0;ei=0;ea=1;xunew=1;
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 6
Numerical Methods
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
Code 3.3.1 – Pseudocode for Problem 3.3INPUT n,s,h
IF R = 0 THEN
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 7
Numerical Methods
'Graphical Method DO
DISPLAY iDISPLAY f(i)
IF i = iter THEN EXIT DO ENDIf ENDDO
ElseIf R = 1 Then'Bisection MethodZ = 0DO
DISPLAY qDISPLAY xuDISPLAY 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 MethodZ = 0Do
DISPLAY qDISPLAY xuDISPLAY 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 8
Numerical Methods
IF f(xl) * f(xr) < 0 THEN xu = xr ELSE xl = xr ENDIF IF e <= tol THEN EXIT DO ENDIFENDDO
'Modified False position MethodElsefl = f(xl)fu = f(xu)i = 1Do
DISPLAY iDISPLAY xuDISPLAY 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 fu = fu / 2 ENDIF ELSE ea = 0
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 9
Numerical Methods
ENDIF IF ea <= tol THEN EXIT ENDIF If i = imax Then Exit Sub ENDIF i = i + 1ENDDOENDIF
Function f(x)f = (x ^ 5) - 8 * (x ^ 4) + 44 * (x ^ 3) - 91 * (x ^ 2) + (85 * x) - 26End Function
Code 3.3.2 – VBA Code for Problem 3.3Function f(x)f = (x ^ 5) - 8 * (x ^ 4) + 44 * (x ^ 3) - 91 * (x ^ 2) + (85 * x) - 26End Function
Sub machineproblem3_bisection()Dim xu, xl, xr As DoubleDim q As Integerxu = 1xl = 0q = 1Z = 0es = 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 If f(xl) * f(xr) < 0 Then xu = xr Else xl = xr
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 10
Numerical Methods
End If If e <= es Then Exit Do End If q = q + 1LoopMsgBox ("The root is " & xr)End Sub
Sub machineproblem3_falseposition()Dim xu, xl, xr As DoubleDim q As Integerxu = 1xl = 0q = 1Z = 0tol = 0.2Do 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 + 1LoopMsgBox ("The root is " & xr)End Sub
Sub machineproblem3_modfalse()xu = 1xl = 0q = 1i = 1Z = 0
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 11
Numerical Methods
tol = 0.2fl = f(xl)fu = f(xu)xrold = 0Do 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 IfLoopMsgBox ("The root is " & xr)End Sub
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 12
Numerical Methods
Code 3.3.3 – Mathscript for Problem 3.3xu=.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)); i=i+1; ea=abs(((xrnew-xr)/(xrnew))*100); xr=xrnew; table(i,1)=i; table(i,2)=xu;
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 13
Numerical Methods
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; table(i,1)=i; table(i,2)=xunew; table(i,3)=xlnew; table(i,4)=xr;
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 14
Numerical Methods
table(i,5)=ea; mul=fxl*fxu; if mul>0 xl=xr; else xu=xr; endend
disp(num2str(table))
Machine Problem 3.4
Code 3.4.1 – Pseudocode for Problem 3.4INPUT n,s,hIF R = 0 THEN'Graphical Method Do
DISPLAY iDISPLAY f(i)
IF i = iter THEN EXIT DO ENDIF i = i + inter ENDDOELSEIF R = 1 Then 'False Position MethodZ = 0Do
DISPLAY qDISPLAY xuDISPLAY 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 IF f(xl) * f(xr) < 0 THEN xu = xr ELSE xl = xr ENDIF
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 15
Numerical Methods
IF e <= tol THEN EXIT DO ENDIF q = q + 1ENDDODISPLAY xrENDIFEnd Sub
Function f(x)f = Sin(x) - x ^ 3End Function
Code 3.4.2 – VBA Code for Problem 3.4Function f(x)
f = Sin(x) - x ^ 3End Function
Sub clear()Range("A3:E1000").clearEnd Sub
Sub machineproblem4_bisection()Dim xr As SingleDim e As Single
xu = InputBox("Upper Boundary")xl = InputBox("Lower Boundary")tol = 0.5 * (10 ^ -4)q = 1Z = 0
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 If f(xl) * f(xr) < 0 Then xu = xr
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 16
Numerical Methods
Else xl = xr End If If e <= tol Then Exit Do End If q = q + 1
LoopMsgBox ("The root is " & xr)End Sub
Code 3.4.3 – Mathscript for Problem 3.4xl=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)^3fu=sin(xu)-(xu)^3
if r==0%bisection methodZ = 0while(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 if e<=tol break,endendelseif r==1
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 17
Numerical Methods
%False Position Methodi=1Z = 0while(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,endendelse%graphical methodp=i:interz=sin(p)-p.^3plot(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.5INPUT xu, xl
tol = 1i = 1Z = 0
DO 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
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 18
Numerical Methods
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 + 1LoopEnd Sub
Function f(x)a = x / 12f = ((((a + 1) ^ 72) - 1) / (((a + 1) ^ 72) * a)) - 50End Function
Code 3.4.2 – VBA Code for Problem 3.5Sub machineproblem3_5
Dim xu, xl, xr As DoubleDim T, Z As Double
xu = InputBox("value for the upper limit")xl = InputBox("value for the lower limit")tol = 1i = 1Z = 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 Z = xr If f(xl) * f(xr) < 0 Then xu = xr Else xl = xr
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 19
Numerical Methods
End If If e <= tol Then Exit Do End If i = i + 1LoopEnd Sub
Function f(x)a = x / 12 'used for interest per periodf = ((((a + 1) ^ 72) - 1) / (((a + 1) ^ 72) * a)) - 50End Function
Code 3.5.3 – Mathscript for Problem 3.5xu=input('value for the upper limit')xl=input('value for the lower limit')tol=1i=1Z=0fu=(((((xu/12)+1)^72)-1)/((((xu/12)+1)^72) * (xu/12)))-50fl=(((((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+1end
IV. RESULTS AND INTERPRETATION
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 20
Numerical Methods
MACHINE PROBLEM 1
VBA
MATHSCRIPT
MACHINE PROBLEM NO. 2
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 21
Numerical Methods
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
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 27
Numerical Methods
Machine Problem No.4 – Roots of Equations: Bracketing Methods Page 28