Curve fitting: Higher order PolynomialsThe formula: ax + by + cz = 1 A= [-1 -1 -3;1 0 0;2 1 2;2 3 6]; b = [1;1;1;1]; x = A\b; NOTE: Ax = b...backslash(\) to solve for a,b,c Curve fitting: linearizationExponential linearized function The formula is y = bemx: x = [1 2 3 4]; y = [6 3 2 1]; Givena = polyfit(x,log(y),1) >>a = -0.5781 2.3411; m = a(1) >>m = -0.5781; b = exp(a(2)); >>b = 10.3923; The final product: y = 10.39e-0.5781x Eigenvalues & EigenvectorsA=[2 -1 0;-1 2 -1;0 -1 2]; a=eig(A); [V,D] = eig(A) V - eigenvectorsV(:,1) :get 1st columnv1 = V(:,1); v1 = v1/v1(1); : normalise eigenvectorODEs using ode45a = 0.1; b = 0.002; c = 0.0025; d = 0.2; f = @(t,x) [-ax(1)+bx(1)*x(2); dx(2)-cx(1)*x(2)]; X0 = [20; 100]; Tspan = linspace(0,150,1000); [T,X] = ode45(f,Tspan,X0); plot(T,X,'LineWidth',2); ODEs using ode45a = 0.1; b = 0.002; c = 0.0025; d = 0.2; f = @(t,x) [-ax(1)+bx(1)*x(2); dx(2)-cx(1)*x(2)]; X0 = [20; 100]; Tspan = linspace(0,150,1000); [T,X] = ode45(f,Tspan,X0); plot(T,X,'LineWidth',2); Gauss Integration
f=@(x) 1/2*sqrt(16+(x+3).^4)./(x+3)^2; G2=f(-1/sqrt(3))+f(1/sqrt(3)); >>G2= 1.1289; and G3=5/9f(-sqrt(3/5))+8/9f(0)+5/9*f(sqrt(3/5)); >>G3= 1.1319; |
ODEs: Boundary-value problems (BVPs)
y0=1;y1=2; % N = 1/h;h = 0.2 i = [1:N-1]'; xi = i*h; % Interior nodes A=(h^2-2)*eye(N-1); % Diagonal part of A A=A+diag(1-0.5hxi(2:N-1),-1);% Add sub- and superdiagonal A=A+diag(1+0.5hxi(1:N-2),+1);% Add sub- and superdiagonal b = 2h^2xi;% RHS refined formula b(1)=b(1)-(1-0.5hxi(1))*y0;% Add boundary contributions b(N-1)=b(N-1)-(1+0.5hxi(N-1))*y1;% Add boundary contributions y = sparse(A)\b; % Solve Finite Difference: Laplace&Poisson PDEsMultiple Integralsf = @(x,y) x.^2-y.^2-1; a = 0; b = 3; g = 0; p = 1; integral2(f,a,b,g,p); >>ans = 5.000000000001309; or f = @(x,y) y; a = 0; b = pi/4; g = @(x) sin(x); p = @(x) cos(x); m = integral2(f,a,b,g,p); >>m = 0.250000000000013; Curve fitting& Interpolation
y = a0 + a1x + a2x2: x = [1; 2; 3; 4]; y = [6; 3; 2; 1]; A = [ones(4,1) x x.ˆ2]; a = A\y a = 9.5000 -4.1000 0.5000 a2 = polyfit(x,y,2) xx = linspace(0,5); y2 = polyval(a2,xx); plot(x,y,'r.',xx,y2,'b-') Error,Residual,Norms,Condition numberx = [1; -1; 3; -5]; norm(x,inf) ||x||inf(subscript)||A||2 = (||Ax||2)/(||x||2) ~ cond(A) = (||A||)(||A-1||) ~ cond(A) >> 1:ill-conditioned&close singular matrix~ cond(A) ≈ 1:well-conditioned~ decimals digits that trustworthy = 10-a = 10c x 10-d a = d-c ~r = b - Ax det (A) = 0; No A-1; Singular matrix test singularity: cond,condest,rank |
Built-in Functions
f=@(x)sqrt(1+x.^4)./x.^2; L=integral(f,1,2); >>L=1.132090393305918; or y=[0 0.3 0.5 1 1.5 2 2.5 3 4 5]; v=[0 0.4 0.5 0.56 0.6 0.63 0.66 0.68 0.71 0.72]; Q=10*trapz(y,v); >>Q=30.8000; Euler's MethodRunge-Kutta Methodsf = @(x,y) -x*y; %Define rhs N = 100; % Number of steps .... for i = 1:N K1 = f(x(i),y(i)); K2 = f(x(i)+0.5h,y(i)+0.5h*K1); K3 = f(x(i)+0.5h,y(i)+0.5h*K2); K4 = f(x(i+1),y(i)+h*K3); x(i+1) = x(i)+h; y(i+1) = y(i)+(1/6)h(K1+2K2+2K3+K4); end Runge-Kutta Methodsf = @(x,y) -x*y; %Define rhs N = 100; % Number of steps .... for i = 1:N K1 = f(x(i),y(i)); K2 = f(x(i)+0.5h,y(i)+0.5h*K1); K3 = f(x(i)+0.5h,y(i)+0.5h*K2); K4 = f(x(i+1),y(i)+h*K3); x(i+1) = x(i)+h; y(i+1) = y(i)+(1/6)h(K1+2K2+2K3+K4); end Numerical Differentiation |
Composite Simpson's Rule
f = @(x) sqrt(1+x.^4)./x.^2; a = 1; b = 2; N = 100; h = (b-a)/N; x = a+[0:N]*h; fx = f(x); L = h/3(fx(1)+4sum(fx(2:2:N))+2*sum(fx(3:2:N-1))+fx(N+1)) or w = ones(size(x)); w(2:2:N) = 4; w(3:2:N-1) = 2; L = h/3sum(w.f(x)) >>L = 1.132090394695845; Composite Trapezium Rulef = @(x) sqrt(1+x.^4)./x.^2; a = 1; b = 2; N = 100; h = (b-a)/N; x = a+[0:N]*h; fx = f(x); L = h3( 0.5fx(1) + sum(fx(2:N)) + 0.5*fx(N+1) ) or w = ones(size(x)); w(1) = 0.5; w(N+1) = 0.5; L = hsum(w.f(x)); >>L = 1.132101672788808; Numerical Partial DifferentiationModified EulerSpline
x = [22 35 48 61 74]; y = [320 490 540 500 480]; yy = spline(x,y,42); not-a-knot >>yy = 531.1971; xx = linspace(20,80,1000); *clamped conditions: [0 y 0] ss = spline(x,y,xx); plot(x,y,'r.',xx,ss,'b-'); |
Cheatography
https://cheatography.com
Numerical Methods 262 Cheat Sheet (DRAFT) by Kamva
Doing numerical methods using MATLAB to compute the certain codes needed.
This is a draft cheat sheet. It is a work in progress and is not finished yet.