calculate integration in matlab for environmental physics data series - matlab

The following diagram is a schematic of a lake, and the equation illustrates how to calculate the effective heat flux of a lake.
where S is a vector of surface fluxes, q is short wave radiation, h is depth of the mixed layer, and z is the depth of the lake. For example:
q0 = 400+(1-400).*rand(100,1); % This is the short wave radiation
kd = 0.8; % extinction coefficient
h = 10; % depth of the surface mixed layer
for i = 1:length(q0); % loop for calculating short wave radiation at depth h
qh(i) = q0(i).*exp(-kd*h); % here, qh is calculated according to the Lambert Beer law
end
given
dz = 0.5
and z varies from 0 (surface) to depth h in increments of dz i.e.
z = 0:dz:h
how would I calculate the last portion of this equation in matlab i.e. how to calculate q at depth z between the surface and h? which is expressed here as an integral?
Apologies if this should be on another one of the stack overflow forums but it seems more related to programming than pure physics or maths questions.

To integrate this correctly, you will need compute all values of q(z) in the range [0, h]. If q0 and qh are N-by-1 column vectors, this means that q should be an N-by-M matrix, where M is the number of sample points in the range [0, h].
First, lets define z properly:
z = linspace(0, h, 200); %// M=200, but it's an arbitrary number to your choosing
The computation of q can be reduced to:
q = q0 * exp(-kd * z);
and qh actually equals to the final column of q, i.e q(:, end).
The integral itself can be approximated to a sum and computed using sum:
dz = z(2) - z(1);
I = sum(q, 2) * dz;
P.S.
Since q(z) = e(-kd ·z), it's simple enough for you to compute the integral analytically:
I = q0 * (1 - exp(-kd * h)) / kd;

Related

Is the PrincipalAxisLength measurement equivalent to the object diameter in the regionprops3 output?

