setdefaults
+
%plot --format svg
+
The interp2 function uses splines to interpolate between data points. What are three options for interpolation:
+cubic spline
+piecewise cubic spline
+linear spline
+quadratic spline
+fourth-order spline
+Numerical integration is a general application of the Newton-Cotes formulas. What is the first order approximation of the Newton-Cotes formula? *
+trapezoidal rule
+Simpson's 1/3 rule
+Simpson's 3/8 rule
+linear approximation of integral
+constant approximation of integral (sum(f(x)*dx))
+Is spring here to stay?
+ +The time is now.
+Final Project Sheet?
+can you provide some sort of hw answer key?
+What's the most 1337 thing you've ever done?
+Can we do out more examples by hand (doc cam or drawing on computer notepad) instead of with pre-written code?
+Favorite movie?
+fe_c=load('structural_steel_psi.jpg.dat');
+fe_cr =load('stainless_steel_psi.jpg.dat');
+
+fe_c_toughness=trapz(fe_c(:,1),fe_c(:,2));
+fe_cr_toughness=trapz(fe_cr(:,1),fe_cr(:,2));
+
+fprintf('toughness of structural steel is %1.1f psi\n',fe_c_toughness)
+fprintf('toughness of stainless steel is %1.1f psi',fe_cr_toughness)
+
Evaluating an integral, we assumed a polynomial form for each Newton-Cotes approximation.
+If we can evaluate the function at any point, it makes more sense to choose points more wisely rather than just using endpoints
+ +Let us set up two unknown constants, $c_{0}$ and $x_{0}$ and determine a wise place to evaluate f(x) such that
+$I=c_{0}f(x_{0})$
+and I is exact for polynomial of n=0, 1
+$\int_{a}^{b}1dx=b-a=c_{0}$
+$\int_{a}^{b}xdx=\frac{b^2-a^2}{2}=c_{0}x_{0}$
+so $c_{0}=b-a$ and $x_{0}=\frac{b+a}{2}$
+$I=\int_{a}^{b}f(x)dx \approx (b-a)f\left(\frac{b+a}{2}\right)$
+ +f1=@(x) x+1
+f2=@(x) 1/2*x.^2+x+1
+f3=@(x) 1/6*x.^3+1/2*x.^2+x
+plot(linspace(-18,18),f3(linspace(-18,18)))
+
fprintf('integral of f1 from 2 to 3 = %f',f2(3)-f2(2))
+fprintf('integral of f1 from 2 to 3 ~ %f',(3-2)*f1(3/2+2/2))
+
+fprintf('integral of f2 from 2 to 3 = %f',f3(3)-f3(2))
+fprintf('integral of f2 from 2 to 3 ~ %f',(3-2)*f2(3/2+2/2))
+
This process is called Gauss Quadrature. Usually, the bounds are fixed at -1 and 1 instead of a and b
+$I=c_{0}f(x_{0})$
+and I is exact for polynomial of n=0, 1
+$\int_{-1}^{1}1dx=b-a=c_{0}$
+$\int_{-1}^{1}xdx=\frac{1^2-(-1)^2}{2}=c_{0}x_{0}$
+so $c_{0}=2$ and $x_{0}=0$
+$I=\int_{-1}^{1}f(x)dx \approx 2f\left(0\right)$
+Now, integrals can be performed with a change of variable
+a=2
+b=3
+x= 2 to 3
+or $x_{d}=$ -1 to 1
+$x=a_{1}+a_{2}x_{d}$
+at $x_{d}=-1$, x=a
+at $x_{d}=1$, x=b
+so
+$x=\frac{(b+a) +(b-a)x_{d}}{2}$
+$dx=\frac{b-a}{2}dx_{d}$
+$\int_{2}^{3}x+1dx=\int_{-1}^{1}\left(\frac{(2+3) +(3-2)x_{d}}{2} ++1\right) +\frac{3-2}{2}dx_{d}$
+$\int_{2}^{3}x+1dx=\int_{-1}^{1}\left(\frac{5 +x_{d}}{2} ++1\right) +\frac{3-2}{2}dx_{d}$
+ +$\int_{2}^{3}x+1dx=\int_{-1}^{1}\left(\frac{7}{4}+\frac{1}{4}x_{d}\right)dx_{d}=2\frac{7}{4}=3.5$
+ +function I=gauss_1pt(func,a,b)
+ % Gauss quadrature using single point
+ % exact for n<1 polynomials
+ c0=2;
+ xd=0;
+ dx=(b-a)/2;
+ x=(b+a)/2+(b-a)/2*xd;
+ I=func(x).*dx*c0;
+end
+
gauss_1pt(f1,2,3)
+
Matlab/Octave built-in functions use two types of adaptive quadrature to increase accuracy of integrals of functions.
+quad
: Simpson quadrature good for nonsmooth functions
quadl
: Lobatto quadrature good for smooth functions
q = quad(fun, a, b, tol, trace, p1, p2, …)
+fun : function to be integrates
+a, b: integration bounds
+tol: desired absolute tolerance (default: 10-6)
+trace: flag to display details or not
+p1, p2, …: extra parameters for fun
+quadl has the same arguments
+
% integral of quadratic
+quad(f2,2,3)
+quadl(f2,2,3)
+f3(3)-f3(2)
+
Expanding the Taylor Series:
+$f(x_{i+1})=f(x_{i})+f'(x_{i})h+\frac{f''(x_{i})}{2!}h^2+\cdots$
+ +x=linspace(-pi,pi);
+y_smooth=sin(x);
+y_noise =y_smooth+rand(size(x))*0.1-0.05;
+plot(x,y_smooth,x,y_noise)
+title('Low noise in sin wave')
+
% Central Difference derivative
+
+dy_smooth=zeros(size(x));
+dy_smooth([1,end])=NaN;
+dy_smooth(2:end-1)=(y_smooth(3:end)-y_smooth(1:end-2))/2/(x(2)-x(1));
+
+dy_noise=zeros(size(x));
+dy_noise([1,end])=NaN;
+dy_noise(2:end-1)=(y_noise(3:end)-y_noise(1:end-2))/2/(x(2)-x(1));
+
+plot(x,dy_smooth,x,dy_noise)
+title('Noise Amplified with derivative')
+
Options:
+Fit a function and take derivative
+a. splines won't help much
+b. best fit curve (better)
+Smooth data (does not matter if you smooth before/after derivative)
+y_spline_i1=interp1(x,y_noise,x+0.1);
+y_spline_in1=interp1(x,y_noise,x-0.1);
+dy_spline=(y_spline_i1-y_spline_in1)/0.2;
+plot(x,dy_spline,x,dy_noise)
+legend('deriv of spline','no change')
+
Z=[sin(x')];
+a=Z\y_noise'
+plot(x,a*sin(x),x,y_noise)
+
plot(x,a*cos(x),x,dy_smooth,'o')
+
a=[1,2,3,4,5]
+pa=[a(4/2:-1:1) a a(end:-1:end-4/2+1)]
+size(pa)
+
% Smooth data
+N=10; % average data between 10 points (forward/backward)
+y_data=[y_noise(N/2:-1:1) y_noise y_noise(end:-1:end-N/2+1)];
+y_filter=y_data;
+for i=6:length(x)
+ y_filter(i)=mean(y_data(i-5:i+5));
+end
+y_filter=y_filter(N/2:end-N/2-1);
+size(y_filter)
+plot(x,y_filter,x,y_noise,'.')
+
dy_filter=zeros(size(x));
+dy_filter([1,end])=NaN;
+dy_filter(2:end-1)=(y_filter(3:end)-y_filter(1:end-2))/2/(x(2)-x(1));
+
+plot(x,dy_smooth,x,dy_filter,x,dy_noise,'.')
+title('Noise Amplified with derivative')
+
+