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

w+SzPTxlahGOgAAAABJRU5ErkJggg==

aAlgf2vIp1QShAoHAtlbCqQl9mQSkEvr2cHIIIIAAAggggAACCCCAAAIIIIBA+xNIbn+XxBUhgAACCCCAAAIIIIAAAggggAACCCSyAAGpRL47nBsCCCCAAAIIIIAAAggggAACCCDQDgUISLXDm8olIYAAAggggAACCCCAAAIIIIAAAoksQEAqke8O54YAAggggAACCCCAAAIIIIAAAgi0QwECUu3wpnJJCCCAAAIIIIAAAggggAACCCCAQCILEJBK5LvDuSGAAAIIIIAAAggggAACCCCAAALtUICAVDu8qVwSAggggAACCCCAAAIIIIAAAgggkMgCBKQS+e5wbggggAACCCCAAAIIIIAAAggggEA7FCAg1Q5vKpeEAAIIIIAAAggggAACCCCAAAIIJLIAAalEvjucGwIIIIAAAggggAACCCCAAAIIINAOBQhItcObyiUhgAACCCCAAAIIIIAAAggggAACiSxAQCqR7w7nhgACCCCAAAIIIIAAAggggAACCLRDAQJS7fCmckkIIIAAAggggAACCCCAAAIIIIBAIgsQkErku8O5IYAAAggggAACCCCAAAIIIIAAAu1Q4P8BwoFF356aUh4AAAAASUVORK5CYII=

MATLAB General Setup and Output

General Setup and Output

1 — Define a function; for roots, use left side − right side.

f = @(x) <expression>;

2 — Create plotting points.

x = linspace(startValue, endValue, numberOfPoints);
x = startValue:spacing:endValue;
y = f(x);

3 — Elementwise operations.

.*    ./    .^

4 — Plot and label.

plot(x,y,'LineWidth',1.5)
fplot(f,[a b])
hold on
yline(0,'--')
plot(xPoint,f(xPoint),'ro')
xlabel('Variable (units)')
ylabel('Function (units)')
legend('Curve','Location','best')
grid on

5 — Print results.

fprintf('Result = %.6f units\n',result)
fprintf('Residual = %.3e\n',residual)
fprintf('Iterations = %d\n',iter)

%.6f = six decimal places; %.3e = scientific notation; %d = integer; \n = newline.

  
  

Root Finding: Choosing the Method

Given or RequestedUse
Sign-changing bracket; guaranteed interval reductionBisection
Sign-changing bracket; straight-line estimateFalse position
One guess and derivativeNewton–Raphson
Two guesses; no derivativeSecant
Rearrangement x = g(x)Fixed point
Built-in root solver permittedfzero

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 bracket

Bisection 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) < tolF

4 — 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 denominator

4 — 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)) < tolF

Secant 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)) < tolF

Fixed-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)) < tolF

Local convergence criterion:

abs(gDerivative(root)) < 1

Built-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 = 0

2 — 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.

ConditionConclusion
rA < rAbNo exact solution
rA == rAb && rA < nInfinitely many solutions
rA == rAb && rA == nUnique solution
Square matrix: rA < nNumerically singular
c > 1000Ill-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-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.

Useful Chemical-Engineering Model Syntax

ModelReusable MATLAB Expression
Ideal-gas total-volume guessV0 = n*R*T/P
van der Waals, total volumef = @(V) (P+a*n^2./V.^2).*(V-n*b)-n*R*T
Antoine vapor pressurePsat = @(T) 10.^(A-B./(C+T))
Binary bubble-point residualf = @(T) x1*Psat1(T)+x2*Psat2(T)-P
Vapor mole fractionyi = xi*Psati(Tb)/P
Arrhenius rate constantk = @(T) Apre*exp(-Ea./(R*T))
Adsorption equilibriumK = @(T) K0*exp(-DHads./(R*T))
Catalytic raterate = @(T) k(T).*K(T)*CA./(1+K(T)*CA)
Recovery fractionrecovery = soluteRecovered/soluteFed
Outlet selectivity, same flowselectivity = CB/CC

Checks: consistent units; valid domain; small residual; physically sensible results; endpoints for optimization. Reaching maxIter alone does not establish convergence.