Clear Filters
Clear Filters

How to interpolate the given plot?

5 views (last 30 days)
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! :)

Accepted Answer

Star Strider
Star Strider on 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 Comments
JD_PM
JD_PM on 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 on 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.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Visual Exploration 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!