1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| clc
clear all
close all
%% Generate 1D Brownian motion
% Set the parameter H and the sample length
H = 0.3; lg = 100;
% Generate and plot wavelet-based d for H = 0.3
d = wfbm(H,lg);
t=1:1:lg;
%% Calculate the MSD
GN=size(t);
GN=GN(2);
for n=1:GN
temp = 0;
for i=1:GN-n
temp = temp+(d(i+n)-d(i))^2;
end
msd(n) = 1/(GN-n)*temp;
end
% Calculate the error bar
deltad=1; % experimental error bar on d
for n=1:GN
timp=0;
for i=1:GN-n
timp = timp + 4*(d(i+n)-d(i));
end
DMSD(n)=timp*deltad/(GN-n);
end
%% Plot and fit
subplot(2,1,1)
plot(t,d)
title('Brownien motion')
xlabel('time')
ylabel('distance')
% FIT on the first 10% of the msd
debut=GN/10;
x=t(2:debut);
y=msd(2:debut);
f=fittype('c*x');
fopt=(fitoptions(f));
fopt.startpoint=[1];
myfit=fit(x',y',f,fopt)
myfit=myfit(1:GN/2);
subplot(2,1,2)
errorbar(t,msd,DMSD)
hold on
plot(myfit,'r')
title('MSD');
xlabel('time')
ylabel('MSD')
hold off |
Partager