Professional Documents
Culture Documents
>> 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=
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.
>> 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
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
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.
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
>> x(2:4)
ans =
>> 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 =
>> abs(x(1:end))
ans =
2.0000 3.1320 2.9853 2.9847 2.9847
2.9847
6.5
5.5
4.5
2.2
2.4
2.6
2.8
3.2
3.4
1
2
3
4
B=
1
0
0
2
C=
>> 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
6
1
2
3
4
5
7
We will come back to matrices
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
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
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
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
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