Sunteți pe pagina 1din 2

MATH 272 Reference Sheet Creating matrices Colons: 1:N = 1, 2, ... N 5:2:11 = 5, 7, 9, 11 Loops for (i = 1:10) ...

end

Page 1

linspace: linspace(0, 20) - 100 evenly spaced points from i = 1; 0 to 20 while (i <= 10) linspace(0, 20, 500) - 500 evenly spaced points ... i = i+1; Uniform Matrices: end A = zeros(r, c) - r c matrix, all zeros A = sparse(r, c) - r c matrix, all zeros, in sparse Systems of Linear Equations format Solving Ax = b with A an n n matrix, b an n 1 A = ones(r, c) - r c matrix, all ones vector. A = ones(r, 1) - r-long column vector, all ones Solving Ax = b in MATLAB: x = A \ b; Accessing, Modifying Matrices V = 2:2:10 - 5 element vector, 2,4,6,8,10 V(3) - 3rd element of V (6) V(end) - gives last element of V (10) A = ones(10, 20) - create A a 10 20 matrix of 1s A(1:3,10:20) = 2 - Changes entries in rows 1-3 and cols 10-20 (inclusive) to value 2. A(end, 1) - Gets last entry in column 1 Non-Linear Equations Polynomials roots(c) where c = vector holding polynomial coecients, in order from highest to lowest power. Bisection Start at [a, b], with f(a), f(b) opposite signs Find midpoint, c and f(c) Replace a or b with c, maintaining opposite signs at interval ends Secant deriv_estimate =
f (x 1 ) f (x 0 )

Reading And Writing to File x1 x0 dlmwrite(matrix_filename.txt, M); - writes M xnew = x1 f (x1 )/ deriv_estimate to comma-separated le M = dlmread(matrix_filename.txt); - reads M fzero from comma-separated le [res] = fzero(fun,x0, options) Same will work with xlsread on Excel les. Assorted Matrix Functions Colormaps and surfaces A - transpose of A [x,y] = meshgrid(xv, yv); - xv and yv are inv(A) - inverse of A vectors where the x and y grid points lie. pcolor(x, y, T); - for matrices x, y, and T of same Animation size F(i) = getframe; surf(x, y, T); - same as pcolor, but uses T for movie(F, rep, fps); both height and color shading flat or shading interp - change shading of pcolor, surf

MATH 272 Reference Sheet Interpolation Fitting through the points x, y pp - piecewise polynomial information xx, yy - display points in x and y Polynomial N = length(x); p = polyfit(x, y, N-1); yy = polyval(p, xx); Spline yy = spline(x, y, xx) pp = spline(x, y) yy = ppval(pp, xx) Spline with End Slopes yy = spline(x, [slope1, y, slope2], xx) pp = spline(x, [slope1, y, slope2]) yy = ppval(pp, xx) Monotone Hermitian Polynomials yy = pchip(x, y, xx) pp = pchip(x, y) yy = ppval(pp, xx) Function Fitting Not tested on exam. ODE Solvers f (t, y ) are formulas for Eulers Method

Page 2

dy dt

(or slopes)

ti+1 = t + t yi+1 = yi + f (ti , yi )t

Heuns Method ti+1 = t + t y = yi + f (ti , yi )t 1 )) t yi+1 = yi + (f (ti , yi ) + f (ti+1 , y 2 MATLAB ODE Solvers [t, y] = ode45(f,tspan,y0,options) [t, y] = ode23(f,tspan,y0,options) [t, y] = ode23s(f,tspan,y0,options) Common ODE Options odeset(AbsTol, 1e-12, RelTol, 1e-8); odeset(Events, event_func);

Numerical Integration Left-Hand Rule int = lhr(func, a, b, n)


n

Area = x
i=1

f ( xi )

Trapezoidal Rule int = trap(func, a, b, n) x n Area = f (xi ) + f (xi+1 ) 2 i=1 Simpsons Rule int = simp(func, a, b, n) (2x) n Area = (f (xi ) + 4f (xi+1 ) + f (xi+2 )) 6 i=1 but you must increment i by 2 each time Adaptive Simpsons Rule int = quad(func, a, b, tol, trace)

Optimization func - function to be minimized x0 - initial value (can be a vector) Simplex [xopt, fopt] = fminsearch(func, x0, options) Unconstrained Gradient-Based Minimization [xopt, fopt] = fminunc(func, x0, options) Constrained Gradient-Based Minimization [xopt, fopt] = fmincon(fun,x0,A,b,Aeq,beq,... lb,ub,nonlcon,options) Common Optimization Options optimset(Display, iter); optimset(MaxIter, 50) optimset(LargeScale, off) Project-related formulas 1 g x2 y = y0 + tan(0 )x 2 2 v0 cos2 (0 )

S-ar putea să vă placă și