CME 315 Midterm 1: Root Finding, Optimization & Linear Systems in MATLAB
Posted by Anonymous and classified in Mathematics
Written on in
English with a size of 643.81 KB
CME 315 - Midterm 1
1 — Solve using a bracket or initial guess. root = fzero(f, [xLower guess xUpper guess]); root = fzero(f, x0);
Minimum & Maximum: fminbnd
1 — Minimum candidate. [xMinCandidate, fMinCandidate] = fminbnd(f, a, b);
2 — Maximum candidate. xMaxCandidate = fminbnd(@(x) -f(x), a, b); fMaxCandidate = f(xMaxCandidate);
Linear Systems & Mass Balances
1 — Start from the balance. % Accumulation = In - Out + Net generation. % Steady state: In - Out + Net generation = 0
2 — Arrange equations into A*x = b. Rows = equations; columns = unknowns. Move known terms to the right.
3 — Diagnostics. n = size(A,2); Aug = [A b]; rA = rank(A); rAb = rank(Aug); d = det(A); % Square A only c = cond(A);
Gaussian Elimination
1 — Setup. N = length(b); Aug = [A b]; X = zeros(N,1);
2 — Pivot-column loop. for j = 1:N-1
3 — Rows below pivot. for i = j+1:N
4 — Multiplier. m = Aug(i,j)/Aug(j,j);
5 — Row elimination; then close both loops. Aug(i,:) = Aug(i,:) - m*Aug(j,:);
6 — Last unknown. X(N) = Aug(N,N+1)/Aug(N,N);
7 — Back-substitution loop. for k = N-1:-1:1
8 — Remaining unknowns; then close loop. X(k) = (Aug(k,N+1) - Aug(k,k+1:N)*X(k+1:N))/Aug(k,k);
9 — Verify with original matrix. residual = norm(A*X - b);
Zero/small pivot: swap rows before elimination. MATLAB uses lowercase end.
| MATLAB Syntax | Theoretical Question It Answers |
|---|---|
| determinant | For square A, is it singular? Exactly zero means singular in exact arithmetic. |
| rank of A | How many independent coefficient equations are present? |
| Augmented matrix | What does the full system look like when the right-hand side is included? |
| rank of augmented matrix | Does the right-hand side introduce an incompatible requirement? |
| condition of A | How sensitive could the solution be to small changes in the inputs? |
| backslash solution | What values satisfy the system? |
| norm of equation (A*x - b) | How closely does the computed answer satisfy the original equations? |
%.6f = 6 decimal places; %.3e = scientific notation; %d = integer; = newline.
Bisection Method
1 — Setup. f = @(x) <expression>; xL = <lower bound>; xU = <upper bound>;
2 — Evaluate endpoints. fL = f(xL); fU = f(xU);
3 — Check bracket. If either endpoint gives zero, it is a root. Otherwise require fL*fU < 0 and continuity throughout the interval.
4 — Iteration loop. for iter = 1:maxIter
5 — Midpoint and evaluation. xR = (xL + xU)/2; fR = f(xR);
6 — Check convergence. if abs(xU - xL)/2 < tolX || abs(fR) < tolF → break → end
7 — Update bracket. if fL*fR < 0 → xU = xR; fU = fR; → else → xL = xR; fL = fR; → end
8 — Close iteration loop. end
9 — Store and verify. root = xR; residual = abs(f(root));
False Position Method
1 — Setup. f = @(x) <expression>; xL = <lower bound>; xU = <upper bound>;
2 — Evaluate endpoints. fL = f(xL); fU = f(xU);
3 — Check bracket. Handle endpoint roots; otherwise require fL*fU < 0 and continuity.
4 — Iteration loop. for iter = 1:maxIter
5 — Straight-line estimate. xR = xU - fU*(xL - xU)/(fL - fU);
6 — Evaluate and check convergence. fR = f(xR); if abs(fR) < tolF → break → end
7 — Update bracket. if fL*fR < 0 → xU = xR; fU = fR; → else → xL = xR; fL = fR; → end
8 — Close iteration loop. end
9 — Store and verify. root = xR; residual = abs(f(root));
Bracket width may stay large; a small change in the estimate alone can indicate stagnation.
Newton–Raphson Method
1 — Define function and derivative. f = @(x) <expression>; df = @(x) <derivative>;
2 — Initial guess. x = <initial guess>;
3 — Iteration loop. for iter = 1:maxIter
4 — Evaluate and check current residual. fx = f(x); if abs(fx) < tolF → break → end
5 — Evaluate derivative. dfx = df(x); If abs(dfx) < 1e-12, stop: unsafe denominator.
6 — Newton update. xNew = x - fx/dfx; Check that xNew stays in the function’s domain.
7 — Calculate step and replace estimate. step = abs(xNew - x); x = xNew;
8 — Check convergence. if step < tolX || abs(f(x)) < tolF → break → end
9 — Close loop; store and verify. end root = x; residual = abs(f(root));
Numerical derivative alternative: h = 1e-6; df = @(x) (f(x+h) - f(x-h))/(2*h);