MATH 373 Homework Solutions

MATH 373 Homework Solutions
Section 01, Fall 2016
October 26, 2016
2
Contents
1 Homework 2
1.1 Problem 1
1.2 Problem 2
1.3 Problem 3
1.4 Problem 4
1.5 Problem 5
1.6 Problem 6
1.7 Problem 7
1.8 Problem 8
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5
5
8
12
12
16
20
27
27
4
Chapter 1
Homework 2
MATH 373 Numerical Analysis
Homework #2
Assigned: September 19, 2016
Due: September 26, 2016
Problems 5 and 6 are due from this homework set. Include any code in your solution that you
wrote to solve the problems. No other problems are due. If your name is attached to a problem,
then you are responsible for typesetting the solution for that problem in LATEX. The solution is
due by the due date. LATEX code should be submitted through the Submit It! feature on the
MCS website.
1.1
Problem 1
1. [Bauske, L.] Develop an M-file for the Newton-Raphson method. Test it by solving Problem
6.3.
5
Solution: I was asked to develop an M-file for the Newton-Raphson method and test it by
solving Problem 6.3. Determine the highest real root of f (x) = x3 − 6x2 + 11x − 6.1 using
three iterations and Xguess = 3.5.
(a) The Newton-Raphson method was the method I had to use to solve Problem 6.3. The
code was run from the command:
[ X_root, num_iterations, func_val, E_approx ] = Newton_Raphson( 3.5,
@(x) x^3-6*x^2+11*x-6.1, @(x) 3*x^2-12*x+11, 0.0001, 3 )
This method used the function given to find the root at Xroot = 3.0473. On top of
the solution of the root, I attained a number of iterations, numiterations = 3, a function
value of f uncval = 0.0015, and an approximate error Eapprox = 0.7017%. The NewtonRaphson method code can be found in Listing 1.1.
Listing 1.1: MATLAB code for Newton-Raphson Method
function [ X root , n u m i t e r a t i o n s , f u n c v a l , E approx ] = Newton Raphson ( X gues
func , d f u n c , E d e s i r e d , m a x i t e r a t i o n s )
%
Newton−Raphson Method
%
September 26 , 2016
%
Luke Bauske
%
%
%
%
%
This method t a k e s t h e X g u e s s and makes a t a n g e n t l i n e o f f t h a t
l o c a t i o n t h r o u g h t h e x−a x i s , g e t s a new X g u e s s from t h a t o p e r a t i o n ,
and t h e n makes a n o t h e r t a n g e n t l i n e a t t h e new g u e s s . This s h o u l d
c o n t i n u e t o happen and w i l l c l o s e r t o t h e X r o o t t h a t i s t r y i n g t o be
found .
%
%
%
%
%
%
Inputs :
X guess − users f i r s t guess of the l o c a t i o n f o r the root
func − the function t h a t the user inputs to f i n d the root of
d f u n c − d e r i v a t i v e o f t h e f u n c used f o r t h e t a n g e n t o f t h e f u n c t i o n
E desired − desired error
m a x i t e r a t i o n s − maximum number o f i t e r a t i o n s we want t o run
%
%
%
%
%
Outputs :
X r o o t − l o c a t i o n t h a t t h e f u n c t i o n c r o s s e s t h e x−a x i s
n u m i t e r a t i o n s − number o f t i m e s i t t o o k t o g e t t o t h e r o o t
func val − the value of the function at the root
E approx − t h e a p p r o x i m a t e amount o f e r r o r
%
%
%
%
%
Test I n p u t 1 ( Book Example )
X guess = 5
f u n c = @( x ) x ˆ(2) −9
d f u n c = @( x ) 2∗ x
E desired = 0.0001
6
%
m a x i t e r a t i o n s = 50
%
I n i t i a l i z e Values
num iterations = 0;
E approx = 1 0 0 ;
%
Run a While Loop t o k e e p g e t t i n g c l o s e r t o t h e a c t u a l X r o o t
while ( n u m i t e r a t i o n s < m a x i t e r a t i o n s ) && ( E approx > E d e s i r e d )
num iterations = num iterations + 1;
X old=X g u e s s ;
%
G e t t i n g t h e new X g u e s s
X guess = X guess − ( ( func ( X guess )/ d func ( X guess ) ) ) ;
%
C a l c u l a t e the approximate error in r e l a t i o n to the l a s t X guess
i f ( X g u e s s ˜=0)
E approx = abs ( ( X g u e s s − X old ) / ( X g u e s s ) ) ∗ 1 0 0 ;
end
%
End o f t h e I f s t a t e m e n t
end
%
End o f t h e While Loop
X root = X guess ;
% F i n a l i z e t h e X r o o t v a l u e a f t e r t h e While Loop
f u n c v a l = func ( X root ) ;
end
7
1.2
Problem 2
2. [Bollinger-Brown, G.] Problem 6.6
8
Solution: Initially, an M-file to execute the secant method was written. the file takes the
function being evaluated, two guesses, and optionally the desired approximate error and a
maximum number of iterations. It returns the estimated root, the value of the function at
the estimate, the approximate error and the number of iterations.
The secant method was then used to estimate the highest real root of f (x) = x3 − 6x2 + 11x −
6.1. The function was set using the command
func =@(x) x^3-6*x^2+11*x-6.1
The function was then passed to the secant method with the x value guesses 99 and 110, with
a desired approximate error of .0002%, and a maximum of 100 iterations using the command
[root, fx, err_appr, iter] = secant(func, 99, 110, .0002, 100)
The secant method found a root at x = 3.0467. The function value at that point was
1.4852 × 10−10 and the approximate error was 1.4700 × 10−5 %. These results were found after
22 iterations.
The secant method was used again to verify the solution, this time with the guesses of 50
and 60, a desired approximate error of .0001%, and a maximum of 200 iterations using the
command
[root, fx, err_appr, iter] = secant(func, 50, 60, .0001, 200)
The secant method found a root at x = 3.0467. The function value at that point was
9.7877 × 10−13 and the approximate error was 16.6263 × 10−7 %. These results were found
after 20 iterations.
Listing 1.2: M-file for executing the secant method.
function [ r o o t , fx , e r r a p p r , i t e r ] = s e c a n t ( func , x 1 , x 2 , e r r d e s , m a x i t e r ,
% S e c a n t Method
% September 22 , 2016
% Gavin B o l l i n g e r −Brown
%
%
%
%
Performs an open s e a r c h u s i n g t h e s e c a n t method t o e s t i m a t e t h e r o o t o f a
f u n c t i o n u s i n g a s e c a n t l i n e c r e a t e d by u s i n g two x v a l u e s from t h e u s e r .
Outputs t h e e s t i m a t e d r o o t , t h e f u n c t i o n a t t h a t p o i n t , t h e a p p r o x i m a t e
e r r o r , and t h e number o f i t e r a t i o n s .
% Inputs :
%
f u n c = name o f f u n c t i o n
%
x 1 = f i r s t guess for x
%
x 2 = second g u e s s f o r x
%
e r r d e s = d e s i r e d approximate error
%
m a x i t e r = maxiumum number o f i t e r a t i o n s
% Outputs :
9
%
%
%
%
root = estimated root
fx = function value at root estimate
err appr = approximate error
i t e r = number o f i t e r a t i o n s c o m p l e t e d
% Test i n p u t s 1
%
f u n c = @( x ) ( x +1)ˆ2
%
[ r o o t , f x , e r r a p p r , i t e r ] = s e c a n t ( func , 90 , 100 , . 0 0 0 2 , 100)
% Results :
%
r o o t = −1.0000
%
f x = 5 . 8 5 4 3 e−12
%
e r r a p p r = 1 . 4 9 5 4 e−04
%
i t e r = 36
% Test i n p u t s 2 ( Problem 6 . 3 p . 178)
%
f u n c = @( x ) xˆ3−6∗x ˆ2+11∗x −6.
%
[ r o o t , f x , e r r a p p r , i t e r ] = s e c a n t ( func , 99 , 110 , . 0 0 0 2 , 100)
% Results :
%
root = 3.0467
%
f x = 1 . 4 8 5 2 e−10
%
e r r a p p r = 1 . 4 7 0 0 e−05
%
i t e r = 22
% c h e c k t o s e e which v a l u e s have been e n t e r e d
i f ( nargin < 3 )
error ( ’ At l e a s t 3 arguments r e q u i r e d . ’ )
end
i f ( nargin < 4 )
err des = .0001;
end
i f ( nargin < 5 )
max iter = 50;
end
%I n i t i a l i z e v a l u e s
iter = 0;
err appr = 100;
% C a l c u l a t e t h e v a l u e o f x a t t h e p o i n t where t h e s e c a n t l i n e i n t e r s e c t s
% t h e x a x i s , u n t i l t h e maximum number o f i t e r a t i o n s has been r e a c h e d or
% the approximate error i s below the d e s i r e d error .
while ( ( i t e r < m a x i t e r ) && ( e r r a p p r > e r r d e s ) )
x 3 = x 2;
x 2 = x 1;
10
iter = iter + 1;
x 1 = x 2 − ( f u n c ( x 2 , v a r a r g i n { : } ) ∗ ( x 3−x 2 ) ) / ( f u n c ( x 3 , v a r a r g i n {:}) − f u n c
i f ( x 1 ˜= 0 )
e r r a p p r = abs ( ( x 1 − x 2 ) / x 1 ) ∗ 1 0 0 ;
end
end
root = x 1 ;
fx = func ( root , varargin { : } ) ;
11
1.3
Problem 3
3. [Bonebright, S.] Problem 6.7
No solution was provided.
1.4
Problem 4
4. [Brungardt, S.] Develop an M-file for the Quadratic Inverse Interpolation method. Test it by
solving Problem 6.3. Assume that the method won’t do anything strange and that the three
points will remain distinct.
12
Solution: Code was developed for Quadratic Inverse Interpolation. The code in listing one
was used to determine the highest real root of
f (x) = x3 − 6x2 + 11x − 6.1
The code was run with the following commands
func = @(x) x.^3 - 6.*x.^2 + 11.*x - 6.1
[Xroot, E_approx , num_iteration , func_val] =...
inverse_quadratic_interpolation...
(func,2.5,3.5,4.5,.0001,20)
The method found a root at x = 3.0467. The function value at the root was 2.2204 × 10−13 ,
the approximate error was εapprox = 2.2150 × 10−6 , and the number of iterations was 11.
MATLAB Code is displayed below.
Listing 1.3: MATLAB code for Quadratic Inverse Interpolation.
function [ Xroot , E approx , n u m i t e r a t i o n , f u n c v a l ] = . . .
inverse quadratic interpolation . . .
( func , X 1 , X 2 , X 3 , E d e s i r e d , m a x i t e r a t i o n s )
% Inverse Quadratic I n t e r p o l a t i o n , Section 6 . 4 . 1
% September 21 , 2016
% S k y l e r Brungardt
%
%
%
%
%
%
%
%
%
Performs an r o o t l o c a t i o n u s i n g an open method . I n v e r s e Q u a d r a t i c
I n t e r p o l a t i o n i s s i m i l a r t o t h e s e c a n t method . In IQI 3 p o i n t a r e i n p u t .
A q u a d a r t i c e q u a t i o n can be formed u s i n g t h r e e p o i n t , b u t a q u a d r a t i c
d o e s not a l w a y s c r o s s t h e X−a x i s . Because o f t h e t h i s t h e p a r a b o l a i s
i n v e r t e d and formed i n terms o f t h e Y c o o r d i n a t e and t h e p a r a b o l a i s
s i d e w a y s . t h i s makes s u r e t o c r o s s t h e X−a x i s . The p o i n t were t h e
p a r a b o l a c r o s s e s t h e a x i s i s t h e n e x t p o i n t used t o c r e a t e t h e n e x t
p a r a b o l a . This r e p e a t s u n t i l t h e d e s i r e d a c c u r a c y or max i t e r a t i o n s i s
reached .
%
%
%
%
%
%
%
Inputs :
f u n c − Function o f i n t e r e s t
m a x i t e r a t i o n − Max t i m e s t o run s e a r c h
E desired − Presicion required
Xpoint1 − P o i n t 1 on t h e p o r a b o l a
Xpoint2 − P o i n t 2 on t h e p o r a b o l a
Xpoint3 − P o i n t 3 on t h e p o r a b o l a
% Outputs :
% Xroot − Located r o o t s
% E approx − Approximate e r r o r
13
% n u m i t e r a t i o n − Number o f i t e r a t i o n s run
% f u n c ( r o o t ) − Function v a l u e o f t h e r o o t l o c a t i o n f o r p r e s i c i o n
%
%
%
%
%
Test i n p u t s 1 ( Kepler ’ s E q u a t i o n )
e = 0.048; % eccentricity for Jupiter
M = pi /4;
% mean anomaly
f u n c = @(E) E − e ∗ s i n (E) − M;
[ Xroot , E approx , n u m i t e r a t i o n , f u n c v a l ] = . . .
%i n v e r s e q u a d r a t i c i n t e r p o l a t i o n ( func , . 7 , . 7 5 , . 8 , . 0 0 0 0 1 , 2 0 )
% Check t o s e e i f m a x i t e r a t i o n s and E d e s i r e d have been e n t e r e d
i f ( nargin < 4 )
error ( ’ At l e a s t f o u r arguments r e q u i r e d . ’ ) ;
end
i f ( nargin < 5 )
E desired =.001;
end
i f ( nargin <6)
m a x i t e r a t i o n s =50;
end
% Inverse Quadratic I n t e r p o l a t i o n
% I n i t i a l i z e values
num iteration = 0;
E approx = 1 0 0 ;
Xroot = 0 ;
while ( ( n u m i t e r a t i o n < m a x i t e r a t i o n s )&&(E approx > E d e s i r e d ) ) ;
num iteration = num iteration + 1;
X r o o t o l d = Xroot ;
%
Y
Y
Y
Find Y v a l u e s a t X p o i n t s
1 = func ( X 1 ) ;
2 = func ( X 2 ) ;
3 = func ( X 3 ) ;
% Find new r o o t
Xroot = ( ( ( Y 2∗Y 1 ) / ( ( Y 3−Y 2 ) ∗ ( Y 3−Y 1 ) ) ) ∗ X 3 ) + . . .
( ( ( Y 3∗Y 1 ) / ( ( Y 2−Y 3 ) ∗ ( Y 2−Y 1 ) ) ) ∗ X 2 ) + . . .
( ( ( Y 3∗Y 2 ) / ( ( Y 1−Y 3 ) ∗ ( Y 1−Y 2 ) ) ) ∗ X 1 ) ;
i f ( Xroot ˜= 0 )
E approx = abs ( ( Xroot−X r o o t o l d ) / Xroot ) ∗ 1 0 0 ;
14
end
X 3 = X 2;
X 2 = X 1;
X 1 = Xroot ;
end
% Outputs
f u n c v a l= f u n c ( Xroot ) ;
end
15
1.5
Problem 5
5. [Lee, O.] Problem 6.12
Your solution should include a plot of the function to go along with your explanation. Plot
both the polynomial and the tangent line for the first guess on the same plot. The MATLAB
commands fplot, hold on, and hold off will be helpful. To include an image in LATEX, the
best approach is to save the image as a .pdf in the folder where you place your LATEX code and
use the command \includegraphics. You will need to trim the image. See the LATEXcode
in this file for commands to do this.
16
function [ x r o o t , e a p p r o x , n u m i t e r a t i o n , f u n c v a l ] = . . .
n e w t o n r a p h s o n g r a p h ( func , d f u n c , x 1 , e r r , m a x i t e r a t i o n s )
% Newton Raphson , S e c t i o n
% September 22 , 2016
% Orrenzo Lee
%t h i s program t a k e s a known f u n c t i o n and f i n d s t h e t a n g e n t l i n e o f t h e
% known f u n c t i o n ( t h r o u g h d e r i v i t i v e s ) and f i n d s t h e r o o t o f t h e t a n g e n t
% l i n e . I t h e n f i n d p l u g s t h a t r o o t b a c k i n t o t h e known f u n c t i o n and f i n d s
%t h e t a n g e n t l i n e a t t h e new p o i n t i t c o n t i n u e s t h i s p r o c e s s u n t i l a r o o t
%o f t h e known f u n c t i o n i s found .
%
%
%
%
%
Inputs :
f u n c − known f u n c t i o n
m a x i t e r a t i o n − Max t i m e s t o run program
err − Presicion desired
d f u n c − d e r i v i t i v e o f know f u n c t i o n
%
%
%
%
%
Outputs :
x r o o t − Located r o o t
e a p p r o x − Approximate e r r o r
n u m i t e r a t i o n − Number o f i t e r a t i o n s run
f u n c v a l − Function v a l u e o f t h e r o o t l o c a t i o n f o r p r e s i c i o n
% input check
i f ( nargin < 4 )
error ( ’ At l e a s t f o u r arguments r e q u i r e d . ’ ) ;
end
i f ( nargin < 5 )
m a x i t e r a t i o n s =50;
end
% Newton−Raphson Method
% I n i t i a l i z e values
num iteration = 0;
e approx = 100;
x r o o t=x 1 ;
% Plot the function
f p l o t ( func , [ − 1 , 2 0 ] )
t i t l e ( ’ Visual representation ’ )
17
xlabel ( ’ Values o f X ’ )
ylabel ( ’ Values o f Y ’ )
grid
hold on
while ( ( n u m i t e r a t i o n < m a x i t e r a t i o n s )&&( e a p p r o x > e r r ) ) ;
pause
% Function c r e a t e d by s l o p e
f u n c s l o p e = @( x ) d f u n c ( x r o o t ) ∗ ( x−x r o o t ) + f u n c ( x r o o t ) ;
% P l o t f u n c t i o n c r e a t e d by p o i n t
fplot ( func slope ,[ −1 ,20])
x root old = x root ;
x root = x root − ( func ( x root ) / d func ( x root ) ) ;
num iteration = num iteration + 1;
i f ( x r o o t ˜= 0 )
e a p p r o x = abs ( ( x r o o t −x r o o t o l d ) / x r o o t ) ∗ 1 0 0 ;
end
end
% Output
f u n c v a l= f u n c ( x r o o t ) ;
hold o f f
end
18
Using Newton Raphson method I found the root to be 0.4685 and the graph above to go with
it using the fplot command in MatLab. But as you can see the graph is a tangent line for
a different root. In this code if you change the signs the tangent line changes for the.This
problem was interesting because it the orginal function was a polynomial to the 4th power
there were 4 roots. Depending on what you input for X 1 the Newton Raphson above will
output a different root.
19
1.6
Problem 6
6. [Liske, J.] A 2.54 cm pipe is to span a 25 m section of a river while submerged in the river.
Flow of the river past the pipe will create a drag force on the pipe. A structural analysis of
this length of pipe indicated that it can withstand a maximum force of 150 N. Determine the
maximum velocity of the water that the pipe can withstand to four significant figures. The
drag force on the pipe is given by
ρV 2
FD = CD
,
2
where ρ is the density of the water (1000 kg/m3 ), V is the average velocity of the water (m/s),
and the drag coefficient, CD , is given by the correlation CD = 0.193Re0.681 P r1/3 , where P r is
the Prandtl number, which is 14.0 for water, and Re is the Reynolds number, which is given
by ρDV /µ. In this situation the pipe diameter is D = 2.54 cm, and the viscosity of the water
is µ = 0.02 g/cm-s.
Use three of the open rooting-finding methods developed for this homework to confirm that
the approximated solution is accurate. There is a reason why you will not want to use one of
them. In your solution explain which method this is and why you did not use it. Compare
the results in a table like the one in Table 1.1. Report the information that the book outputs
for the various functions (see Figures 5.4 and 5.7 for examples). While this particular
problem could be done without sub-functions, I am assigning this problem so
that you practice using sub-functions. Do not hard code functions or constants
in order to solve this problem. Use robust programming techniques to write a
function that includes CD and Re.
Table 1.1: A sample table for reporting your answers.
Newton-Raphson
Secant
Modified Secant
Inverse Quadratic
xroot
2.3444
2.3444
2.3445
2.3456
f (xroot )
0.001
2.367 × 10−6
εapprox
Iterations
1.23 × 10−17
20
4
20
Solution: The Newton-Raphson, Secant and Modified Secant methods were used to find the
solution for this problem. The functions from the problem statement were converted into
MATLAB code and substituted into the FD equation. The following script was run to use
these functions with the root search methods. The code for the Newton, Secant and Modified
Secant methods can be found in Listings 1.4, 1.5 and 1.6 respectively. The output of the
script has been formatted and placed in Table 1.2. The velocity of the river, xroot , is shown
to be .07701m/s with all three methods.
format (’long’); %more sigfigs
d_pipe = .0254; %(m)
river_span = 25; %(m)
max_force_drag = 150; %(N)
rho_water = 1000; %(kg/m^3)
prandtl = 14.0; %const
viscosity = .002; %kg/(m*s)
%provided functions
ReynoldsF = @(vel_river)rho_water*d_pipe*vel_river/viscosity;
CoeffDragF = @(reynolds)(.193*(reynolds^(.681))*prandtl^(1/3));
RootF = @(vel_river)(CoeffDragF(ReynoldsF(vel_river)))*((rho_water*...
vel_river^2)/2)-max_force_drag;
%error and runaway variables
percent_error = .0001;
max_count = 100;
%Newtons method variables
diff_variable = ’vel_river’;
x_guess = .1;
%secant method variables
x_guess_1 = .2;
x_guess_2 = .1;
%modified secant variables
delta = .0001;
x_guess_mod_sec = .1;
disp(’-----Newton Raphson------’)
[x_root,error_approx,count,funcroot] = ...
NewtonRaphson(RootF,diff_variable,x_guess,max_count,percent_error)
disp(’-----Secant------’)
[x_root,error_approx,count,funcroot] = ...
SecantMethod(RootF,x_guess_1,x_guess_2,max_count,percent_error)
21
disp(’-----Modified Secant------’)
[x_root,error_approx,count,funcroot] = ...
ModifiedSecant(RootF,x_guess_mod_sec,delta,max_count,percent_error)
Table 1.2: Solutions to Problem 6
Newton-Raphson
Secant
Modified Secant
xroot (m/s)
0.07701
0.07701
0.07701
f (xroot )
−5.684 × 10−14
6.148 × 10−11
1.447 × 10−11
εapprox (%)
1.7984 × 10−9
1.341 × 10−6
4.271 × 10−8
Iterations
5
7
5
22
Listing 1.4: MATLAB code for the newton search method.
%Jordan L i s k e
%Numerical A n a l y s i s 2pm, 2016 F a l l
%Newton Raphson Root Method
%−−−−−−−−−−−−−−−−−−−−Function I n p u t s −−−−−−−−−−−−−−−−−−−−%
%f u n c : f u n c t i o n t o f i n d r o o t
%d i f f v a r i a b l e : v a r i a b l e t o d i f f e r e n t i a t e a c c o r d i n g t o
%x g u e s s : i n i t i a l g u e s s f o r x r o o t
%max count : how many t i m e s t o l o o p b e f o r e g i v i n g up
%e r r o r : d e s i r e d a c c u r a c y f o r a n a l y s i s i n %
%Note : t h i s method u s e s t h e d i f f f u n c t i o n t o c a l c u l a t e t h e d e r i v a t i v e .
%The s u b s and e v a l f u n c t i o n s a r e t h e n used t o c a l c u a l t e t h e v a l u e a t
%t h a t p o i n t .
function [ x r o o t , percentChange , count , f u n c r o o t ] = . . .
NewtonRaphson ( func , d i f f v a r i a b l e , x g u e s s , max count , error )
%c r e a t e a d e r i v a t i v e v a r i a b l e
dvar = sym ( d i f f v a r i a b l e ) ;
%s e t e r r o r c h e c k i n g and l o o p c o un t v a r i a b l e s
count = 0 ;
percentChange = 1 0 0 ;
%s e t t h e i n i t i a l g u e s s
x root = x guess ;
while ( ( percentChange > error ) && ( count < max count ) )
%s a v e r o o t b e f o r e c a l c u l a t i n g new r o o t
x root old = x root ;
%c a l c u l a t e new r o o t
x r o o t = x r o o t −( f u n c ( x r o o t ) / eval ( s u b s ( d i f f ( func , dvar ) , . . .
dvar , x r o o t ) ) ) ;
%c a l c u l a t e p e r c e n t e r r o r
i f ( x r o o t ˜=0)
percentChange = abs ( ( x r o o t −x r o o t o l d ) / x r o o t ) ∗ 1 0 0 ;
end
%i n c r e m e n t l o o p c ou n t
count = count +1;
end
23
%c a l c u l a t e f u n c t i o n v a l u e f o r u s e r
f u n c r o o t=f u n c ( x r o o t ) ;
end
Listing 1.5: MATLAB code for the secant search method.
%Jordan L i s k e
%Numerical A n a l y s i s 2pm, 2016 F a l l
%S e c a n t Root Method
%−−−−−−−−−−−−−−−−−−−−Function I n p u t s −−−−−−−−−−−−−−−−−−−−%
%f u n c : f u n c t i o n t o f i n d r o o t
%x g u e s s : two g u e s s e s f o r s e c a n t method
%max count : how many t i m e s t o l o o p b e f o r e g i v i n g up
%e r r o r : d e s i r e d a c c u r a c y f o r a n a l y s i s i n %
function [ x r o o t , percentChange , count , f u n c r o o t ] = SecantMethod ( . . .
func , x g u e s s 1 , x g u e s s 2 , max count , error )
%s e t e r r o r c h e c k i n g and l o o p c o un t v a r i a b l e s
count = 0 ;
percentChange = 1 0 0 ;
%s e t t h e i n i t i a l g u e s s e s
x root = x guess 1 ;
x root old = x guess 2 ;
while ( ( percentChange > error ) && ( count < max count ) )
%C a l c u l a t e new r o o t and s a v e i n temp v a r i a b l e
x r o o t t e m p = x r o o t − ( f u n c ( x r o o t ) ∗ ( x r o o t o l d −x r o o t ) ) . . .
/ ( f u n c ( x r o o t o l d )− f u n c ( x r o o t ) ) ;
%s a v e t h e p r e v i o u s r o o t as o l d
x root old = x root ;
%s e t new r o o t from temp as c u r r e n t r o o t
x root = x root temp ;
%c a l c u l a t e p e r c e n t e r r o r
i f ( x r o o t ˜=0)
percentChange = abs ( ( x r o o t −x r o o t o l d ) / x r o o t ) ∗ 1 0 0 ;
end
%i n c r e m e n t l o o p c ou n t
24
count = count +1;
end
%c a l c u l a t e f u n c t i o n v a l u e f o r u s e r
f u n c r o o t=f u n c ( x r o o t ) ;
end
Listing 1.6: MATLAB code for the modified secant search method.
%Jordan L i s k e
%Numerical A n a l y s i s 2pm, 2016 F a l l
%M o d i f i e d S e c a n t Root Method
%−−−−−−−−−−−−−−−−−−−−Function I n p u t s −−−−−−−−−−−−−−−−−−−−%
%f u n c : f u n c t i o n t o f i n d r o o t
%x g u e s s : two g u e s s f o r m o d i f i e d s e c a n t method
%d e l t a : s m a l l p e r t u r b a t i o n f r a c t i o n
%max count : how many t i m e s t o l o o p b e f o r e g i v i n g up
%e r r o r : d e s i r e d a c c u r a c y f o r a n a l y s i s i n %
function [ x r o o t , percentChange , count , f u n c r o o t ] = M o d i f i e d S e c a n t ( . . .
func , x g u e s s , d e l t a , max count , error )
%s e t e r r o r c h e c k i n g and l o o p c o un t v a r i a b l e s
count = 0 ;
percentChange = 1 0 0 ;
%s e t t h e i n i t i a l g u e s s
x root = x guess ;
while ( ( percentChange > error ) && ( count < max count ) )
%s a v e r o o t b e f o r e c a l c u l a t i n g new r o o t
x root old = x root ;
%c a l c u l a t e new r o o t
x root = x root − ( func ( x root )∗ x root ∗ d e l t a )/( func ( . . .
x r o o t+d e l t a ∗ x r o o t )− f u n c ( x r o o t ) ) ;
%c a l c u l a t e p e r c e n t e r r o r
i f ( x r o o t ˜=0)
percentChange = abs ( ( x r o o t −x r o o t o l d ) / x r o o t ) ∗ 1 0 0 ;
end
%i n c r e m e n t l o o p c ou n t
count = count +1;
end
25
%c a l c u l a t e f u n c t i o n v a l u e f o r u s e r
f u n c r o o t=f u n c ( x r o o t ) ;
end
26
1.7
Problem 7
7. [Schluckebier, J.] Consider a cylindrical pipe oriented with the horizontal plane containing
liquid to a height h from the bottom surface of the inside of the pipe. The volume of liquid
in the horizontal pipe is given by
V = r2 cos−1
r−h
h
p
− (r − h) 2rh − h2 L,
where V is the volume of liquid, r is the radius of the cylinder, h is the height of the liquid
level, and L is the length of the cylindrical pipe. Determine the height of the liquid h to four
significant figures for V = 250 ft3 , r = 60 in, and L = 12 ft. Use multiple rooting-finding
methods (at least one bracketed and one open) to confirm that the approximated solution is
accurate. Report the information that the book outputs for the various functions (see Figures
5.4 and 5.7 for examples).
No solution was provided.
1.8
Problem 8
8. [Shipper, C.] The secant formula, used to calculate the load that a column can withstand
before buckling, is given by

σmax

P
L
= 1 + εr sec 
A
2k
s

P 
,
EA
where σmax is the maximum stress (200,000 MPa/m2 ) for the material used to make the
column, εr is the eccentricity ratio (0.25), E is modulus of elasticity of the material (150,000
MPa), and L/k is the slenderness ratio (30). Determine the load (P/A), which is the force
per unit area (or stress), that satisfies the secant formula to within four significant figures.
Use multiple rooting-finding methods (at least one bracketed and one open) to confirm that
the approximated solution is accurate. Report the information that the book outputs for the
various functions (see Figures 5.4 and 5.7 for examples).
27
Homework 2 - Problem 8
By: Colten Shipper
09/26/2016
Solution: This problem actually has four different roots as can be explained by Figure 1.1.
The graph of the function reveals that the it crosses the x-axis in four different locations due
to the secant function within the maximum stress function.
Plot missing.
Figure 1.1: Plot of the function.
The four root locations were all found using the Bisection method and Modified Secant
method. The code for the Bisection method is located in Listing 1.7 and the code for the
Modified Secant method is located in Listing 1.8. Table 1.3 below compares the answers
between the two methods and displays the values of the stress P/A in GPa.
Table 1.3: Comparing the values found in each method.
Bisection
Modified Secant
Root 1
14.9314
14.9314
Root 2
40.4616
40.4616
Root 3
83.2508
83.2508
Root 4
125.2219
125.2219
Avg. Iterations
16
5.25
Both methods found four roots at 14.9314 GPa, 40.4616 GPa, 83.2508 GPa, and 125.2219
GPa. The average iterations prove that the open method is much faster than the bracketed
method. The Modified Secant method found a root at an average of 5.25 iterations, which
is about three times as fast as the Bisection method with an average of 16 iterations. Both
methods were ran with a default of .0001 as an approximate error and a maximum amount of
iterations of 50. The next sections below show the commands that were used in each method.
Bisection Method:
stress_max = 200; Er = .25; E = 150; slend_ratio = 30;
func = @(stress) (stress)*(1+Er*sec((slend_ratio*(1/2))*sqrt((1/E)*stress))) - stress_max;
[fx,x_root,E_approx,iter] = Problem8_Bisection(func,14.9,15)
[fx,x_root,E_approx,iter] = Problem8_Bisection(func,35,41)
[fx,x_root,E_approx,iter] = Problem8_Bisection(func,81,85)
[fx,x_root,E_approx,iter] = Problem8_Bisection(func,120,130)
Modified Secant Method:
28
stress_max = 200; Er = .25; E = 150; slend_ratio = 30;
func = @(stress) (stress)*(1+Er*sec((slend_ratio*(1/2))*sqrt((1/E)*stress))) - stress_max;
[fx,x_root,E_approx,iter] = Problem8_Mod_Secant(func,14.9,.1)
[fx,x_root,E_approx,iter] = Problem8_Mod_Secant(func,40,.1)
[fx,x_root,E_approx,iter] = Problem8_Mod_Secant(func,83,.1)
[fx,x_root,E_approx,iter] = Problem8_Mod_Secant(func,125,.1)
Listing 1.7: MATLAB Code for the Bisection Method
function [ fx , x r o o t , E approx , i t e r ] = . . .
P r o b l e m 8 B i s e c t i o n ( func , x min , x max , E d e s i r e d , m a x i t e r , v a r a r g i n
% B i s e c t i o n method
% September 24 , 2016
% Colten Shipper
%
%
%
%
%
%
%
%
My Problem
s t r e s s m a x = 2 0 0 ; Er = . 2 5 ; E = 1 5 0 ; s l e n d r a t i o = 3 0 ;
f u n c = @( s t r e s s ) ( s t r e s s )∗(1+ Er∗ s e c ( ( s l e n d r a t i o ∗ ( 1 / 2 ) ) ∗ s q r t ( ( 1 /E)∗ s t r e s s ) ) )
− stress max ;
[ f x , x r o o t , E approx , i t e r ] = P r o b l e m 8 B i s e c t i o n ( func , 1 4 . 9 , 1 5 )
[ f x , x r o o t , E approx , i t e r ] = P r o b l e m 8 B i s e c t i o n ( func , 3 5 , 4 1 )
[ f x , x r o o t , E approx , i t e r ] = P r o b l e m 8 B i s e c t i o n ( func , 8 1 , 8 5 )
[ f x , x r o o t , E approx , i t e r ] = P r o b l e m 8 B i s e c t i o n ( func , 1 2 0 , 1 3 0 )
% Input checks
i f ( nargin < 3 )
error ( ’ At l e a s t t h r e e arguments r e q u i r e d . ’ ) ;
end
t e s t = f u n c ( x min , v a r a r g i n { : } ) ∗ f u n c ( x max , v a r a r g i n { : } ) ;
i f ( t e s t > 0)
error ( ’No s i g n change . ’ ) ;
end
i f ( nargin < 4 | | isempty ( E d e s i r e d ) )
E desired = .0001;
end
i f ( nargin < 5 | | isempty ( m a x i t e r ) )
max iter = 50;
end
% Plot the function
f p l o t ( func , [ 0 , 1 4 0 ] ) ;
grid ;
% I n i t i a l values
29
iter = 0;
x r o o t = x min ;
E approx = 1 0 0 ;
% F i n d i n g t h e r o o t and a p p r o x i m a t e e r r o r
while ( ( i t e r < m a x i t e r ) && ( E approx > E d e s i r e d ) )
iter = iter + 1;
x root old = x root ;
T = f u n c ( x r o o t ) ∗ f u n c ( x min ) ;
i f (T < 0 )
x max = x r o o t ;
e l s e i f (T > 0 )
x min = x r o o t ;
else
E approx = 0 ;
end
x r o o t = ( x min + x max ) / 2 ;
i f ( x r o o t ˜= 0 )
E approx = abs ( ( x r o o t − x r o o t o l d ) / x r o o t ) ∗ 1 0 0 ;
end
end
%D i s p l a y t h e v a l u e o f t h e r o o t i n t h e f u n c t i o n
fx = func ( x root , varargin { : } ) ;
end
Listing 1.8: MATLAB Code for the Modified Secant Method
function [ fx , x r o o t , E approx , i t e r ] = . . .
Problem8 Mod Secant ( func , g u e s s , d e l t a , E d e s i r e d , m a x i t e r , v a r a r g i
% M o d i f i e d S e c a n t method
% September 24 , 2016
% Colten Shipper
%
%
%
%
%
%
%
%
My Problem
s t r e s s m a x = 2 0 0 ; Er = . 2 5 ; E = 1 5 0 ; s l e n d r a t i o = 3 0 ;
f u n c = @( s t r e s s ) ( s t r e s s )∗(1+ Er∗ s e c ( ( s l e n d r a t i o ∗ ( 1 / 2 ) ) ∗ s q r t ( ( 1 /E)∗ s t r e s s ) ) )
− stress max ;
[ f x , x r o o t , E approx , i t e r ] = Problem8 Mod Secant ( func , 1 4 . 9 , . 1 )
[ f x , x r o o t , E approx , i t e r ] = Problem8 Mod Secant ( func , 4 0 , . 1 )
[ f x , x r o o t , E approx , i t e r ] = Problem8 Mod Secant ( func , 8 3 , . 1 )
[ f x , x r o o t , E approx , i t e r ] = Problem8 Mod Secant ( func , 1 2 5 , . 1 )
% Input checks
30
i f ( nargin < 3 )
error ( ’ At l e a s t t h r e e arguments r e q u i r e d . ’ ) ;
end
i f ( nargin < 4 | | isempty ( E d e s i r e d ) )
E desired = .0001;
end
i f ( nargin < 5 | | isempty ( m a x i t e r ) )
max iter = 50;
end
% Plot the function
f p l o t ( func , [ 0 , 1 4 0 ] ) ;
grid ;
% I n i t i a l values
iter = 0;
E approx = 1 0 0 ;
% Save t h e g u e s s as t h e f i r s t p o i n t and c a l c u l a t e t h e second
x1 = g u e s s ;
x2 = g u e s s + d e l t a ;
% Keep l o o p i n g w h i l e t h e e r r o r and i t e r a t i o n s a r e t o o h i g h
while ( ( i t e r < m a x i t e r ) && ( E approx > E d e s i r e d ) )
x r o o t = x1 − ( ( f u n c ( x1 ) ∗ ( x2−x1 ) ) / ( f u n c ( x2)− f u n c ( x1 ) ) ) ;
i f ( x r o o t ˜= 0 )
E approx = abs ( ( x r o o t − x1 ) / x r o o t ) ∗ 1 0 0 ;
end
% Update t h e number o f i t e r a t i o n s
iter = iter + 1;
% Make t h e new r o o t t h e o l d r o o t and t h e o r i g i n a l o l d r o o t t h e second o l d r
x2 = x1 ;
x1 = x r o o t ;
end
% Display the value of the root in the function
fx = func ( x root , varargin { : } ) ;
end
31