I am using the regionprops3 command to compute properties of an object in MATLAB. I thought PrincipalAxisLength is the diameter of my object but it is not.
I created a binary image I containing an ellipsoid with radii (40, 10, 10). I get these values from regionprops3:
sum(I(:)) = 16741
stats.volume = 16741
stats.EquivDiameter = 31.739 % 4/3*pi*(31.739/2)^3=16741
stats.PrincipalAxisLength = [71.535, 17.936, 17.908]
If PrincipalAxisLength is the 3 diameters of an ellipsoid then it's volume is:
4/3*pi*71.535/2*17.936/2*17.908/2 = 12031
Which is not equivalent to above volume.
So, how can I compute the radii of the ellipsoid?
The documentation for the PrincipalAxisLength shape measurement describes it as (emphasis mine):
Length (in voxels) of the major axes of the ellipsoid that have the same normalized second central moments as the region, returned as 1-by-3 vector. regionprops3 sorts the values from highest to lowest.
In other words, it fits a multivariate probability distribution to the region and calculates the central moments for that distribution, returning the second central moment (i.e. variance) for the PrincipalAxisLength measurement.
The volume discrepancy you're seeing is due to the fact that these central moments don't define a bounding ellipsoid fit to the data in the region, just the variance of a probabilistic fit to the data distributed within it. For a case where your region contains an ellipsoid shape, it will underestimate the extent of the ellipsoid (and thus the total volume). Here's some code to visualize this:
% Create 3D binary data containing ellipsoid:
[X, Y, Z] = meshgrid(-50:50, -20:20, -20:20);
R = [40 10 10];
I = ((X./R(1)).^2 + (Y./R(2)).^2 + (Z./R(3)).^2 <= 1);
% Calculate statistics with regionprops3:
stats = regionprops3(I, 'all');
L = stats.PrincipalAxisLength./2;
% Create ellipsoid surface from second central moments and plot:
[x, y, z] = ellipsoid(0, 0, 0, L(1), L(2), L(3), 100);
surf(x, y, z, 'FaceAlpha', 0.5, 'FaceColor', 'r', 'EdgeColor', 'none');
axis equal;
hold on;
% Create image of middle slice through ellipsoid and plot:
imageSlice = 256.*uint8(I(:, :, 21));
imageSlice = cat(3, imageSlice, imageSlice, imageSlice);
image(-50:50, -20:20, imageSlice);
view(0, 90);
And here's the final plot you get:
If you're wanting to fit an ellipsoid surface to your binary object, you could probably use the PrincipalAxisLength measurement as a starting guess for the true axes lengths. There's this function on the File Exchange that does ellipsoid fitting, but that uses a set of surface points as opposed to a binary volume. Perhaps you can adapt that code to fit your needs.
Update
The link provided by Cris in a comment suggests that a clear mathematical relationship could be determined for how the second central moments and the ellipse radii are related. Although I haven't had a chance to work through the math yet, I noticed that simply scaling PrincipalAxisLength by sqrt(5/16) gives values tantalizingly close to R:
>> sqrt(5/16).*stats.PrincipalAxisLength
ans =
40.008372204885049 9.970908565527971 9.970908565527971
I was facing the same problem some time ago for identifying ellipsoids corresponding to binary regions. By developing the maths it is possible retrieve a factor of sqrt(5) for converting the square root of eigen values to ellipsoid radiusses.
The regionprops3 function from Matlab uses a factor of 4, therefore the factor sqrt(5/16) fixes the difference.
The maths involve integration using spherical coordinates, and linearisation of trigonometric expression.
As I could not find any other document or resource explaining it, I propose to write it here (hoping this is adequate...).
Development
Definitions
Let a>b>c be the three radiusses of the ellipsoid.
We can assume the ellipsoid is centered and aligned with the three main axes,
the longest radius along x, the smallest one along z.
In that case, the eigen values correspond to the three
second-order centered moments lambda_1 = mu200, lambda_2 = mu020, and lambda_2 = mu002.
The idea is then to compute explicitely these values from the parameters of the ellipsoid.
For mu200, the definition is:
mu200 = \int \int \int I(x,y,z) x^2 dx dy dz
with I(x,y,z) being 1 within the ellipsoid, and 0 otherwise.
Integration using spherical coordinates
For the integration, it is more convenient to use spherical coordinates (rho, theta, phi), corresponding to the radius, inclination with the vertical, and azimuth.
They corresponds to:
x = rho * a * cos(phi) * sin(theta)
y = rho * b * sin(phi) * sin(theta)
z = rho * c * sin(theta)
The change of coordinates requires the use of the Jacobian of the transform.
In that case, its determinant is
a * b * c * rho^2 * sin(theta)
The moment integral for mu200 is then
mu200 = a * b *c \int_0^{2*pi} \int_0^pi \int_0^1 (a * rho * cos(phi) * sin(theta) )^2 * rho^2 * sin(theta) drho dtheta dphi
Can be simplified into
mu200 = ( a^3 * b * c / 5 ) \int_0^{2*pi} \int_0^pi (sin(theta))^3 dtheta (cos(phi))^2 dphi
The integral over theta can be developed using the linearisation of sin^3 theta:
sin^3(theta) = (3/4) * sin(theta) - (1/4) * sin(theta)
Then the inner integral becomes:
\int_0^pi (sin(theta))^3 dtheta = 4/3
Incorporating into the full integral:
mu200 = ( a^3 * b * c / 5 ) * (4/3) \int_0^{2*pi} (cos(phi))^2 dphi
mu200 = ( a^3 * b * c / 5 ) * (4/3) * (1/2) 2*pi
mu200 = (4 * pi / 3) * (a^3 * b * c / 5)
Identification
Denoting by m000 = (4 * pi / 3) * a * b * c the volume of the ellipsoid, one gets:
mu200 = (a^2 / 5) * V
In regionprops3 function, the covariance matrix is obtained by normalizing by the volume.
We therefore have:
lambda_1 = a^2 / 5
and the relation:
a = sqrt(5) * sqrt(lambda1)

