Find the velocity of a travelling wave like behaviour
조회 수: 7 (최근 30일)
이전 댓글 표시
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!
댓글 수: 2
Image Analyst
2023년 7월 31일
편집: Image Analyst
2023년 7월 31일
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
Where is it? You forgot to attach it!
What is velocity? What variable? What are the units of the x axis and y axis?
Make it easy for us to help you, not hard.
채택된 답변
Bruno Luong
2023년 8월 1일
%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
Bruno Luong
2023년 9월 11일
편집: Bruno Luong
2023년 9월 11일
Not sure why you need to filter out the oscillation, if the physical model and simulation is correct, then the oscillation is the physics reality.
If not then better figure out why the oscillation artefact occurs.
But here I do 2 fits with polynomial and spline. Spline has better behavior according to my eyes. I also fits on peaks+valleys only, since your data look skewed (the peaks is sharper) so that the fit goes somewhat close to the middle of alternating peak and valeys.
load('matlab.mat')
n = length(vest);
x = 1:n;
for fitfunction = {'poly' 'spline'}
b = islocalmin(vest) | islocalmax(vest);
switch fitfunction{1}
case 'poly'
[P, ~, mu] = polyfit(x(b), vest(b), 4);
vestsmooth_p = polyval(P,(x-mu(1))/mu(2));
case 'spline'
% Need file here https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
pp = BSFK(x(b), vest(b));
vestsmooth_s = ppval(pp,x);
end
end
figure
h1=plot(x,vest);
hold on
h2=plot(x,vestsmooth_p,'k','LineWidth',1);
h3=plot(x,vestsmooth_s,'r','LineWidth',1);
legend('data','polynomial','spline')
Bruno Luong
2023년 9월 11일
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')
추가 답변 (1개)
KSSV
2023년 8월 1일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!