Problem 2 from homework: We have a cable of length $$L$$ and suspend it from two given points. Find the graph $$y=f(x)$$. We obtain a curve $$\Bigl(x(s),f(x(s))\Bigr)$$ with arc length $$s\in [0,L]$$. The potential energy due to gravity is given by $$E=\int_{s=0}^L f(x(s))ds$$. Among all possible curves of length $$L$$ we will obtain the curve with minimal potential energy. This curve is called catenary. It is the graph of the function $$f(x) = a \cdot \cosh(x/a)$$ (with the origin of the coordinate system suitably chosen). Drag the red point with your finger/mouse!

NEWS:
• Solution of homework ,   Matlab mlx file
• Solution of exam ,   Matlab mlx file
• Additional practice problems for the final (corrected version)
solution (corrected version)
solution of problem 3 using function
• There will be a review for the final exam on Tue Dec 15 at 2:00pm.
• The final exam is on Dec. 19 from 10:30am to 12:30pm.
You can use a cheat sheet: two letter size pages, handwritten (submit together with exam).
• Homework, deadline extended to Dec. 17 at 10pm.
You should hand in
• a single pdf file containing the paper and pencil work, the computational results (published pdf from Matlab) and observations, and answers to all the questions I asked.
• your .m or .mlx files so that I can run your code

