Cobweb Con Matlab PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Homework 2 solutions

Section 2.1: Ex 1,3,6,11; AP 1


Section 2.2: Ex 3,4,9,12,14

1. Determine if each function has a unique fixed point on the specified interval.
(a) g(x) = 1 – x2/4 on [0,1]. g’(x) = -x/2, so g is continuous and decreasing on [0,1].
g(0) = 1 and g(1) = ¾. Therefore g(x) is in [0,1] for all x in [0,1]. Furthermore,
| g ' ( x ) || x / 2 | 1 / 2  1 for x in [0,1]. The fixed point theorem thus implies the existence
of an unique fixed point in [0,1].

(b) g(x) = 2-x on [0,1]. g’(x) = -log(2) 2-x, so g is continuous and decreasing on [0,1].
g(0)=1 and g(1)=1/2. Therefore g(x) is in [0,1] for all x in [0,1]. And since g is
decreasing, there is a unique fixed point in [0,1].

(c) g(x) = 1/x on [0.5, 5.2]. g(x) = x is 1/x = x, or x2 = 1, which has a single solution (x =
1) in the interval [0.5, 5.2].

3. Graph the function with the line y=x, construct a cobweb diagram, and determine if
the fixed point iteration converges.
(a) g(x) = (6 + x)1/2, P = 3, and po = 7.
(b) g(x) = 1 + 2/x, P = 2, and po = 4.
(c) g(x) = x2/3, P = 3, and po = 3.5.
(d) g(x) = -x2 + 2x + 2, P = 2, and po = 2.5.

The diagrams are shown below.


(a) The iteration converges monotonically.
(b) The iteration exhibits oscillating convergence.
(c) The iteration does not converge—it diverges to infinity.
(d) The iteration does not converge.
6. Suppose g(x) and g’(x) are continuous on (a,b); po, p1, p2 are in (a,b), and p1=g(po) and
p2=g(p1). Also assume that there exists a constant K such that |g’(x)| < K. Show that |p2-
p1| < K|p1-po|. Hint: Use the mean value theorem.

We have p2-p1 = g(p1) – g(po) = g’(co) (p1 – po) for some co between po and p1
(by the mean value theorem)
Since po and p1 are in (a,b), co must also be in (a,b), and therefore |g’(co)|<K.
Therefore,
|p2-p1| = |g’(co) (p1 – po)| = |g’(co)| |(p1 – po)| < K|p1-po|.

11. For fixed-point iteration, discuss why it is an advantage to have g’(P) = 0.

The iteration satisfies |pn+1 – pn| < K |pn – pn-1| ,


where K is a bound on the absolute value of the derivative in the region of the fixed
point. Thus, the smaller |g’(P)|, the smaller will K be in the inequality above, and the
closer will successive iterates be. This means that the iteration will converge faster if the
derivative is smaller. The smallest derivative is 0, so an iteration around a fixed point
with a zero derivative will converge rapidly.
ALGORITHMS AND PROGRAMS

1. Use the fixed point iteration algorithm to approximate the fixed points (if any) of the
following functions to 12 decimal places. Produce a graph of each function and the line
y=x that clearly shows any fixed points.
(a) g(x) = x5 – 3x3 – 2x2 + 2 The iteration does not converge. There are
3 unstable fixed points
(b) g(x) = cos(sin(x)) x* = 0.7681691567
2
(c) g(x) = x – sin(x + 0.15) The iteration does not converge
(d) g(x) = xx-cos(x) The iteration will to converge to the fixed point
x*=1 if the initial condition is in the interval
[.6, 1.2]. There is another unstable fixed point at
approximately 1.283.

Cobweb diagrams are below. MATLAB code follows.


MATLAB code

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Computes iterates and cobweb diagram for given function %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% PARAMETERS
% ic - initial condition
% num - number of iterations

num=20;

% DEFINE FUNCTION USED IN ITERATIONS

f=@(x)cos(sin(x));
ic=-1; a=-1;b=2;c=-.1;d=1.1; fs=sprintf('cos(sin(x))');

%%%%%%%%%% ITERATIONS %%%%%%%%%%%%%


%
x = ic; %
for i=1:num %
x(i+1) = f(x(i)); %
end %
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

