You are on page 1of 49

ROOT FINDING CONTD

Run your MATLAB. Revision to the


previous problem: x=x-(tanh(x)-x/3)/
(sech(x)^2-1/3)
>>x=2
x=
2
>> x=x-(tanh(x)-x/3)/(sech(x)^2-1/3)
x=
3.1320

>> x=x-(tanh(x)-x/3)/(sech(x)^21/3)
x=

2.9853
>> x=x-(tanh(x)-x/3)/(sech(x)^21/3)
x=

2.9847
>> x=x-(tanh(x)-x/3)/(sech(x)^21/3)
x=

Exercise 4. How many


iterations did you obtain for the
solution to converge? What did
it converge to? Tell MATLAB to
show the full accuracy and find
the 16-digit number that the
iteration converges to.

>> format long


>> x=x-(tanh(x)-x/3)/(sech(x)^21/3)
x=
2.984704585357887

Convergence
Various root-finding algorithms
have relatively different speed at
which the approximate solution
converges or gets closer to the
true solution. An iterative method
xn+1= g(xn) is defined as having pth order convergence if for a
sequence xn, limnxn= exists,
then
limn|xn+1||xn|p=L0.

Newton's method has (generally)


second-order convergence, then
from the last eqn., p=2.
The secant method has an order of
convergence between 1 and 2.
To discover it we need to modify the
code for the Newtons method so
that it remembers all the
approximations
Lets use x(1) for x1, x(2) for x2,
x(n) for xn:

x(1)=2 means our 1st guess.


for n=1:5 means we will iterate 5
times with n indicating the current valid
approximation.
x(n+1)=x(n)-(tanh(x(n))-x(n)/3)/(sech(x(n))^2-1/3)

>> x(1)=2
x=
2
>> for n=1:5
x(n+1)=x(n)-(tanh(x(n))-x(n)/3)/(sech(x(n))^21/3)
end
Press the enter key to obtain

x=
2.0000 3.1320
x=
2.0000 3.1320 2.9853
x=
2.0000 3.1320 2.9853 2.9847
x=
2.0000 3.1320 2.9853 2.9847
2.9847
x=
2.0000 3.1320 2.9853 2.9847 2.9847
2.9847

The above can be re-computed to


obtain all the answers in a single line.
>> x(1)=2;
>> for n=1:5
x(n+1)=x(n)-(tanh(x(n))-x(n)/3)/
(sech(x(n))^2-1/3);

x
end
x=
2.0000 3.1320 2.9853 2.9847
2.9847 2.9847

Comments:
After running this code, x holds the 6
approximations (including our initial
guess) with the last one being the most
accurate approximation as shown above.
The semicolon (;) at the end of the 1st
and 3rd lines tell MATLAB not to display
the value of x after the assignment.
When x in line 4 is not specified, the
code would calculate x, but nothing
would be shown.
Re-write the code without x. This gives

>> x(1)=2;
>> for n=1:5
x(n+1)=x(n)-(tanh(x(n))-x(n)/3)/(sech(x(n))^21/3);

end
>> nothing is shown. Enter and write
x and enter again. What happens?
The position of x in the code also
matters. When it is placed before the
end command, the results will be
displayed five times:

>> x(1)=2;
>> for n=1:5
x(n+1)=x(n)-(tanh(x(n))-x(n)/3)/(sech(x(n))^2-1/3);
x
end
x=
2.0000 3.1320 2.9853 2.9847 2.9847 2.9847
x=
2.0000 3.1320 2.9853 2.9847 2.9847 2.9847
x=
2.0000 3.1320 2.9853 2.9847 2.9847 2.9847
x=
2.0000 3.1320 2.9853 2.9847 2.9847 2.9847
x=
2.0000 3.1320 2.9853 2.9847 2.9847 2.9847

Remember that in using the Newton


formula (code) the difference between
two successive iterations must be equal
to zero, for us to conclude convergence.
In our own case, there is a small but nonzero distance between x(5) and x(6):
>> x(6)-x(5)
ans =
4.4409e-16
This difference is really small enough for
us to safely assume convergence of
solution

CODING:
Coding in a file instead of writing all your
commands at the command prompt is
possible.
you can type a list of commands in a file,
save it and then copy into MATLAB
command prompt to execute" all of the
commands.
This is useful because of the likely mistakes
you are bound to make every time you write
more than 5 lines of code.
By putting the commands in a file you can
correct your mistakes without introducing
new ones.

Create a file in Word.doc of the


Newtons code: copy and paste
in to the command prompt.
x(1)=2;
for n=1:5
x(n+1)=x(n)-(tanh(x(n))-x(n)/3)/
(sech(x(n))^2-1/3);
end
x
You have the same results as before.

Saving and Executing MATLAB files.


They have names that end with .m.
The file name must comprise only letters and
numbers with no spaces. The first character
must be a letter, not a number.
Open a new file by clicking on the white newfile icon in the top left of the window, or select
from the menu FileNewScript M-file. Copy
the Newton method code into it.
Save it and give it a name (e.g
NewtonTanh.m). Go to the command prompt
and "run" the file by typing the name (without
the .m) and pressing Enter.
Same results are displayed as before

NOTE:
You can store your files wherever you
want, but they have to be in MATLAB's
"search path" (or in the current
directory).
To add the directory you want to the
path select File Set path select
"Add Folder", select the folder you
want, click "OK" then "Save".
To check if your file is in the path you
can type which NewtonTanh and the
result should be the path to your file

If you choose a file-name that is


already the name of a MATLAB
command, you will effectively "hide''
that command as MATLAB will use your
file instead. So, before choosing a file
name such as sum, or find, or exp,
check, use the which command to see
if it is an in-built MATLAB command.

The same warning also applies to


