CME 315 Midterm 1 Cheat Sheet: MATLAB Root Finding, Optimization & Linear Systems
Posted by Anonymous and classified in Mathematics
Written on in
English with a size of 621.09 KB
CME 315 Midterm 1 Cheat Sheet
MATLAB General Setup and Output
General Setup and Output1 — Define a function; for roots, use left side − right side. 2 — Create plotting points. 3 — Elementwise operations. | 4 — Plot and label. 5 — Print results.
|
|---|---|
Root Finding: Choosing the Method
| Given or Requested | Use |
|---|---|
| Sign-changing bracket; guaranteed interval reduction | Bisection |
| Sign-changing bracket; straight-line estimate | False position |
| One guess and derivative | Newton–Raphson |
| Two guesses; no derivative | Secant |
Rearrangement x = g(x) | Fixed point |
| Built-in root solver permitted | fzero |
Before a bracketed method: check continuity and domain; evaluate endpoint roots separately.
fL = f(xL);
fU = f(xU);fL*fU < 0 % Opposite signs: valid continuous bracket
fL*fU > 0 % No sign-changing bracketBisection Method: Ordered Formulas
1 — Midpoint. xR = (xL+xU)/2;
2 — Evaluate. fR = f(xR);
3 — Position error bound and residual. errorBound = abs(xU-xL)/2; residual = abs(fR);
4 — Stopping condition. errorBound < tolX || residual < tolF
5 — Update the bracket.
% If fL*fR < 0:
xU = xR;
fU = fR;
% Otherwise:
xL = xR;
fL = fR;6 — Iterations needed for position tolerance.
N = max(0,ceil(log2((xU_initial-xL_initial)/(2*tolX))));False-Position Method: Ordered Formulas
1 — Straight-line root estimate.
xR = xU-fU*(xL-xU)/(fL-fU);2 — Evaluate.
fR = f(xR);3 — Check residual.
abs(fR) < tolF4 — Update the bracket using the same sign test as bisection.
% If fL*fR < 0:
xU = xR;
fU = fR;
% Otherwise:
xL = xR;
fL = fR;A small step alone can indicate stagnation; check the residual.
Newton–Raphson: Ordered Formulas
1 — Define derivative.
df = @(x) <derivative expression>;Or use a centered finite difference:
df = @(x) (f(x+h)-f(x-h))/(2*h);2 — Evaluate.
fx = f(x);
dfx = df(x);3 — Check current residual and derivative before dividing.
abs(fx) < tolF
abs(dfx) < derivativeTolerance % Unsafe Newton denominator4 — Update.
xNew = x-fx/dfx;5 — Check domain, calculate step, then replace estimate.
step = abs(xNew-x);
x = xNew;6 — Check convergence and verify residual.
step < tolX || abs(f(x)) < tolFSecant Method: Ordered Formulas
1 — Evaluate both guesses.
f0 = f(x0);
f1 = f(x1);2 — Check denominator before dividing.
denominator = f1-f0;3 — Update.
xNew = x1-f1*(x1-x0)/denominator;4 — Calculate step before shifting guesses.
step = abs(xNew-x1);5 — Shift guesses in this order.
x0 = x1;
x1 = xNew;6 — Check convergence.
step < tolX || abs(f(x1)) < tolFFixed-Point Iteration: Ordered Formulas
1 — Rearrange the equation.
g = @(x) <expression satisfying x=g(x)>;2 — Update.
xNew = g(x);3 — Calculate step and replace estimate.
step = abs(xNew-x);
x = xNew;4 — Check step and original-equation residual.
step < tolX && abs(f(x)) < tolFLocal convergence criterion:
abs(gDerivative(root)) < 1Built-in Root Finding: fzero
1 — Solve using a bracket or initial guess.
root = fzero(f,[xL xU]);
root = fzero(f,x0);2 — Check the original equation.
residual = abs(f(root));For multiple roots: use separate brackets around separate crossings.
Minimum and Maximum: fminbnd
1 — Minimum candidate.
[xMinCandidate,fMinCandidate] = fminbnd(f,a,b);2 — Maximum candidate.
xMaxCandidate = fminbnd(@(x)-f(x),a,b);
fMaxCandidate = f(xMaxCandidate);3 — Compare all interior candidates and both endpoints.
xCandidates = [a interiorCandidates b];
fCandidates = f(xCandidates);4 — Select absolute minimum.
[fMin,iMin] = min(fCandidates);
xMin = xCandidates(iMin);5 — Select absolute maximum.
[fMax,iMax] = max(fCandidates);
xMax = xCandidates(iMax);Multiple peaks/valleys: search separate subintervals. One fminbnd call does not locate every extremum.
Linear Systems and Mass Balances
1 — Start from the balance.
% Accumulation = In - Out + Net generation
% Steady state: In - Out + Net generation = 02 — Arrange equations into A*x = b.
A = [a11 a12 a13;
a21 a22 a23;
a31 a32 a33];
b = [b1; b2; b3];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);4 — Classify.
| Condition | Conclusion |
|---|---|
rA < rAb | No exact solution |
rA == rAb && rA < n | Infinitely many solutions |
rA == rAb && rA == n | Unique solution |
Square matrix: rA < n | Numerically singular |
c > 1000 | Ill-conditioned under exam criterion |
Ill-conditioned can still mean unique. A tiny determinant alone is not a reliable numerical singularity test.
5 — Solve when appropriate.
solution = A\b;6 — Extract unknowns and verify.
unknown1 = solution(1);
unknown2 = solution(2);
residual = norm(A*solution-b);Gaussian Elimination: Ordered Formulas
1 — Setup.
N = length(b);
Aug = [A b];
X = zeros(N,1);2 — Pivot-column loop.
for j = 1:N-13 — Rows below pivot.
for i = j+1:N4 — 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:18 — 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.
Useful Chemical-Engineering Model Syntax
| Model | Reusable MATLAB Expression |
|---|---|
| Ideal-gas total-volume guess | V0 = n*R*T/P |
| van der Waals, total volume | f = @(V) (P+a*n^2./V.^2).*(V-n*b)-n*R*T |
| Antoine vapor pressure | Psat = @(T) 10.^(A-B./(C+T)) |
| Binary bubble-point residual | f = @(T) x1*Psat1(T)+x2*Psat2(T)-P |
| Vapor mole fraction | yi = xi*Psati(Tb)/P |
| Arrhenius rate constant | k = @(T) Apre*exp(-Ea./(R*T)) |
| Adsorption equilibrium | K = @(T) K0*exp(-DHads./(R*T)) |
| Catalytic rate | rate = @(T) k(T).*K(T)*CA./(1+K(T)*CA) |
| Recovery fraction | recovery = soluteRecovered/soluteFed |
| Outlet selectivity, same flow | selectivity = CB/CC |
Checks: consistent units; valid domain; small residual; physically sensible results; endpoints for optimization. Reaching maxIter alone does not establish convergence.