Problem 1: How to import CO2 data into Matlab , mlx file
This shows how to obtain the vectors t and conc. Then let y=log(conc) and perform the curve fitting for the vectors t and y.
Problem 2: I have added additional explanations for fsolve below, in particular look at this example. This shows how you can plot for $$n=2$$ you can plot the zero contours of $$f_1,f_2$$ using zerocont2 (you need to download the m-file zerocont2.m. This helps you to pick a good initial guess for fsolve.
Problem 3: It should be $$dx$$ in the integral, the homework is now corrected.
• Exam on Tue Dec 8 will cover linear systems, least squares problem, interpolation, nonlinear equations
You can use a cheat sheet: two letter size pages, handwritten (submit together with exam).
Practice problems for exam
Solution, Matlab mlx file (open this in Matlab to modify /run this)
You should know how to
• generate graphs using plot. Example: x=0:.1:60; plot(x,x.*sin(x))
• write, run, and publish a script m-file
(use  %% for the main title and section titles in an m-file)
• write a function m-file

## Course information

• Review of Taylor's theorem with example
• Introduction: Errors, Taylor Theorem (covered on Tue Sept. 1)
• Error propagation, forward error analysis, numerical stability
• Unavoidable error, numerically stable and unstable algorithms.
Download numstab.mlx, then you can open this in Matlab as a "Live Script", and modify and rerun the code.
How to use Live Scripts
• Useful functions available in all programming languages:
expm1(x) = ex-1,     log1p(x) = ln(1+x),     hypot(x,y) = (x2 + y2)1/2
For x close to zero evaluating exp(x)-1 and log(1+x) causes unnecessarily large roundoff error. Using expm1 and log1p always gives optimal accuracy.
Evaluating sqrt(x^2+y^2) can cause problems with overflow and underflow: Try x=y=1e-300 or x=y=1e300. Using hypot(x,y) always gives optimal accuracy.
• Machine numbers and machine arithmetic
IEEE754 machine numbers and machine arithmetic
• Gaussian elimination without and with pivoting
• How to approximate the condition number if you have the LU decomposition: Download invnormest.m and use
cond1 = norm(A,1)*invnormest(L,U)
condInf = norm(A,Inf)*invnormest(U',L')
• Errors for linear systems (contains all the proofs which I skipped in class)
• How to solve sparse linear systems in Matlab:
A sparse matrix is a matrix where most elements are zero. In this case it is much more efficient to use the special sparse data structures in Matlab. All operations like *, \, lu have special efficient algorithms for matrices in sparse format. In particular the backslash command x=A\b uses clever row and column permutations to minimize "fill-in".
• initializing a sparse matrix: A sparse matrix can be described by giving a list of the nonzero elements, as vectors iv (contains i-values), jv (contains j-values), av (contains matrix entries). Then a sparse matrix is initialized by
A = sparse(iv,jv,av);
Example: The full matrix given by A = [0 7 0; 0 0 8; 9 0 0] can be defined as a sparse matrix by
A = sparse([1 2 3],[2 3 1],[7 8 9])
• Solving a linear system with a sparse matrix:
• [L,U,p,q] = lu(A,'vector'); % Gaussian elimination with row and column permutations (chosen to minimize "fill-in")
y = L\b(p)                  % forward substitution with permuted vector b
x = U\y; x(q) = x           % back substitution, then undo permutation given by q
• Matlab shortcut
x = A\b
This performs the above steps automatically (but without the option to save the result of the LU-decomposition).
• Interpolation, divided difference algorithm (updated 10/22)
• INTERACTIVE DEMONSTRATION of polynomial interpolation and cubic spline
• Interpolation example from class , mlx file (open this in Matlab to run and modify code)
• INTERACTIVE DEMONSTRATION for node polynomial $$\omega(x)=(x-x_1)\cdots(x-x_n)$$
compare equidistant and Chebyshev nodes
• Piecewise polynomial interpolation (updated 10/27)
• Piecewise cubic interpolation in Matlab: The column vector x contains the x-coordinates of the nodes, the column vector y contains the function values at the nodes, the vector yp contains the derivatives at the nodes. The vector xi contains the points where we want to evaluate the interpolating function, the vector yi contains the corresponding values of the interpolating function.
• Cubic Hermite interpolation: Download hermite.m . Then use yi = hermite(x,y,yp,xi)
• Complete cubic spline: points are given by column vectors x, y. Slopes at left and right endpoint are sl and sr.
yi = spline(x,[sl;y;sr],xi)
• Not-a-knot cubic spline: yi = spline(x,y,xi)
• Example for piecewise cubic interpolation ,   Matlab mlx file (open this in Matlab, you can then modify and rerun this)
• historical origin of "splines": "used principally by ship architects ... splines are thin, tapering pieces of lancewood or red pine" in "Treatise on Mathematical Drawing instruments" from 1868
• Linear least squares problem
• Nonlinear equations (updated 11/19)
• Newton example from class (11/19) ,   Matlab mlx file (open this in Matlab to modify and run this)
• Nonlinear system: Example for Newton method and fsolve     (m-file)
• Solving a nonlinear system in Matlab using fsolve: (without using the Jacobian)
Write an m-file f.m containing the function f:
function y = f(x)
y = ...
Then use x = fsolve(@f,x0) where x0 is the initial guess.
Example: See the bottom of the first page of Nonlinear equations
You can specify additional options using optimset, e.g. the tolerance for function value and the tolerance for x:
opt = optimset('TolFun',1e-5,'TolX',1e-5);
x = fsolve(@f,x0,opt);
There are many other options (e.g., display each iteration, or use the Jacobian) , type doc fsolve for more information.
• Numerical integration (updated 12/03)
• Example for adaptive integration ,   mlx-file (open this in Matlab to modify and run the code)
• Numerical integration with Matlab:   Q = integral(f,a,b)
Example: Find the integral of $$e^{(-x^2)}$$ from 0 to 5:
f = @(x) exp(-x.^2);     % Use .* ./ .^ instead of * / ^
Q = integral(f,0,5)
Note:
• Write your function f(x) so that it works for a vector x and returns a vector f(x) of function values f(x1),...,f(xn): use .* ./ .^ instead of * / ^ in the definition of f. E.g., for $$f(x)=\sin(x^2)/x$$ use f=@(x)sin(x.^2)./x
• If your function f(x) only works for scalars x you MUST use the option 'ArrayValued',true:
Example: f = @(x) exp(-x^2); Q = integral(f,0,5,'ArrayValued',true)
• You can specify tolerances for the relative and absolute error:
Q = integral(f,a,b,'RelTol',1e-13,'AbsTol',1e-13)
• This also works for "improper integrals", e.g., $$\displaystyle \int_0^\infty x^{-1/3} e^{-x}\cos x\,dx$$
f = @(x) x.^(-1/3).*exp(-x).*cos(x)
Q = integral(f,0,Inf)
Q = integral(f,0,Inf,'RelTol',1e-14,'AbsTol',0)   % want relative error$$\le 10^{-14}$$
• Matlab functions which have a function f as argument (e.g., fzero, fzero, fsolve):
• if the function f is given as an @-expression: use fzero(f,[a,b])
• if the function f is given in a separate m-file f.m: use fzero(@f,[a,b])
(the argument needs to be a "function handle")

## Matlab programming

We will use Matlab to see how various algorithms work. We will use Matlab to see how various algorithms work.

• Gentle introduction to Matlab (also explains how to use the Matlab interface, how to publish)
• We will NOT use SYMBOLIC Matlab commands (like sym, syms, subs, diff, taylor etc.) in this class. (except when I explicitly ask you to use vpa to compare machine arithmetic with higher precision arithmetic.)
• Matlab Primer gives a concise summary of the most important Matlab commands. It was written for an older version of Matlab, so skip sections 14, 15, 17.

How to hand in Matlab results for homeworks:

• You have to write an m-file for each problem. (Typing commands at the >> prompt and printing this out is not allowed)
• You have to hand in all m-files together with the generated output and graphics.
The best way is to use the publish command in Matlab (if you call additional m-files you have to print them out separately).
• Comment your code so that another person (or you one week later) can see what you are doing.
• Only print out values which are asked for in the problem. (Make sure you have a semicolon at the end of lines.)
Label all numerical output and graphics: How to display and label numerical output and graphics in Matlab
• Hand in additional pages (this can be handwritten) which show the "paper and pencil work",
• Each homework has to be handed in as a SINGLE PDF file on ELMS.

How to use the publish command in Matlab:
Use lines starting with %% for the main title and for each section title.
Lines starting with % right after a section title will be printed at the beginning of each section. Use this for your observations about the problem.