0% found this document useful (0 votes)
2 views3 pages

Chapter3 Prob18

The document describes a MATLAB function named Ln(p) that calculates the natural logarithm of a positive number p using the bisection method. It outlines the conditions for input values, the stopping criteria for iterations, and error handling for invalid inputs. Example outputs for various inputs, including error messages for negative values, are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Chapter3 Prob18

The document describes a MATLAB function named Ln(p) that calculates the natural logarithm of a positive number p using the bisection method. It outlines the conditions for input values, the stopping criteria for iterations, and error handling for invalid inputs. Example outputs for various inputs, including error messages for negative values, are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

3.18 Determining the natural logarithm of a number p, ln p , is the same as finding a solution to the equa-
tion f ( x ) = e x – p = 0 . Write a MATLAB user-defined function that determines the natural logarithm of a
number by solving the equation using the bisection method. Name the function X = Ln(p). The output argu-
ment X is the value of ln p , and the input argument p is the number whose natural logarithm is determined.
The program should include the following features:
• The starting values of a and b (see Section 3.3) are a = e 0 and b = p , respectively, if b > e 1 , and
a = – 1 ⁄ p and b = e 0 , respectively, if b < e 1 .
• The iterations should stop when the tolerance (Eq. (3.7)) is smaller than 1 × 10 –6 .
• The number of iterations should be limited to 100. If a solution is not obtained in 100 iterations, the
function stops and displays an error message.
• If zero or a negative number is entered for p, the program stops and displays an error message.
Use the function Ln to determine the natural logarithm of (a) 510, (b) 1.35, (c) 1, and (c) – 7 .
Solution

function X = Ln(p)
% Input variables:
% p The number whose square root is determined.
% Output variable:
% Xs The square root of p.
F=@ (x) exp(x)-p;
if p < 0 | p ==0
X='***ERROR*** Argument must be a positive number';
else
if p>exp(1)
a=exp(0); b=p;
else
a=-1/p; b=exp(0);
end
imax=100;
for i = 1:imax
xs = (a + b)/2;

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2

toli = (b - a)/2;
Fxs = F(xs);
if Fxs == 0 | toli < 0.0000001
X=xs;
break
end
if i == imax
X='Solution was not obtained in 100 iterations';
break
end
if F(a)*Fxs < 0
b = xs;
else
a = xs;
end
end
end

Command Window:

>> % (a)
>> Ln(510)
ans =
6.2344
>> % (b)
>> Ln(1.35)
ans =
0.3001
>> % (c)
>> Ln(1)
ans =
0
>> % (d)
>> Ln(-7)

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3

ans =
***ERROR*** Argument must be a positive number
>>

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.

You might also like