Filter löschen
Filter löschen

How to interpolate the given plot?

5 Ansichten (letzte 30 Tage)
JD_PM
JD_PM am 28 Apr. 2022
Kommentiert: Star Strider am 28 Apr. 2022
I plot speed vs. width (the first three columns are the velocity components and the last the width, please see first file here https://drive.google.com/drive/folders/1CgeDuCahVZvLXXkNkGWlPg8lSGyurmI3) using the following code
clc
close all
clear all
warning('OFF', 'MATLAB:table:ModifiedAndSavedVarnames')
%step 0
Qi = readmatrix("004timestepN2Void30mm.csv");
[v,w] = size(Qi);
%step 1
fCom_004 = Qi(:,1);
sCom_004 = Qi(:,2);
tCom_004 = Qi(:,3);
width_004 = [0;Qi(:,w)+0.0075];
U_004 = sqrt(fCom_004.^2 + sCom_004.^2 + tCom_004.^2);
refined_U_004 = [0;U_004];
%step 3
figure
plot(width_004,refined_U_004)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
You'll see that the peak is far from being smooth. To fix the issue I am trying to use interpolation. To do so I modified step 3
newX = linspace (0, 0.015,3e3);
newY = interp1(width_0002,refined_U_0002,newX);
figure
plot(newX,newY)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
But the following error pops up "Sample points must be unique."
I think I know why this is happening.
By default, Matlab seems to only read the first four decimals of a given number. Notice that in the last column many numbers only differ from the fifth number; that's why Matlab sees them as equal.
Assuming that I am right, how to make Matlab read the full given numbers of a file?
Thanks in advance! :)

Akzeptierte Antwort

Star Strider
Star Strider am 28 Apr. 2022
MATLAB reads what it is given.
%step 0
Qi = readmatrix("JD_PM sampleData30mm.csv");
[v,w] = size(Qi);
%step 1
fCom_004 = Qi(:,1);
sCom_004 = Qi(:,2);
tCom_004 = Qi(:,3);
width_004 = [0;Qi(:,w)+0.0075];
U_004 = sqrt(fCom_004.^2 + sCom_004.^2 + tCom_004.^2);
refined_U_004 = [0;U_004];
%step 3
figure
plot(width_004,refined_U_004)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
% % You'll see that the peak is far from being smooth. To fix the issue I am trying to use interpolation. To do so I modified step 3
newX = linspace (0, 0.015,3e3);
[width_004,idx] = unique(width_004, 'stable'); % Eliminate Duplicates
newY = interp1(width_004,refined_U_004(idx),newX);
figure
plot(newX,newY)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
I tried different interpolation methods ('pchip', 'spline') and do not see a difference.
.
  2 Kommentare
JD_PM
JD_PM am 28 Apr. 2022
Thank you Star Strider. As interpolation seems to not help to get a smoother peak, do you know of an alternative method to do so?
Star Strider
Star Strider am 28 Apr. 2022
My pleasure!
One option is to develop a nonlinear model for that and fit the data to it.
Another option is to use the Signal Processing Toolbox sgolayfilt funciton —
%step 0
Qi = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/982200/JD_PM%20sampleData30mm.csv');
[v,w] = size(Qi);
%step 1
fCom_004 = Qi(:,1);
sCom_004 = Qi(:,2);
tCom_004 = Qi(:,3);
width_004 = [0;Qi(:,w)+0.0075];
U_004 = sqrt(fCom_004.^2 + sCom_004.^2 + tCom_004.^2);
refined_U_004 = [0;U_004];
%step 3
figure
plot(width_004,refined_U_004)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
% % You'll see that the peak is far from being smooth. To fix the issue I am trying to use interpolation. To do so I modified step 3
newX = linspace (0, 0.015,3e3);
[width_004,idx] = unique(width_004, 'stable'); % Eliminate Duplicates
newY = sgolayfilt(refined_U_004(idx), 3, 81); % Use 'sgolayfilt'
figure
plot(width_004,newY)
legend('0.004 timestep','Location','bestoutside')
xlabel('width [m] | 30mm profile')
ylabel('U magnitude [m/s]')
axis square;
grid on;
Change the ‘framelen’ value to get the desired result.
The smoothdata function has an ‘sgolay’ option, however I do not know if that requires the Signal Processing Toolbox as well.
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Visual Exploration finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by