Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

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
Identify the possible problems or common errors
to this code.
>> x=x-(tanh(X)-x/3)/(sec(x)^2-1/3)) [3 errors]
The 1st 2 errors will be identified when you press
enter. The 3rd error may not be easily detected.
>> x = x - (tanh(X)-x/3)/(sec(x)^2-1/3))
??? x = x - (tanh(X)-x/3)/(sec(x)^2-1/3))
Error: Unbalanced or unexpected parenthesis or
bracket.
[simply press the arrow key pointing upwards to
repeat codes. Then edit as you desire. You don’t
have to type codes over and over]
>> x = x - (tanh(X)-x/3)/(sec(x)^2-(1/3))
'??? Undefined function or variable 'X.
>> x = x - (tanh(x)-x/3)/(sec(x)^2-(1/3))
x = 1.9453
>> x=2; x = x - (tanh(x)-x/3)/(sech(x)^2-(1/3))
x=
3.1320
>> x=2; x = x - (tanh(x) - x/3) / (sech(x)^2 - 1/3)[spacing ]
x=
3.1320
>> x = x - (tanh(x) - x/3) / (sech(x)^2 - 1/3)
x=
2.9853
>> x = x - (tanh(x) - x/3) / (sech(x)^2 - 1/3)
x=
2.9847
>> x = x - (tanh(x) - x/3) / (sech(x)^2 - 1/3)
x=
2.9847
>> format long
>> x = x - (tanh(x) - x/3) / (sech(x)^2 - 1/3)[simply
copy the previous code]
x=
2.984704585357887
>> x = x - (tanh(x) - x/3) / (sech(x)^2 - 1/3)
x=
2.984704585357887
• 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.
• 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
p-th order convergence if for a
sequence xn, lim n→∞ xn=α exists, then
• lim n→∞ |xn+1 −α| |xn −α|p =L≠0.
• 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 Newton’s method so that
it remembers all the approximations
• Let’s 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))^2-1/3)
• end

• 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))^2-1/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 non-zero
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
Newton’s 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 new-file
icon in the top left of the window, or select from the
menu File→New→Script →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
• 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? Let’s discuss and finally verify

• >> 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

4
2 2.2 2.4 2.6 2.8 3 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.
• 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 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

• Plot of y = x2
• We could make use of any colour for
the line.
• Let’s 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 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

• Plot of y = x2
• 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 0.2 0.4 0.6 0.8 1
• 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 0.2 0.4 0.6 0.8 1
• >> 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.2

-0.4

-0.6

-0.8

-1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
• 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