Section 1.8 Review of Chapter 1
Subsection 1.8.1 Methods and Formulas
Subsubsection 1.8.1.1 Solving equations numerically:
- \(f(x) = 0\)
-
an equation we wish to solve.
- \(x^{*}\)
-
a true solution.
- \(x_{0}\)
-
starting approximation.
- \(x_{n}\)
-
approximation after \(n\) steps.
- \(e_{n} = x_{n} - x^{*}\)
-
error of \(n\)-th step.
- \(r_{n} = y_{n} = f(x_{n})\)
Subsubsection 1.8.1.2 Newton’s method:
\begin{equation*}
x_{i+1}= x_{i} - \frac{f(x_{i})}{f'(x_{i})}
\end{equation*}
Subsubsection 1.8.1.3 Bisection method:
\(f(a)\) and \(f(b)\) must have different signs.
\begin{equation*}
x = (a+b)/2
\end{equation*}
Choose \(a = x\) or \(b = x\text{,}\) depending on signs. \(x^{*}\) is always inside \([a,b]\text{.}\) \(e < (b-a)/2\text{,}\) current maximum error.
Subsubsection 1.8.1.4 Secant method:
\begin{equation*}
x_{i+1}= x_{i} - \frac{x_{i} - x_{i-1}}{y_{i} - y_{i-1}}y_{i}
\end{equation*}
Subsubsection 1.8.1.5 Regula Falsi
Is a hybrid between secant and bisection methods.
\begin{equation*}
x = b - \frac{b - a}{f(b) - f(a)}f(b)
\end{equation*}
Choose \(a = x\) or \(b = x\text{,}\) depending on signs.
Subsubsection 1.8.1.6 Convergence:
Bisection is very slow. Newton is very fast. Secant methods are intermediate in speed. Bisection and Regula Falsi never fail to converge. Newton and Secant can fail if \(x_{0}\) is not close to \(x^{*}\text{.}\)
Subsubsection 1.8.1.7 Locating roots:
Use knowledge of the problem to begin with a reasonable domain. Systematically search for sign changes of \(f(x)\text{.}\) Choose \(x_{0}\) between sign changes using bisection or secant.
Subsubsection 1.8.1.8 Usage:
For Newton’s method one must have formulas for \(f(x)\) and \(f'(x)\text{.}\) Secant methods are better for experiments and simulations. Bisection and Regula Falsi are slower, but keep the root within the current bounds.
Subsection 1.8.2 MATLAB
Subsubsection 1.8.2.1 Commands:
-
v = [0 1 2 3] -
Make a row vector.
-
u = [0; 1; 2; 3] -
Make a column vector.
-
w = v' -
Transpose: row vector \(\leftrightarrow\) column vector
-
x = linspace(0,1,11) -
Make an evenly spaced vector of length 11.
-
x = -1:.1:1 -
Make an evenly spaced vector, with increments \(0.1\text{.}\)
-
y = x.^2 -
Square all entries.
-
plot(x,y) -
plot y versus x.
-
f = @(x) 2*x.^2 - 3*x + 1 -
Make an anonymous function.
-
y = f(x) -
A function can act on a vector.
-
plot(x,y,'*','red') -
A plot with options.
-
Control-c -
Stops a computation.
Subsubsection 1.8.2.2 Program structures:
for ... end example: for i=1:20
S = S + i;
end
if ... end example: if y == 0
disp('An exact solution has been found')
end
while ... end example: while i <= 20
S = S + i;
i = i + 1;
end
if ... else ... end example: if c*y>0
a = x;
else
b = x;
end
Subsubsection 1.8.2.3 Function Programs:
-
Begin with the word
function. -
There are inputs and outputs.
-
The outputs, name of the function and the inputs must appear in the first line. i.e.
function x = mynewton(f,x0,n) -
The body of the program must assign values to the outputs.
-
Internal variables are not visible outside the function.
-
A function program may not use variables in the current workspace unless they are inputs.
Subsubsection 1.8.2.4 Script Programs:
-
There are no inputs and outputs.
-
A script program may use, create, change and even delete variables in the current workspace.
Subsubsection 1.8.2.5 Symbolic:
syms x y |
|
f = 2*x^2 - sqrt(3*x) |
|
subs(f,sym(pi)) |
|
double(ans) |
|
g = log(abs(y)) |
MATLAB uses log for natural logarithm. |
h(x) = compose(g,f) |
|
k(x,y) = f*g |
|
fplot(f) |
|
fplot(g,-10,10) |
|
ezsurf(k) |
|
f1 = diff(f,'x') |
|
F = int(f,'x') |
indefinite integral (antiderivative) |
int(f,0,2*pi) |
definite integral |
poly = x*(x - 3)*(x-2)*(x-1)*(x+1) |
|
polyex = expand(poly) |
|
polysi = simple(polyex) |
|
solve(f) |
|
solve(g) |
|
solve(polyex) |
