hw02soln
DESCRIPTION
NUMERICAL METHODTRANSCRIPT
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.4
1 of 22
hw02soln.docx
Problem: (a) determine the roots of ( ) 32 3192013 xxxxf += graphically. In addition, determine the 1st root of the function using (b) the Bisection Method, (c) the False Position Method. For (b) & (c), assume 1=lx and 0=ux and use a stopping criteria of 1%.
Solution:
The following interactive Matlab session results in the plot shown:
>> x=linspace(-2,5); >> f=-3.*x.^3+19.*x.^2-20.*x-13; >> plot(x,f),grid
Using the data curser tool, the apparent roots are: 44.0x , 0.2x , 7.4x .
-2 -1 0 1 2 3 4 5-20
0
20
40
60
80
100
120
140
X: -0.4444Y: -0.09465
Ans (a)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.4
2 of 22
hw02soln.docx
Using the Bisection Method:
Ill use Matlab in interactive mode to do the calculations and show/explain the results here.
>> f=inline('-3*x^3+19*x^2-20*x-13','x') f = Inline function: f(x) = -3*x^3+19*x^2-20*x-13
>> xl=-1; xu=0; >> f(xl) ans = 29 >> f(xu) ans = -13
A sign change occurs so a root exists between these values. Bisect the range.
>> xr=(xl+xu)/2 xr = -0.5000
>> f(xl)*f(xr) ans = 61.6250
This is positive, so the root is in the upper interval Therefore, xr becomes xl for the next iteration.
>> xl=xr; >> xr=(xl+xu)/2 xr =
-0.2500
Determine the approximate error.
>> ea=abs((xr-xl)/xr)*100 ea = 100
This is greater than the stopping criteria, so we continue.
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.4
3 of 22
hw02soln.docx
>> f(xl)*f(xr) ans = -14.3770
This is negative, so the root is in the lower interval. xr becomes xu for the next iteration.
>> xu=xr; >> xr=(xl+xu)/2 xr = -0.3750
Determine the approximate error.
>> ea=abs((xr-xu)/xr)*100 ea = 33.3333
This is still greater than the stopping criteria of 1%, so we continue. I modified the bisection function written in class to report the summary of results as seen below:
function root = bisection_hw(func,xl,xu,es,maxit) % root = bisection_hw(func,xl,xu,es,maxit): % uses bisection method to find the root of a function % This is a modification of the generic function to report % the results as request by HW Pr. 5.4(b). % input: % func = name of function % xl, xu = lower and upper guesses % es = (optional) stopping criterion (%) % maxit = (optional) maximum allowable iterations % output: % root = real root
if func(xl)*func(xu)>0 %if guesses do not bracket a sign change disp('no bracket') %display an error message return %and terminate end % if necessary, assign default values if nargin
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.4
4 of 22
hw02soln.docx
disp('iter xl xu xr ea') while (1) xrold = xr; xr = (xl + xu)/2; iter = iter + 1; if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end test = func(xl)*func(xr); if test < 0 xu = xr; elseif test > 0 xl = xr; else ea = 0; end fprintf('\n%3d %8.4f %8.4f %8.4f %8.4f', iter,xl,xu,xr,ea) if ea = maxit, break, end end root = xr;
The output from this code is:
>> bisection_hw(f,-1,0,1) iter xl xu xr ea
1 -0.5000 0.0000 -0.5000 100.0000 2 -0.5000 -0.2500 -0.2500 100.0000 3 -0.5000 -0.3750 -0.3750 33.3333 4 -0.5000 -0.4375 -0.4375 14.2857 5 -0.4688 -0.4375 -0.4688 6.6667 6 -0.4531 -0.4375 -0.4531 3.4483 7 -0.4531 -0.4453 -0.4453 1.7544 8 -0.4492 -0.4453 -0.4492 0.8696 ans =
-0.4492
Note that the first three lines agree with the manual (by-hand) iterations shown above.
Finally, look at the same problem using the False Position Method.
The general form of this method is:
( )( )( ) ( )ul
uluur
xfxfxxxf
xx
=
Ans (b)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.4
5 of 22
hw02soln.docx
Ill again use an interactive Matlab session to perform the calculations.
>> xu=0;xl=-1; >> xr=xu-f(xu)*(xl-xu)/(f(xl)-f(xu)) xr = -0.3095 >> f(xl)*f(xr) ans = -142.1078
This is negative, so the root is in the lower interval and xr becomes xu for the next iteration.
>> xu=xr; >> xr=xu-f(xu)*(xl-xu)/(f(xl)-f(xu)) xr = -0.4093
Calculate the approximate error.
>> ea=abs((xr-xu)/xr)*100 ea = 24.3832
This is greater than the stopping criteria, so we iterate.
>> f(xl)*f(xr) ans = -41.2992
This is negative, so again, the root is in the lower interval and xr becomes xu for the next iteration.
>> xu=xr; >> xr=xu-f(xu)*(xl-xu)/(f(xl)-f(xu)) xr = -0.4370 >> ea=abs((xr-xu)/xr)*100 ea = 6.3271 >> f(xl)*f(xr) ans = -11.0776
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.4
6 of 22
hw02soln.docx
The error seems to be dropping rapidly, so Ill just continue with the hand calculations. >> xu=xr; >> xr=xu-f(xu)*(xl-xu)/(f(xl)-f(xu)) xr = -0.4443 >> ea=abs((xr-xu)/xr)*100 ea = 1.6475 >> f(xl)*f(xr) ans = -2.9070
>> xu=xr; >> xr=xu-f(xu)*(xl-xu)/(f(xl)-f(xu)) xr = -0.4462 >> ea=abs((xr-xu)/xr)*100 ea = 0.4290
This is less that my 1% stopping criteria, so we stop.
The estimate of the root is -0.4462 after 5 iterations.
Ans (c)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.17
7 of 22
hw02soln.docx
Problem: You are designing a spherical water tank for a small village. The volume of liquid in the tank is computed as: ( )
332 hRhV = pi where V is in [m3] and h and R have
units of [m]. If R = 3m, to what depth must the tank be filled so that it holds 30 m3? Use three iterations of the Bisection Method to determine your answer. Use initial guesses of 0 and R and determine your approximate error after each iteration. Check your answer using the bisection function developed for Pr. 5.4.
Solution:
Plugging in the given information, the function becomes:
( )( )
( ) 303313
3330
23
2
+=
=
hhhf
hh
pipi
pi
Ill use a command session in Matlab to perform the manual calculations required for the 1st three iterations to find h. As the problem suggests, Ill start with initial guess of hl=0 & hu=3.
>> f=inline('-1/3*pi*h^3+3*pi*h^2-30','h') f = Inline function: f(h) = -1/3*pi*h^3+3*pi*h^2-30 >> hl=0;hu=3; >> hr=(hl+hu)/2 hr = 1.5000 hr1
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.17
8 of 22
hw02soln.docx
>> f(hl)*f(hr) ans = 369.8562
This is positive, so the root is in the upper interval and we set hl = hr for the next iteration.
>> hl=hr; >> hr=(hl+hu)/2 hr = 2.2500 >> ea=abs((hr-hl)/hr)*100 ea = 33.3333
This is a fairly large error so well repeat.
>> f(hl)*f(hr) ans = -71.3170
This is negative, so the root is in the lower interval and well set hu = hr for the next iteration.
>> hu=hr; >> hr=(hl+hu)/2 hr = 1.8750 >> ea=abs((hr-hl)/hr)*100 ea = 20
Still a large error. However, the problem only asked for the 1st three iterations. I ran this function to a stopping criteria of 1% using the edited function from the last problem and the table summarizing the results is shown below:
hr2
hr3
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 5.17
9 of 22
hw02soln.docx
>> bisection_hw(f,0,3,1) iter xl xu xr ea 1 1.5000 3.0000 1.5000 100.0000 2 1.5000 2.2500 2.2500 33.3333 3 1.8750 2.2500 1.8750 20.0000 4 1.8750 2.0625 2.0625 9.0909 5 1.9688 2.0625 1.9688 4.7619 6 2.0156 2.0625 2.0156 2.3256 7 2.0156 2.0391 2.0391 1.1494 8 2.0156 2.0273 2.0273 0.5780 ans =
2.0273
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.2
10 of 22
hw02soln.docx
Problem: Determine the highest real root of ( ) 57.177.112 23 += xxxxf (a) graphically, (b) using fixed point iteration (3 iterations w/x0=3), (c) The Newton-Raphson Method (3 iterations w/x0=3).
Solution:
Using Matlab, the following plot was prepared:
>> x=linspace(0,5); >> f=2.*x.^3-11.7.*x.^2+17.7.*x-5; >> plot(x,f),grid
So graphically, the highest positive root is 586.3=rx
For the Fixed Point Method, we need to solve the function so that we obtain a form ( )xgx = . There are many ways to do this. But the simplest way is to move the 17.7x term to the left of the equal sign and then divide thru by 17.7 to obtain:
7.1757.112 23 ++
=
xxx
This form of the equation is then converted to an iterative form as follows:
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-5
0
5
10
15
20
25
30
35
40
45
X: 3.586Y: 0.2433
Ans (a)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.2
11 of 22
hw02soln.docx
7.1757.112 23
1++
=+ii
ixx
x
Ill use Matlab in interactive mode to perform the iterations starting with x = 3 @ i = 0.
>> x=3; >> xn=(-2*x^3+11.7*x^2+5)/17.7 xn =
3.1808
>> ea=abs((xn-x)/xn)*100 ea = 5.6838 >> x=xn; >> xn=(-2*x^3+11.7*x^2+5)/17.7 xn =
3.3340
>> ea=abs((xn-x)/xn)*100 ea = 4.5942 >> x=xn; >> xn=(-2*x^3+11.7*x^2+5)/17.7 xn =
3.4425
>> ea=abs((xn-x)/xn)*100 ea = 3.1542
This is still more error than would normally be acceptable. However, the answer is converging on the highest root.
The Newton-Raphson Method calls for us to solve the iterative equation:
( )( )i
ii
xfxf
xx
=+ 11
For this we need to determine the derivative of our function.
( ) 57.177.112 23 += xxxxf ( ) 7.174.236 2 += xxxf
Ans (b)
Ans (b)
Ans (b)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.2
12 of 22
hw02soln.docx
An interactive Matlab session to perform the calculations shows:
>> f=inline('2*x^3-11.7*x^2+17.7*x-5','x') f = Inline function: f(x) = 2*x^3-11.7*x^2+17.7*x-5
>> df=inline('6*x^2-23.4*x+17.7','x') df = Inline function: df(x) = 6*x^2-23.4*x+17.7
>> x=3; >> xn=x-f(x)/df(x) xn = 5.1333
>> ea=abs((xn-x)/xn)*100 ea = 41.5584 >> x=xn; >> xn=x-f(x)/df(x) xn = 4.2698
>> ea=abs((xn-x)/xn)*100 ea = 20.2256 >> x=xn; >> xn=x-f(x)/df(x) xn = 3.7929
>> ea=abs((xn-x)/xn)*100 ea = 12.5712
This is still a large error. Running this problem though the newtraph function developed in class reveals the following result at a stopping criteria of 0.001 error.
>> newtraph(f,df,3) ans = 3.5632
Ans (c)
Ans (c)
Ans (c)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.2
13 of 22
hw02soln.docx
>> f(ans) ans = 1.5767e-011
This is essentially zero. Good results.
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.4
14 of 22
hw02soln.docx
Problem: Determine the real roots of ( ) 15.545.0 23 += xxxxf to within 0.01% (a) graphically, (b) using the Newton-Raphson Method.
Solution:
Using Matlab, the graphical solution is:
>> x=linspace(0,10); >> f=0.5.*x.^3-4.*x.^2+5.5.*x-1; >> plot(x,f),grid
The estimated roots are: 202.01 =rx 515.12 =rx 263.63 =rx
To use the Newton-Raphson Method, we need to make three initial guesses (one for each root). The derivative of the function is:
( ) 5.585.1 2 += xxxf
The following Matlab interactive sessions find the roots:
0 1 2 3 4 5 6 7 8 9 10-20
0
20
40
60
80
100
120
140
160
X: 6.263Y: -0.6259
Ans (a)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.4
15 of 22
hw02soln.docx
>> f=inline('0.5*x^3-4*x^2+5.5*x-1','x') f = Inline function: f(x) = 0.5*x^3-4*x^2+5.5*x-1 >> df=inline('1.5*x^2-8*x+5.5','x') df = Inline function: df(x) = 1.5*x^2-8*x+5.5
>> x=0; >> xn=x-f(x)/df(x) xn =
0.1818 >> ea=abs((xn-x)/xn)*100 ea = 100
>> x=xn; >> xn=x-f(x)/df(x) xn =
0.2134 >> ea=abs((xn-x)/xn)*100 ea = 14.7893
>> x=xn; >> xn=x-f(x)/df(x) xn =
0.2143 >> ea=abs((xn-x)/xn)*100 ea = 0.4466
>> x=xn; >> xn=x-f(x)/df(x) xn =
0.2143 >> ea=abs((xn-x)/xn)*100 ea = 4.0809e-004
(4 iterations to meet the stopping criteria.)
>> x=1.5; >> xn=x-f(x)/df(x)
Ans (b)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.4
16 of 22
hw02soln.docx
xn =
1.4800 >> ea=abs((xn-x)/xn)*100 ea = 1.3514 >> x=xn; >> xn=x-f(x)/df(x) xn =
1.4798 >> ea=abs((xn-x)/xn)*100 ea = 0.0156 >> x=xn; >> xn=x-f(x)/df(x) xn =
1.4798 >> ea=abs((xn-x)/xn)*100 ea = 2.0929e-006
>> x=6; >> xn=x-f(x)/df(x) xn =
6.3478 >> ea=abs((xn-x)/xn)*100 ea = 5.4795 >> x=xn; >> xn=x-f(x)/df(x) xn =
6.3065 >> ea=abs((xn-x)/xn)*100 ea = 0.6547 >> x=xn; >> xn=x-f(x)/df(x) xn =
6.3059 >> ea=abs((xn-x)/xn)*100 ea = 0.0101 >> x=xn; >> xn=x-f(x)/df(x) xn =
6.3059 >> ea=abs((xn-x)/xn)*100
Ans (b)
Ans (b)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.4
17 of 22
hw02soln.docx
ea = 2.3956e-006
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.30
18 of 22
hw02soln.docx
Problem: You are designing a spherical water tank for a small village. The volume of liquid in the tank is computed as: ( )
332 hRhV = pi where V is in [m3] and h and R use
units of [m]. If R = 3m, to what depth must the tank be filled so that it holds 30 m3? Use three iterations of the Newton-Raphson Method to determine your answer. An initial guess of R will always converge. Check your answer using the newtraph function developed in class.
Solution:
This is a repeat of Prob. 5.17. As we found then, the function reduces to:
( )( ) hhhf
hhhfpipi
pipi
6
30331
2
23
+=
+=
A Matlab interactive session for three iterations follows:
>> f=inline('-1/3*pi*h^3+3*pi*h^2-30','h') f = Inline function: f(h) = -1/3*pi*h^3+3*pi*h^2-30 >> df=inline('-pi*h^2+6*pi*h','h') df = Inline function: df(h) = -pi*h^2+6*pi*h
>> h=3; >> hn=h-f(h)/df(h)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 6.30
19 of 22
hw02soln.docx
hn = 2.0610 >> ea=abs((hn-h)/hn)*100 ea = 45.5581 >> h=hn; >> hn=h-f(h)/df(h) hn = 2.0270 >> ea=abs((hn-h)/hn)*100 ea = 1.6769 >> h=hn; >> hn=h-f(h)/df(h) hn = 2.0269 >> ea=abs((hn-h)/hn)*100 ea = 0.0067
This is fairly good agreement with problem 5.17. Using the newtraph function from class to the default (0.001) stopping criteria yields:
>> newtraph(f,df,3)
ans =
2.0269
Ans
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 7.15
20 of 22
hw02soln.docx
Problem: Use Matlab to determine the roots of (a) ( ) 22.647.0 23 += xxxxf (b)
( ) 34.997.213.16704.3 23 ++= xxxxf (c) ( ) 5262 234 ++= xxxxxf .
Solution:
Ill use the built-in function roots to find these roots. The following interactive session shows the results.
>> a=[0.7 -4 6.2 -2]; >> roots(a)
ans =
3.2786 2.0000 0.4357
>> a=[-3.704 16.3 -21.97 9.34]; >> roots(a)
ans =
2.2947 1.1525 0.9535
>> a=[1 -2 6 -2 5]; >> roots(a)
ans =
1.0000 + 2.0000i 1.0000 - 2.0000i -0.0000 + 1.0000i -0.0000 - 1.0000i
Ans (a)
Ans (b)
Ans (c)
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 7.17
21 of 22
hw02soln.docx
Problem: When trying to find the acidity of a solution of magnesium hydroxide in hydrochloric acid, we obtain the following equation:
( ) 405.3 23 += xxxA
where x is the hydronium ion concentration. Find the hydronium ion concentration for a saturated solution (acidity equals zero) using two different methods in Matlab.
Solution:
1st use the graphical method. The following command interface session accomplishes this:
>> x=linspace(0,5); >> A=x.^3+3.5.*x.^2-40; >> plot(x,A),grid
Results in a concentration of ~ 2.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-50
0
50
100
150
200
X: 2.576Y: 0.3098
Ans
-
HW #02 Due: 02/06/2013
ES 301 Name:
Solution Set 7.17
22 of 22
hw02soln.docx
For the other method, Ill use Matlabs built-in roots function.
>> A=[1 3.5 0 -40]; >> roots(A)
ans =
-3.0338 + 2.5249i -3.0338 - 2.5249i 2.5676
Note that the only real root is 2.5676. Also, the coefficients vector needed to be entered in highest order to lowest order and padded with zero for the missing x-term.
Ans