variable names: a variable will
"hide" any file or command with
the same name.

If you get strange errors when


you try to run your file, make
sure that there are no spaces or
other non-letters in your
filename, and that the file is in
the path.
Remember that after you make
changes to your file, you need to
save it so that MATLAB will be
aware of the changes you made

We have already seen how to access a


specific element; for example to access
the fourth element we write x(4).
MATLAB can access a sublist by giving
it a list of indexes instead of a single
number:
>> x([1 2 3])
ans =
2.0000 3.1320 2.9853
We can also use the colon instead of
listing all the indexes:
>>x(2:4)

>> x(2:4)
ans =

3.1320 2.9853 2.9847


Another thing we can do is
perform element-wise operations
on all the items in the list at once.
Examples
>> x(1:3).^2
ans =
4.0000 9.8095 8.9118

>> x(1:3)*2
ans =
4.0000 6.2640 5.9705
>> x(1:3)-x(6)
ans =
-0.9847 0.1473 0.0006

>> x(2:4)./(x(1:3).^2)
ans =

0.7830 0.3043 0.3349

>> abs(x(1:end))
ans =
2.0000 3.1320 2.9853 2.9847 2.9847
2.9847

We can plot a graph of y = 2x using


>> plot(x(1:end),x(1:end).*2)
The 1st part before the comma is
plotted on the x-axis while the 2nd
part after the comma will be on the
y-axis.
The graph is as shown:

6.5

5.5

4.5

2.2

2.4

2.6

2.8

3.2

3.4

Read about the secant code and


use it to find the root to
tanh(x)=x/3
In the codes above, the periods in
front of *, /, and ^ are used to
request an element-by-element
operation. They are needed when
the operation can have a linear
algebra connotation.

Internalize the differences between


the point-wise and regular versions
of the operators by examining the
results of the following expressions
that use the variables A=[1 2; 3 4],
B=[1 0; 0 2], and C=[3;4]. Note:
some commands may result in an
error message. Understand what the
error is and why it was given
When the variables are written on
the command prompt, the following
results are given

>> A=([1 2; 3 4]),B=([1 0; 0


2]),C=([3;4])
A=

1
2

3
4
B=

1
0

0
2
C=

A*B vs. A.*B vs. B.*A vs. B*A


2*A vs. 2.*A
A^2 vs. A*A vs. A.*A vs. A.^2 vs.
2.^A vs. A^A vs. 2^A. The last
one here might be difficult to
understandit is matrix
exponentiation.
A/B vs. A\B vs. A./B vs. A.\B
A*C vs. A*C' vs. C*A vs. C'*A
A\C vs. A\C' vs. C/A vs. C'/A

>> A*B
ans =
1
4
3
8
>> A.*B
ans =
1
0
0
8

>> B.*A
ans =
1
0
0
8
>> B*A
ans =
1
2
6
8

2*A
ans =
2
6
2.*A
ans =
2
6

4
8

4
8

A^2
ans =
7 10
15 22
>> A*A
ans =
7 10
15 22

A.*A
ans =
1
4
9 16
>> A.^2
ans =
1
4
9 16
>> 2.^A
ans =
2
4
8 16

>> A^A
??? Error using ==> mpower
At least one operand must be
scalar.
>> 2^A (it is matrix
exponentiation)
ans =
10.4827 14.1519
21.2278 31.7106

>> A/B
ans =
1
1
3
2
>> A\B
ans =
-2.0000 2.0000
1.5000 -1.0000

Basic Plotting:
Interpret the following: 1:10,
1:2:10, 100:-25:0
Do them
Let x = [2 5 1 6]. What will x(3),
x([1 2]), x([1:end]), x(end:-1:1),
x(:), x([1 1 1 1]) do? Lets
discuss and finally verify

When creating a matrix, a


space or a comma (,) are the
separator between columns,
while a semicolon (;) separate
between rows. Figure out how
to create the following
matrices: (142536) ,
(100110)

You can nest matrix


construction so that
[ 6 (1:5)
7 ] makes sense (what does it
result in?)
>> [6 (1:5) 7]
ans =

6
1
2
3
4
5
7
We will come back to matrices

The plot command plots a list of


points given as two vectors, X and
Y of their x- and y- coordinates,
respectively.
The default behaviour is that no
mark is placed on the points, and
the points are joined by a straight
line.
To plot a parabola y = x2 for
x[1,1]
we can write:

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1

-0.8

-0.6

Plot of y = x2

-0.4

-0.2

0.2

0.4

0.6

0.8

We could make use of any


colour for the line.
Lets make the line green by
adding a third input
>> x=-1:.1:1; y=x.^2;
>> plot(x,y, 'g.') This gives the
graph below:

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1

-0.8

-0.6

Plot of y = x2

-0.4

-0.2

0.2

0.4

0.6

0.8

The period after g indicates that


the plot should be shown with
the individual points.
For a continuous curve remove
the point.
>> x=-1:.1:1; y=x.^2;
>> plot(x,y, 'g') This gives

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

Simply type the 1st letter of


any colour you desire and it will
plot with the colour
>> x=-1:.1:1; y=x.^2;
>> plot(x,y, 'r.') This is red with
dotted curve.

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

>> x=-1:.1:1; y=x.^2;


>> plot(x,y, 'y') yellow
>> plot(x,y, 'b') blue
>> plot(x,y, 'p')purple
Etc
Generally plots need not be a
function in the mathematical
sense of the word:
For instance t=-1:.01:2*pi;
x=sin(5*t); y=cos(3*t); plot(x,y,'r')

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

Practice this:
Plot the function sinx vs. x , for
x[0,6]
Learn how to plot two functions
with the same plot command,
and plot sinx and cosx vs. x
plot and figure out how to do
the following:
Plot only the points and not the
lines

You might also like