err = abs(x(i+1)-x(i))/abs(x(i));
disp(sprintf('Fixed point at x = %.12f\nRelative error:
%.3e',x(i+1),err))

% <<< Plot results >>>

figure(2); clf
subplot(2,1,1)
hold on; plot(0:num,x,'r.','linewidth',4); plot(0:num, x,'b')
axis([0 num min(0,max(min(x),-10)), max(1,max(x))])
xlabel('n','fontsize',20); ylabel('x_n','fontsize',20)
title(sprintf('Iterations of x_{n+1}=g(x_n);
g(x)=%s',fs),'fontsize',16)

subplot(2,1,2)
xx=linspace(a,b,100);
hold on; plot(xx,f(xx),'r'); plot(xx,xx,'k')
plot([x(1) x(1)],[0 x(2)],'b')
plot([x(1) x(2)],[x(2) x(2)],'b')
for i=2:num
plot([x(i) x(i)],[x(i) x(i+1)],'b')
plot([x(i) x(i+1)],[x(i+1) x(i+1)],'b')
end
plot(xx,f(xx),'r'); plot(ic,0,'c*')
axis([a b c d])
xlabel('x_n','fontsize',20)
ylabel('x_{n+1}','fontsize',20)
title(sprintf('Cobweb diagram; g(x)=%s',fs),'fontsize',16)
Section 2.2

3. For each function, find an interval [a,b] so that f(a) and f(b) have different signs.
(a) f(x) = ex – 2 – x
f(-3) > 0, f(0) < 0, f(3) > 0; thus roots lie in the intervals [-3,0] and [0,3].
(b) f(x) = cos(x) + 1 – x
f(  /4) > 0 and f(  /2) < 0; thus a root lies in the interval [  /4,  /2].
(c) f(x) = ln(x) – 5 + x
f(3) < 0 and f(5) > 0; thus a root lies in the interval [3,5]
(d) f(x) = x2 – 10x + 23
f(3) > 0, f(5) < 0, f(7) > 0; thus roots lie in the intervals [3,5] and [5,7].

4. Start with [ao,bo] and use the false position method to compute co, c1, c2, and c3.
f(x) = ex – 2 – x = 0, [ao,bo] = [-2.4, -1.6]
co = -1.83008, c1 = -1.84093, c2 = -1.84139, c3 = -1.8414

9. What will happen if the bisection method is used with the function f(x) = 1/(x-2) and
(a) the interval is [3,7]?
f(3)f(7) = 1/5 > 0, so the algorithm would tell us that there is no root in [3,7],
which is true.
(b) the interval is [1,7]?
f(1)f(7) = -1/5 < 0, but there is no root in (1,7). The method fails, because the
function is not continuous in [1,7].

f (bn )(bn  a n )
12. Show that the formula c n  bn  is algebraically equivalent to
f (bn )  f (a n )
a n f (bn )  bn f (a n )
cn  .
f (bn )  f (a n )
We add the terms:
f (bn )(bn  a n ) bn ( f (bn )  f ( a n )) f (bn )(bn  a n )
c n  bn   
f (bn )  f ( a n ) f (b n )  f ( a n ) f (b n )  f ( a n )
bn ( f (bn )  f (a n ))  f (bn )(bn  a n )

f (b n )  f ( a n )
a n f (bn )  bn f ( a n )

f (bn )  f ( a n )
14. The polynomial f(x) = (x-1)3(x-2)(x-3) has 3 zeros: x=1 of multiplicity 3, x=2, and
x=3, each of multiplicity 1. If ao and bo are any two real numbers such that ao < 1 and bo
> 3, then f(ao)f(bo)<0. Thus, on the interval [ao,bo] the bisection method will converge to
one of the three zeros. If ao<1 and bo>3 are selected so that cn=(an+bn)/2 is not equal to 1,
2, or 3 for any n>= 1, then the bisection method will never converge to which zero(s)?
Why?

It can’t converge to x=2, unless it hits cn=2 exactly (extremely unlikely), for the
following reason.
Excluding the cases where cn hits 1 or 3 exactly, if ao<1, and bo>3, then the midpoint co
will be either less than 1, greater than 3, or in the interval (1,3). In the first and second
cases, a1=co or b1=co, and we back to the original case ao<1, bo>3. Assume, therefore,
that the iteration has proceeded until cn is in (1,3), and relabel this point co. If, 1 < co < 3,
then there are three possibilities:
(i) 1 < co < 2, in which case f(co)>0, so b1 = co, and x=2 is outside of the bracket,
(ii) 2 < co < 3, in which case f(co)<0, so a1 = co, and again, x=2 is outside of
the bracket, or
(iii) co = 2.
In either case, once an iteration gets a point in the interval (1,3), it brackets in on either 1
or 3, but never 2, unless it happens to hit on 2 exactly.

You might also like