Find the velocity of a travelling wave like behaviour
5 views (last 30 days)
Show older comments
Hi everyone, I have a travelling wave solution drawn at each time step for a set of data obtained from a previous simulation.
Tmax = 10000;
m =load('solution.mat');
SS = m.sol;
N = m.N1;
for i = 1:50:Tmax
Xval = SS(i,N);
hold on
plot(N,Xval,'Linewidth',2);
end
hold off
and below is the out put I got from the above code where x axis represents a distance (N) from 0 to 200 and y axis takes values between 0-1. .Each coloured line is drawn at a different time step.

Next, I'd like to calculate the velocity at each time step when y=0.5. I.e. I want to identify the x value when y=0.5 for each colour line and calculate the velocity as
. The main question here is sometimes there are no exact points in Xval with value 0.5 or no exact N value associated to 0.5.

I'd be happy if someone could help me in creating a loop for this calculation.
Thanks in advance!
Accepted Answer
Bruno Luong
on 1 Aug 2023
%s =load('solution.mat');
%SS = s.sol;
% Create fake N and SS
N = -20:0.1:20;
v = arrayfun(@(x) x/(sqrt(1+x^2)), (1:40)/10)
x0 = -17 + cumsum(v);
[X,X0] = meshgrid(N,x0);
SS = 1-(tanh(X-X0)+1)/2;
% Determine cross point
yx = 0.5;
ys = SS'-yx;
[m,n] = size(ys);
[i,j] = find(ys(1:end-1,:).*ys(2:end,:) <= 0);
% linear interpolation
i1 = i + (j-1)*m; ss1 = ys(i1);
i2 = i1 + 1; ss2 = ys(i2);
w = ss2./(ss2-ss1);
x = N(:);
ix = nan(1,n);
ix(j) = w.*x(i) + (1-w).*x(i+1);
vest = gradient(ix)
figure
plot(N, SS.');
hold on
plot(ix, yx+zeros(size(ix)), '+r', 'Markersize', 10)
figure
plot(vest)
hold on
plot(v)
legend('v estimate', 'v', 'location', 'best')
7 Comments
Bruno Luong
on 11 Sep 2023
For completness this is matlab spline fitting. It oscillates more than I prefer.
load('matlab.mat')
n = length(vest);
x = 1:n;
for fitfunction = {'poly' 'spline'}
b = islocalmin(vest) | islocalmax(vest);
xb = reshape(x(b),[],1);
y = reshape(vest(b),[],1);
switch fitfunction{1}
case 'poly'
[P, ~, mu] = polyfit(xb,y, 6);
vestsmooth_p = polyval(P,(x-mu(1))/mu(2));
case 'BSFK'
% Need file here https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
pp = BSFK(xb, y);
vestsmooth_b = ppval(pp,x);
case 'spline'
N = 12;
[xmin, xmax] = bounds(xb);
xi = linspace(xmin, xmax, N);
B = zeros(numel(xb),N);
for k=1:N
yi = accumarray(k,1,[N,1]);
B(:,k) = spline(xi, yi, x(b));
end
c = B \ y;
vestsmooth_s = spline(xi, c, x);
end
end
figure
h1=plot(x,vest,'g');
hold on
h2=plot(x,vestsmooth_p,'k','LineWidth',1);
%h3=plot(x,vestsmooth_b,'r','LineWidth',1);
h4=plot(x,vestsmooth_s,'r','LineWidth',1);
legend('data','polynomial','spline')
More Answers (1)
KSSV
on 1 Aug 2023
N = 1000 ;
x = linspace(0,200,N) ;
y = rand(size(x)) ;
L1 = [x;y] ;
L2 = [x; repelem(0.5,1,N)] ;
P = InterX(L1,L2) ;
You got the points you want in P. You can do what you want
. 

0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!