% straight line fit to 1965-2016 global temperature data
% Hansen, J., Mki. Sato, R. Ruedy, K. Lo, D.W. Lea, and M. Medina-Elizade,
% 2006: Global temperature change. Proc. Natl. Acad. Sci., 103, 14288-14293,
% doi:10.1073/pnas.0606291103.
% load data from text file
D=load('../data/global_temp.txt');
axis( [1965, 2020, -0.5, 1.0] );
plot(t,d,'r-','LineWidth',3);
plot(t,d,'ro','LineWidth',3);
ylabel('temperature anomaly, deg C');
% least squares solution and predicted data
plot(t,dpre,'b-','LineWidth',3);
fprintf('Caption: Straight line fit (blue line) to temperature anomaly data (red curve)\n');
Caption: Straight line fit (blue line) to temperature anomaly data (red curve)
fprintf('slope: %f +/- %f deg/yr\n', mest(2), 2*sm);
slope: 0.017534 +/- 0.001677 deg/yr
% Hansen, J., Mki. Sato, R. Ruedy, K. Lo, D.W. Lea, and M. Medina-Elizade,
% 2006: Global temperature change. Proc. Natl. Acad. Sci., 103, 14288-14293,
% doi:10.1073/pnas.0606291103.
D=load('../data/global_temp.txt');
% note time shift of 1965, necessary to avoid squaring very big numbers
fprintf('First few rows of data kernel G\n');
First few rows of data kernel G
G(1:10,:)
1 0 0
1 1 1
1 2 4
1 3 9
1 4 16
1 5 25
1 6 36
1 7 49
1 8 64
1 9 81
axis( [1965, 2020, -0.5, 1.0] );
plot(to,d,'r-','LineWidth',3);
plot(to,d,'ro','LineWidth',3);
ylabel('temperature anomaly, deg C');
% least squares solution and predicted data
plot(to,dpre,'b-','LineWidth',3);
fprintf('Caption: Quadratic fit (blue line) to temperature anomaly data (red curve)\n');
Caption: Quadratic fit (blue line) to temperature anomaly data (red curve)
fprintf('coefficients of equation d = m1 + m2*(t-1965) + m3*(t-1965)^2');
coefficients of equation d = m1 + m2*(t-1965) + m3*(t-1965)^2
fprintf('m1: %f +/- %f (95%%)\n', mest(1), 2*sm(1) );
m1: -0.084579 +/- 0.072183 (95%)
fprintf('m2: %f +/- %f (95%%)\n', mest(2), 2*sm(2) );
m2: 0.014783 +/- 0.006545 (95%)
fprintf('m3: %f +/- %f (95%%)\n', mest(3), 2*sm(3) );
m3: 0.000054 +/- 0.000124 (95%)
z = [1, 2, 4, 8, 9, 12, 15, 20]';
fprintf('Kernel for linear case\n');
G
1 1
1 2
1 4
1 8
1 9
1 12
1 15
1 20
fprintf('Kernel for quadratic case\n');
Kernel for quadratic case
G
1 1 1
1 2 4
1 4 16
1 8 64
1 9 81
1 12 144
1 15 225
1 20 400
% acoustic tomography case
% measurements over columns
fprintf('Kernel for tomography case\n');
Kernel for tomography case
G
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
% let the image be square, from (0 to 1) in both x and y
% let it be divided into M=KxL pixels which constitute model parameters
% the pixel array is unwrapped using k=i+(j-1)*K
% let N rays be collected with endpoits at (x1,y1) and (x2,y2).
Dx=1/K; % x dimension of pixel
Dy=1/L; % y dimension of pizel
X1 = [ [0,0]', [0,1]', [0,0.5]' ];
X2 = [ [1,1]', [1,0]', [1, 0.5]' ];
I = zeros(K,L); % image of rays, for plotting purposes
x1=X1(:,n); x2=X2(:,n); % end points of ray
R = sqrt((x2-x1)'*(x2-x1)); % length of ray
t = (x2-x1)/R; % tangent to ray
Nr = floor( R/Ds ); % number of ray segments
x=x1; % starting point along ray
x = x1 + (ir-1)*Ds*t; % point along ray
% indices of pixel containing the point
if( px<1 ) % check that is withing bounds
if( py<1 ) % check that is withing bounds
% index in unwrapped vector of model parameters
I(px,py) = I(px,py)+Ds; % make inage of rays
G(n,k)=G(n,k)+Ds; % increment data kernel
gda_draw(I,'caption Rays');
fprintf('Caption: Ray diagram\n');
fprintf('Transposed data kernel, G-transpose\n');
Transposed data kernel, G-transpose
full(G)'
0.2830 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0.1410 0
0 0.1410 0
0 0 0
% model Mars rover Mossbauer spectra using Lorentzian curves
D=load('../data/mars_soil.txt');
% delete negative velocities
% number of peaks determined by clicking on graph
disp('click on the bottom each peak');
click on the bottom each peak
disp(' click to the left of zero when done');
click to the left of zero when done
axis( [0, 12, min(d), max(d)] );
plot(v,d,'r-','LineWidth',2);
plot(v,d,'ro','LineWidth',3);
xlabel('velocity, mm/s');
title('click bottom of each peak, then left of axis');
% lorentzian curve of peak amplitude a, center velocity v0 and width c
% f(v) = a c^2 / ( (v-v0)^2 + c^2) )
% df/da = c^2 / ( (v-v0)^2 + c^2) )
% df/dv0 = 2 a c^2 (v-v0) / ( (v-v0)^2 + c^2) )^2
% df/dc = 2 a c / ( (v-v0)^2 + c^2) ) - a c^3 / ( (v-v0)^2 + c^2) )^2
% 3 model parameters per lorentzian, (a, v0, c)
% estimate of backgroind level
temp = ((v-m(K+i)).^2+m(2*K+i)^2);
dpre = dpre + m(i)*(m(2*K+i)^2)./temp;
temp = ((v-m(K+i)).^2+m(2*K+i)^2);
G(:,i) = (m(2*K+i)^2)./temp; % d/da
G(:,K+i) = 2*m(i)*(m(2*K+i)^2)*(v-m(K+i))./(temp.^2); % d/dv0
G(:,2*K+i) = 2*m(i)*m(2*K+i)./temp - 2*m(i)*m(2*K+i)^3./(temp.^2); % d/dc
dm = (G'*G+0.001*eye(M))\(G'*dd);
end
0.338978
0.106137
0.041103
0.038910
0.038596
0.038525
0.038508
0.038504
0.038503
0.038502
temp = ((v-m(K+i)).^2+m(2*K+i)^2);
dpre2 = dpre2 + m(i)*(m(2*K+i)^2)./temp;
plot(v,dpre2,'b-','LineWidth',3);
fprintf('Caption: Mossbauer spectral data (red dots) and best fit (blue curve)\n');
Caption: Mossbauer spectral data (red dots) and best fit (blue curve)
% plot for mixing example figure
M=5; % number of elements
s1 = [1, 0, 2, 0.5, 3]'; % endmember (factor) 1
s2 = [3, 0.7, 1, 2.5, 2]'; % endmember (factor) 2
% make some simple vector plots
gda_draw(' ', s1, ' ', sx1, ' ', sx2, ' ', sx3, ' ', sx4, ' ', sx5, ' ', s2);
fprintf('Caption: Portrayal of linear mixing between two end members (left and right),\n');
Caption: Portrayal of linear mixing between two end members (left and right),
fprintf('each with five elements\n');
% filter example, using seismometer response as an exemplary filter
% Note: this routine uses fairly advanced techniques to
% construct a seismometer response filter that are not
% going to be explained in sufficient detail for a reader
% to follow. Sorry about that ... Note that the response
% (which is just a time series) is stored in a the vector
% uoutf1() and in the file ../data/seismometer_response.txt.
% response of the Broadband East component of station J59A of
% of the US Transportable array is in this file (in inscruitable
respfile1='../data/TA_J59A_BHE.txt';
% read the IRIS RESP file for this seismometer
[ Nzeroes, zero, Npoles, pole, F, status ] = gda_read_resp( respfile1 );
% set up an input ground motion with a single small spike
% apply station 1 response, filter result, and plot
[ uout1, status ] = gda_apply_response( uin, Dt, respfile1 );
uoutf1 = gda_chebyshevfilt(uout1, Dt, flow, fhigh);
% save the response filter in a text file
dlmwrite('../data/seismometer_response.txt', [t, uoutf1], 'delimiter', '\t');
% plot a spike in ground motion
axis( [0, Dt*(N-1), -abs(max(uin)), abs(max(uin)) ] );
plot( Dt*[0:N-1]', uin, 'k-', 'LineWidth', 3 );
title('model parameters m');
ylabel('velocity, microns');
% plot the response of the seismometer
axis( [0, Dt*(N-1), -max(abs(uoutf1)), max(abs(uoutf1)) ] );
plot( Dt*[0:N-1]', uoutf1, 'k-', 'LineWidth', 3 );
ylabel('digital counts');
fprintf('(caption: (top) Ground velocity is an impuslive time series (black curve).\n');
(caption: (top) Ground velocity is an impuslive time series (black curve).
fprintf('(right) Seismometer response is a broad pulse\n');
(right) Seismometer response is a broad pulse
% set up an input seismogram with several small spikes
% and a smoothly varying function
uin(floor(N/4)) = (1e-6);
uin(floor(8+N/2)) = 0.5*(1e-6);
uin(floor(17+N/2)) = 0.5*(1e-6);
uin(750:850) = 0.5*(1e-6)*sin( pi*([750:850]'-750)/100 );
[ uout1, status ] = gda_apply_response( uin, Dt, respfile1 );
uoutf1 = gda_chebyshevfilt(uout1, Dt, flow, fhigh);
% plot the complicated ground motion
axis( [0, Dt*(N-1), -max(abs(uin)), max(abs(uin)) ] );
plot( Dt*[0:N-1]', uin, 'r-', 'LineWidth', 3 );
title('model parameters m');
ylabel('velocity, microns');
% plot the corresponding seismometer response
axis( [0, Dt*(N-1), -max(abs(uoutf1)), max(abs(uoutf1)) ] );
plot( Dt*[0:N-1]', uoutf1, 'b-', 'LineWidth', 3 );
title('Seismometer recording');
ylabel('digital counts');
fprintf('Caption: (top) Ground velocity. (Bottom) Seismometer recording\n');
Caption: (top) Ground velocity. (Bottom) Seismometer recording
fprintf('of ground velocity.\n');
% plot example of filter, both as wiggles and a matrix
% load exemplary filter (for a seismometer)
D = load('../Data/seismometer_response.txt');
% build data kernel corresponding to convolution by this filter
G = toeplitz( g, [g(1), zeros(1,N-1)] );
% plot filter, as wiggles
set(gca,'XAxisLocation','top');
axis( [-max(abs(g)), max(abs(g)), t(1), t(end)] );
plot(G(:,clist(i)),t,'k-','LineWidth',3);
xlabel(sprintf('g(t-%.1f)',(clist(i)-1)*Dt));
m(1:floor(N/5)) = 0.5*1e-6*sin( pi*([1:floor(N/5)]'-1)/(N/5-1));
% plot input (model parameters, ground motion)
set(gca,'XAxisLocation','top');
axis( [-max(abs(m)), max(abs(m)), t(1), t(end)] );
plot(m,t,'b-','LineWidth',3);
d = G*m; % perform convolution
% plot output (data, seismometer response)
set(gca,'XAxisLocation','top');
axis( [-max(abs(d)), max(abs(d)), t(1), t(end)] );
plot(d,t,'r-','LineWidth',3);
fprintf('Caption: Graphical depiction of convolution process. (left 6 plots)\n');
Caption: Graphical depiction of convolution process. (left 6 plots)
fprintf('(7th plot) Ground veocity. (8th plot) Seismometer recording.\n');
(7th plot) Ground veocity. (8th plot) Seismometer recording.
gda_draw(G, 'caption G', m, 'caption m', '=', d, 'caption d');
fprintf('Caption: Graphical depiction on the equation G m = d, in the\n');
Caption: Graphical depiction on the equation G m = d, in the
fprintf('case where G m represents a convolution of m with a seismometer\n');
case where G m represents a convolution of m with a seismometer
% illustrations of different types of solutions
% solution is estimate and error bars
axis( [mmin, mmax, mmin, mmax] );
plot( [mbar(1), mbar(1)], [mbar(2)-2*sm(2), mbar(2)+2*sm(2)], 'r-', 'LineWidth', 3 );
plot( [mbar(1)-2*sm(1), mbar(1)+2*sm(1)], [mbar(2), mbar(2)], 'r-', 'LineWidth', 3 );
plot( mbar(1), mbar(2), 'ko', 'LineWidth', 3 );
% use tensor product, m1 increases along rows, m2 along columns
p = exp(-0.5*(m-mbar(1)).^2/sm2(1)) * exp(-0.5*(m-mbar(2)).^2/sm2(2))';
pnorm = (Dm^2)*sum(p(:));
axis( [mmin, mmax, mmin, mmax] );
% trick for checking that orientation is right
% put a defect at m1=20, m2=50 and see if it plots correctly
imagesc([0,100],[0,100],p');
m1real = (sm(1)*randn(N,1)+mbar(1));
m2real = (sm(2)*randn(N,1)+mbar(2));
axis( [mmin, mmax, mmin, mmax] );
plot( m1real, m2real, 'k.', 'LineWidth', 5 );
fprintf('Caption: Illustration of different kinds of solutions to an inverse\n');
Caption: Illustration of different kinds of solutions to an inverse
fprintf('problem. (left) Estimate (circle) and 95 pecent confidence intervals (red)\n');
problem. (left) Estimate (circle) and 95 pecent confidence intervals (red)
fprintf('(middle) Probability density function (colors). (right) Ensemble of probable\n');
(middle) Probability density function (colors). (right) Ensemble of probable
fprintf('solutions (dots).');
% range of pdfs, from simple to complicated
p1a = exp(-0.5*(m1-m1bar).^2/(sm^2));
p1b = exp(-0.5*(m1-m1bara).^2/(sma^2))+0.4*exp(-0.5*(m1-m1barb).^2/(sma^2));
% absurdly complicated distribution
Ac=random('Uniform',0,1);
m1barc=random('Uniform',0,10);
smc=random('Uniform',0.05,1);
tmp=Ac*exp(-0.5*(m1-m1barc).^2/(smc^2));
set(gcf,'pos',[10, 10, 800, 250]);
% plot unimodal distribution
axis( [mmin, mmax, 0, 0.5 ] );
plot(m1,p1a,'b-','LineWidth',3);
% plot bimodal distribution
axis( [mmin, mmax, 0, 0.5 ] );
plot(m1,p1b,'b-','LineWidth',3);
% plot complicated distribution
axis( [mmin, mmax, 0, 0.5 ] );
plot(m1,p1c,'b-','LineWidth',3);
fprintf('Caption: (Left) Unimodal pdf. (Middle) Bimodal pdf. (right) Complicated pdf\n.');
Caption: (Left) Unimodal pdf. (Middle) Bimodal pdf. (right) Complicated pdf
.