HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
5.4 1 of 22
Problem: (a) determine the roots of f ( x ) = 13 20 x + 19 x 2 3 x 3 graphically. In addition, determine the 1st root of the function using (b) the Bisection Method, (c) the False Position Method. For (b) & (c), assume xl = 1 and xu = 0 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
140 120 100 80 60 40 20
X: -0.4444 Y: -0.09465
0 -20 -2
-1
Using the data curser tool, the apparent roots are: x 0.44 , x 2.0 , x 4.7 .
Ans (a)
hw02soln.docx
HW #02 Due: 02/06/2013 Using the Bisection Method:
Name:
ES 301
Solution Set
5.4 2 of 22
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.
hw02soln.docx
HW #02 Due: 02/06/2013 >> f(xl)*f(xr) ans = -14.3770
Name:
ES 301
Solution Set
5.4 3 of 22
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<5, maxit=50; end %if maxit blank set to 50 if nargin<4, es=0.001; end %if es blank set to 0.001 % bisection iter = 0; xr = xl;
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
5.4 4 of 22
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 <= es || iter >= maxit, break, end end root = xr; The output from this code is: >> bisection_hw(f,-1,0,1) iter xl xu xr 1 -0.5000 2 -0.5000 3 -0.5000 4 -0.5000 5 -0.4688 6 -0.4531 7 -0.4531 8 -0.4492 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: 0.0000 -0.2500 -0.3750 -0.4375 -0.4375 -0.4375 -0.4453 -0.4453 -0.5000 -0.2500 -0.3750 -0.4375 -0.4688 -0.4531 -0.4453 -0.4492
ea 100.0000 100.0000 33.3333 14.2857 6.6667 3.4483 1.7544 0.8696
Ans (b)
x r = xu
f ( xu )( xl xu ) f ( x l ) f ( xu )
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
5.4 5 of 22
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
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
5.4 6 of 22
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)
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
5.17 7 of 22
Problem: You are designing a spherical water tank for a small village. The volume of liquid (3R h ) where V is in [m3] and h and R have in the tank is computed as: V = h 2 3 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:
30 = h 2
(3(3) h )
3 1 3 f (h ) = h + 3h 2 30 3
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
hw02soln.docx
HW #02 Due: 02/06/2013
>> f(hl)*f(hr) ans = 369.8562
Name:
ES 301
Solution Set
5.17 8 of 22
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
hr2
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
hr3
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:
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
5.17 9 of 22
>> 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
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
6.2 10 of 22
Problem: Determine the highest real root of f ( x ) = 2 x 3 11.7 x 2 + 17.7 x 5 (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
45 40 35 30 25 20 15 10 5 0 -5
X: 3.586 Y: 0.2433
0.5
1.5
2.5
3.5
4.5
So graphically, the highest positive root is x r = 3.586
Ans (a)
For the Fixed Point Method, we need to solve the function so that we obtain a form x = g (x ) . 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:
x= 2 x 3 + 11.7 x 2 + 5 17.7
This form of the equation is then converted to an iterative form as follows:
hw02soln.docx
HW #02 Due: 02/06/2013
xi +1 = 2 xi3 + 11.7 xi2 + 5 17.7
Name:
ES 301
Solution Set
6.2 11 of 22
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
Ans (b)
Ans (b)
Ans (b)
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:
xi +1 = x1
f ( xi ) f ( xi )
For this we need to determine the derivative of our function.
f ( x ) = 2 x 3 11.7 x 2 + 17.7 x 5 f ( x ) = 6 x 2 23.4 x + 17.7
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
6.2 12 of 22
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)
hw02soln.docx
HW #02 Due: 02/06/2013 >> f(ans) ans = 1.5767e-011
Name:
ES 301
Solution Set
6.2 13 of 22
This is essentially zero. Good results.
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
6.4 14 of 22
Problem: Determine the real roots of f ( x ) = 0.5 x 3 4 x 2 + 5.5 x 1 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
160 140 120 100 80 60 40 20 0 -20
X: 6.263 Y: -0.6259
10
The estimated roots are: x r1 = 0.202
x r 2 = 1.515
x r 3 = 6.263
Ans (a)
To use the Newton-Raphson Method, we need to make three initial guesses (one for each root). The derivative of the function is:
f ( x ) = 1.5 x 2 8 x + 5.5
The following Matlab interactive sessions find the roots:
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
6.4 15 of 22
>> 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
Ans (b)
(4 iterations to meet the stopping criteria.)
>> x=1.5; >> xn=x-f(x)/df(x)
hw02soln.docx
HW #02 Due: 02/06/2013
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
Name:
ES 301
Solution Set
6.4 16 of 22
Ans (b)
Ans (b)
hw02soln.docx
HW #02 Due: 02/06/2013
ea = 2.3956e-006
Name:
ES 301
Solution Set
6.4 17 of 22
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
6.30 18 of 22
Problem: You are designing a spherical water tank for a small village. The volume of liquid (3R h ) where V is in [m3] and h and R use in the tank is computed as: V = h 2 3 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:
1 f (h ) = h 3 + 3h 2 30 3 f (h ) = h 2 + 6h
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)
hw02soln.docx
HW #02 Due: 02/06/2013
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
Name:
ES 301
Solution Set
6.30 19 of 22
Ans
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
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
7.15 20 of 22
Problem: Use Matlab to determine the roots of (a)
f ( x ) = 0.7 x 3 4 x 2 + 6.2 x 2 (b)
f ( x ) = 3.704 x 3 + 16.3 x 2 21.97 x + 9.34 (c) f ( x ) = x 4 2 x 3 + 6 x 2 2 x + 5 .
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 (b) Ans (a)
Ans (c)
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
7.17 21 of 22
Problem: When trying to find the acidity of a solution of magnesium hydroxide in hydrochloric acid, we obtain the following equation:
A( x ) = x 3 + 3.5 x 2 40
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. accomplishes this:
>> x=linspace(0,5); >> A=x.^3+3.5.*x.^2-40; >> plot(x,A),grid
200
The following command interface session
150
100
50
X: 2.576 Y: 0.3098
-50
0.5
1.5
2.5
3.5
4.5
Results in a concentration of ~ 2.5
Ans
hw02soln.docx
HW #02 Due: 02/06/2013
Name:
ES 301
Solution Set
7.17 22 of 22
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
Ans
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 xterm.
hw02soln.docx