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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1446902/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1446907/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1478876/image.png)
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
. ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1447082/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1447082/image.jpeg)
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!