Matlab - plot speed vector of a satellite in Keplerian orbit

I have to plot the speed vector of an object orbiting around a central body. This is a Keplerian context. The trajectory of object is deduced from the classical formula ( r = p/(1+e*cos(theta)) with e=eccentricity.
I manage into plotting the elliptical orbit but now, I would like to plot for each point of this orbit the velocity speed of object.
To compute the velocity vector, I start from classical formulas (into polar coordinates), below the 2 components :
v_r = dr/dt and v_theta = r d(theta)/dt
To take a time step dt, I extract the mean anomaly which is proportional to time.
And Finally, I compute the normalization of this speed vector.
clear % clear variables
e = 0.8; % eccentricity
a = 5; % semi-major axis
b = a*sqrt(1-e^2); % semi-minor axis
P = 10 % Orbital period
N = 200; % number of points defining orbit
nTerms = 10; % number of terms to keep in infinite series defining
% eccentric anomaly
M = linspace(0,2*pi,N); % mean anomaly parameterizes time
% M varies from 0 to 2*pi over one orbit
alpha = zeros(1,N); % preallocate space for eccentric anomaly array
%%%%%%%%%%
%%%%%%%%%% Calculations & Plotting
%%%%%%%%%%
% Calculate eccentric anomaly at each point in orbit
for j = 1:N
% initialize eccentric anomaly to mean anomaly
alpha(j) = M(j);
% include first nTerms in infinite series
for n = 1:nTerms
alpha(j) = alpha(j) + 2 / n * besselj(n,n*e) .* sin(n*M(j));
end
end
% calcualte polar coordiantes (theta, r) from eccentric anomaly
theta = 2 * atan(sqrt((1+e)/(1-e)) * tan(alpha/2));
r = a * (1-e^2) ./ (1 + e*cos(theta));
% Compute cartesian coordinates with x shifted since focus
x = a*e + r.*cos(theta);
y = r.*sin(theta);
figure(1);
plot(x,y,'b-','LineWidth',2)
xlim([-1.2*a,1.2*a]);
ylim([-1.2*a,1.2*a]);
hold on;
% Plot 2 focus = foci
plot(a*e,0,'ro','MarkerSize',10,'MarkerFaceColor','r');
hold on;
plot(-a*e,0,'ro','MarkerSize',10,'MarkerFaceColor','r');
% compute velocity vectors
for i = 1:N-1
vr(i) = (r(i+1)-r(i))/(P*(M(i+1)-M(i))/(2*pi));
vtheta(i) = r(i)*(theta(i+1)-theta(i))/(P*(M(i+1)-M(i))/(2*pi));
vrNorm(i) = vr(i)/norm([vr(i),vtheta(i)],1);
vthetaNorm(i) = vtheta(i)/norm([vr(i),vtheta(i)],1);
end
% Plot velocity vector
quiver(x(30),y(30),vrNorm(30),vthetaNorm(30),'LineWidth',2,'MaxHeadSize',1);
% Label plot with eccentricity
title(['Elliptical Orbit with e = ' sprintf('%.2f',e)]);
Unfortunately, once plot performed, it seems that I get a bad vector for speed. Here for example the 30th element of vrNorm and vthetaNorm arrays :
As you can see, the vector has the wrong direction (If I assume to take 0 for theta from the right axis and positive variation like into trigonometrics).
If someone could see where is my error, this would nice.
UPDATE 1: Has this vector representing the speed on elliptical orbit to be tangent permanently to the elliptical curve ?
I would like to represent it by taking the right focus as origin.
UPDATE 2:
With the solution of #MadPhysicist, I have modified :
% compute velocity vectors
vr(1:N-1) = (2*pi).*diff(r)./(P.*diff(M));
vtheta(1:N-1) = (2*pi).*r(1:N-1).*diff(theta)./(P.*diff(M));
% Plot velocity vector
for l = 1:9 quiver(x(20*l),y(20*l),vr(20*l)*cos(vtheta(20*l)),vr(20*l)*sin(vtheta(20*l)),'LineWidth',2,'MaxHeadSize',1);
end
% Label plot with eccentricity
title(['Elliptical Orbit with e = ' sprintf('%.2f',e)]);
I get the following result :
On some parts of the orbit, I get wrong directions and I don't understand why ...
There are two issues with your code:
The normalization is done incorrectly. norm computes the generalized p-norm for a vector, which defaults to the Euclidean norm. It expects Cartesian inputs. Setting p to 1 means that it will just return the largest element of your vector. In your case, the normalization is meaningless. Just set vrNorm as
vrNorm = vr ./ max(vr)
It appears that you are passing in the polar coordinates vrNorm and vthetaNorm to quiver, which expects Cartesian coordinates. It's easy to make the conversion in a vectorized manner:
vxNorm = vrNorm * cos(vtheta);
vyNorm = vrNorm * sin(vtheta);
This assumes that I understand where your angle is coming from correctly and that vtheta is in radians.
Note
The entire loop
for i = 1:N-1
vr(i) = (r(i+1)-r(i))/(P*(M(i+1)-M(i))/(2*pi));
vtheta(i) = r(i)*(theta(i+1)-theta(i))/(P*(M(i+1)-M(i))/(2*pi));
vrNorm(i) = vr(i)/norm([vr(i),vtheta(i)],1);
vthetaNorm(i) = vtheta(i)/norm([vr(i),vtheta(i)],1);
end
can be rewritten in a fully vectorized manner:
vr = (2 * pi) .* diff(r) ./ (P .* diff(M))
vtheta = (2 * pi) .* r .* diff(theta) ./ (P .* diff(M))
vrNorm = vr ./ max(vr)
vxNorm = vrNorm * cos(vtheta);
vyNorm = vrNorm * sin(vtheta);
Note 2
You can call quiver in a vectorized manner, on the entire dataset, or on a subset:
quiver(x(20:199:20), y(20:199:20), vxNorm(20:199:20), vyNorm(20:199:20), ...)

helix-shaped movement of an object in Matlab

I have found this equation in a paper which represents a helix-shaped movement of an object:
When I plotted the S vector in Matlab I got a different result, not helix shape.
Where is the problem, is it in the equation or in the code?
l is a random number in [-1,1]
r is a random vector in [0,1]
b is a constant for defining the shape of the logarithmic spiral.
Matlab code:
dim =3;
Max_iter =10;
X_star=zeros(1,dim);
ub = 100;
lb = -100;
X=rand(1,dim).*(ub-lb)+lb;
S = [];
t=0;
while t<Max_iter
a=-1+t*((-1)/Max_iter);
r=rand();
b=1;
l=(a-1)*rand + 1;
for j=1:size(X,2)
D= abs(X_star(j) - X(1,j));
X(1,j)= D * exp(b.*l).* cos(l.*2*pi) + X_star(j);
end
X_star=X(1,:);
S = [S X];
plot(S);
t = t+1;
end
that equations does not look like helix at all. Lets do this for 3D. First some definitions:
p0,p1 - centers of start and end of the helix 3D points
r - scalar radius of the helix
m - scalar number of screws (if not integer than the screws will not start and end at the same angular position)
We first need TBN vectors:
n = p1-p0
t = (1,0,0)
if (|dot(n/|n|,t)|>0.9) t = (0,1,0)
b = cross(n,t)
b = r*b / |b|
t = cross(b,n)
t = r*t / |t|
And now we can do the helix using TBN as basis vectors:
// x' y' z'
p(a) = p0 + t*cos(2*Pi*m*a) + b*sin(2*Pi*m*a) + n*a
Where a = <0.0,1.0> is the parameter which point on helix you want. If you move out of this interval you will extrapolate the helix before p0 and after p1. You can consider a is the time...
As you can see the equation is just 2D parametric circle (x',y') + linear movement (z') and transformed by basis vectors to p0,p1 direction and position.
If you need equations/code for the dot,cross and abs value of 3D vectors see:
Understanding 4x4 homogenous transform matrices
They are at the bottom of that answer of mine.

How to interpolate using in polar coordinate

I have polar coordinates, radius 0.05 <= r <= 1 and 0 ≤ θ ≤ 2π. The radius r is 50 values between 0.05 to 1, and polar angle θ is 24 values between 0 to 2π.
How do I interpolate r = 0.075 and theta = pi/8?
I dunno what you have tried, but interp2 works just as well on polar data as it does on Cartesian. Here is some evidence:
% Coordinates
r = linspace(0.05, 1, 50);
t = linspace(0, 2*pi, 24);
% Some synthetic data
z = sort(rand(50, 24));
% Values of interest
ri = 0.075;
ti = pi/8;
% Manually interpolate
rp = find(ri <= r, 1, 'first');
rm = find(ri >= r, 1, 'last');
tp = find(ti <= t, 1, 'first');
tm = find(ti >= t, 1, 'last');
drdt = (r(rp) - r(rm)) * (t(tp) - t(tm));
dr = [r(rp)-ri ri-r(rm)];
dt = [t(tp)-ti ti-t(tm)];
fZ = [z(rm, tm) z(rm, tp)
z(rp, tm) z(rp, tp)];
ZI_manual = (dr * fZ * dt.') / drdt
% Interpolate with MATLAB
ZI_MATLAB = interp2(r, t, z', ri, ti, 'linear')
Result:
ZI_manual =
2.737907208525297e-002
ZI_MATLAB =
2.737907208525298e-002
Based on comments you have the following information
%the test point
ri=0.53224;
ti = pi/8;
%formula fo generation of Z
g=9.81
z0=#(r)0.01*(g^2)*((2*pi)^-4)*(r.^-5).*exp(-1.25*(r/0.3).^-4);
D=#(t)(2/pi)*cos(t).^2;
z2=#(r,t)z0(r).*D(t) ;
%range of vlaues of r and theta
r=[0.05,0.071175,0.10132,0.14422,0.2053, 0.29225,0.41602,0.5922,0.84299,1.2];
t=[0,0.62832,1.2566,1.885, 2.5133,3.1416,3.7699,4.3982,5.0265,5.6549,6.2832];
and you want interplation of the test point.
When you sample some data to use them for interpolation you should consider how to sample data according to your requirements.
So when you are sampling a regular grid of polar coordinates ,those coordinates when converted to rectangular will form a circular shape that
most of the points are concentrated in the center of the cricle and when we move from the center to outer regions distance between the points increased.
%regular grid generated for r and t
[THETA R] = meshgrid(t ,r);
% Z for polar grid
Z=z2(R,THETA);
%convert coordinate from polar to cartesian(rectangular):
[X, Y] = pol2cart (THETA, R);
%plot points
plot(X, Y, 'k.');
axis equal
So when you use those point for interpolation the accuracy of the interpolation is greater in the center and lower in the outer regions where the distance between points increased.
In the other word with this sampling method you place more importance on the center region related to outer ones.
To increase accuracy density of grid points (r and theta) should be increased so if length of r and theta is 11 you can create r and theta with size 20 to increase accuracy.
In the other hand if you create a regular grid in rectangular coordinates an equal importance is given to each region . So accuracy of the interpolation will be the same in all regions.
For it first you create a regular grid in the polar coordinates then convert the grid to rectangular coordinates so you can calculate the extents (min max) of the sampling points in the rectangular coordinates. Based on this extents you can create a regular grid in the rectangular coordinates
Regular grid of rectangular coordinates then converted to polar coordinated to get z for grid points using z2 formula.
%get the extent of points
extentX = [min(X(:)) max(X(:))];
extentY = [min(Y(:)) max(Y(:))];
%sample 100 points(or more or less) inside a region specified be the extents
X_samples = linspace(extentX(1),extentX(2),100);
Y_samples = linspace(extentY(1),extentY(2),100);
%create regular grid in rectangular coordinates
[XX YY] = meshgrid(X_samples, Y_samples);
[TT RR] = cart2pol(XX,YY);
Z_rect = z2(RR,TT);
For interpolation of a test point say [ri ti] first it converted to rectangular then using XX ,YY value of z is interpolated
[xi yi] = pol2cart (ti, ri);
z=interp2(XX,YY,Z_rect,xi,yi);
If you have no choice to change how you sample the data and only have a grid of polar points as discussed with #RodyOldenhuis you can do the following:
Interpolate polar coordinates with interp2 (interpolation for gridded data)
this approach is straightforward but has the shortcoming that r and theta are not of the same scale and this may affect the accuracy of the interpolation.
z = interp2(THETA, R, Z, ti, ri)
convert polar coordinates to rectangular and then apply an interpolation method that is for scattered data.
this approach requires more computations but result of it is more reliable.
MATLAB has griddata function that given scattered points first generates a triangulation of points and then creates a regular grid on top of the triangles and interpolates values of grid points.
So if you want to interpolate value of point [ri ti] you should then apply a second interpolation to get value of the point from the interpolated grid.
With the help of some information from spatialanalysisonline and Wikipedia linear interpolation based on triangulation calculated this way (tested in Octave. In newer versions of MATLAB use of triangulation and pointLocation recommended instead of delaunay and tsearch ):
ri=0.53224;
ti = pi/8;
[THETA R] = meshgrid(t ,r);
[X, Y] = pol2cart (THETA, R);
[xi yi] = pol2cart (ti, ri);
%generate triangulation
tri = delaunay (X, Y);
%find the triangle that contains the test point
idx = tsearch (X, Y, tri, xi, yi);
pts= tri(idx,:);
%create a matrix that repesents equation of a plane (triangle) given its 3 points
m=[X(pts);Y(pts);Z(pts);ones(1,3)].';
%calculate z based on det(m)=0;
z= (-xi*det(m(:,2:end)) + yi*det([m(:,1) m(:,3:end)]) + det(m(:,1:end-1)))/det([m(:,1:2) m(:,end)]);
More refinement:
Since it is known that the search point is surrounded by 4 points we can use only those point for triangulation. these points form a trapezoid. Each diagonal of trapezoid forms two triangles so using vertices of the trapezoid we can form 4 triangles, also a point inside a trapezoid can lie in at least 2 triangles.
the previous method based on triangulation only uses information from one triangle but here z of the test point can be interpolated two times from data of two triangles and the calculated z values can be averaged to get a better approximation.
%find 4 points surrounding the test point
ft= find(t<=ti,1,'last');
fr= find(cos(abs(diff(t(ft+(0:1))))/2) .* r < ri,1,'last');
[T4 R4] = meshgrid(t(ft+(0:1)), r(fr+(0:1)));
[X4, Y4] = pol2cart (T4, R4);
Z4 = Z(fr+(0:1),ft+(0:1));
%form 4 triangles
tri2= nchoosek(1:4,3);
%empty vector of z values that will be interpolated from 4 triangles
zv = NaN(4,1);
for h = 1:4
pts = tri2(h,:);
% test if the point lies in the triangle
if ~isnan(tsearch(X4(:),Y4(:),pts,xi,yi))
m=[X4(pts) ;Y4(pts) ;Z4(pts); [1 1 1]].';
zv(h)= (-xi*det(m(:,2:end)) + yi*det([m(:,1) m(:,3:end)]) + det(m(:,1:end-1)))/det([m(:,1:2) m(:,end)]);
end
end
z= mean(zv(~isnan(zv)))
Result:
True z:
(0.0069246)
Linear Interpolation of (Gridded) Polar Coordinates :
(0.0085741)
Linear Interpolation with Triangulation of Rectangular Coordinates:
(0.0073774 or 0.0060992) based on triangulation
Linear Interpolation with Triangulation of Rectangular Coordinates(average):
(0.0067383)
Conclusion:
Result of interpolation related to structure of original data and the sampling method. If the sampling method matches pattern of original data result of interpolation is more accurate, so in cases that grid points of polar coordinates follow pattern of data result of interpolation of regular polar coordinate can be more reliable. But if regular polar coordinates do not match the structure of data or structure of data is such as an irregular terrain, method of interpolation based on triangulation can better represent the data.
please check this example, i used two for loops, inside for loop i used condition statement, if u comment this condition statement and run the program, u'll get correct answer, after u uncomment this condition statement and run the program, u'll get wrong answer. please check it.
% Coordinates
r = linspace(0.05, 1, 10);
t = linspace(0, 2*pi, 8);
% Some synthetic data
%z = sort(rand(50, 24));
z=zeros();
for i=1:10
for j=1:8
if r(i)<0.5||r(i)>1
z(i,j)=0;
else
z(i,j) = r(i).^3'*cos(t(j)/2);
end
end
end
% Values of interest
ri = 0.55;
ti = pi/8;
% Manually interpolate
rp = find(ri <= r, 1, 'first');
rm = find(ri >= r, 1, 'last');
tp = find(ti <= t, 1, 'first');
tm = find(ti >= t, 1, 'last');
drdt = (r(rp) - r(rm)) * (t(tp) - t(tm));
dr = [r(rp)-ri ri-r(rm)];
dt = [t(tp)-ti ti-t(tm)];
fZ = [z(rm, tm) z(rm, tp)
z(rp, tm) z(rp, tp)];
ZI_manual = (dr * fZ * dt.') / drdt
% Interpolate with MATLAB
ZI_MATLAB = interp2(r, t, z', ri, ti, 'linear')
Result:
z1 =
0.1632
ZI_manual =
0.1543
ZI_MATLAB =
0.1582

Matlab Finding Damped Sine wave decay factor for a given frequency

how to find decay constant value of a damped sine wave for a given frequency in matlab?
t=0:1e-6:0.1;
f= 500000;
y=sin(2*pi*f*t).*exp(-d*t);
i want to solve above equation for "d"
You first need to find the envelope of your oscillating function (the function that amplitude-modulates your sine). This can be done e.g. by rectifying the signal and then low-pass-filtering, but I choose to do a quick and dirty running maximum. After you found the envelope, there are various ways to fit an exponential function. I again chose a quick&dirty trick to do a first order polyfit to the log of the envelope. The code below works for the simple example you gave, but is might not work if you have an offset, if you choose n wrong, etc. It also won't give the best possible result in case of a noisy measurement.
fsamp = 1e5;
tmax = 0.1;
t=0:1/fsamp:tmax;
f = 12e3; %should be smaller than fsamp/2!
tau = 0.0765;
y=sin(2 * pi * f * t) .* exp(-t / tau);
%calculate running maximum
n = 20; %number of points to take max over
nblocks = floor(length(t) / n);
trun = mean(reshape(t(1:n*nblocks), n, nblocks), 1); %n-point mean
envelope = max(reshape(y(1:n*nblocks), n, nblocks), [], 1); %n-point max
%quick and dirty exponential fit, not the proper way in case of noise
p = polyfit(trun, log(envelope), 1);
tau_fit = -1/p(1);
k_fit = exp(p(2));
plot(t, y, trun, envelope, 'or', t, k_fit * exp(-t / tau_fit), '-k')
title(sprintf('tau = %g', tau))
Note that with exponential decay, it is more common to define the time-constant tau = 1 / d.