linear interpolation for the curves

2 views (last 30 days)
ayushman srivastava
ayushman srivastava on 31 Jul 2023
Edited: Chhayank Srivastava on 31 Jul 2023
I have 2 curves of pressure. Pressure represents y-axis and x-axis is the time. There is some difference between the 2 curves. First i have to interpolate to find the pressure for the missing time and secondly I have to find the relative error between the two existing curves using RMS.
Please help me in writing the codes for finding the pressure for the missing time considered as a target time using linear interpolation and for finding the relative error using RMS.
I have attached the excel sheet for better understanding

Answers (2)

Star Strider
Star Strider on 31 Jul 2023
There are missing data, however they are all at the end of the first -time-pressure values. Filling them would require extrapolation, and that is to be strictly avoided here because that would require creating data where not previously existed, rather than interpolating intermediate missing values.
One approach —
% C1 = readcell('DENSITY_BASED_SOLVER.xlsx')
T1 = readtable('DENSITY_BASED_SOLVER.xlsx', 'VariableNamingRule','preserve', 'HeaderLines',1)
T1 = 5000×8 table
Flow time (milli-seconds) Pressure (MPa) Var3 Var4 flow-time (Seconds) Flow time(ms) Pressure (Pascals) Pressure (Mpa) _________________________ ______________ _______ ____ ___________________ _____________ __________________ ______________ 0.0038394 0.10773 NaN NaN 1e-07 0.0001 1.0133e+05 0.10133 0.011518 0.10175 NaN NaN 2e-07 0.0002 1.0133e+05 0.10133 0.017277 0.10474 NaN NaN 3e-07 0.0003 1.0133e+05 0.10133 0.024956 0.10773 0.64816 NaN 4e-07 0.0004 1.0133e+05 0.10133 0.030716 0.10175 NaN NaN 5e-07 0.0005 1.0133e+05 0.10133 0.034555 0.10175 NaN NaN 6e-07 0.0006 1.0133e+05 0.10133 0.042234 0.10773 NaN NaN 7e-07 0.0007 1.0133e+05 0.10133 0.046073 0.11372 NaN NaN 8e-07 0.0008 1.0133e+05 0.10133 0.047993 0.13466 NaN NaN 9e-07 0.0009 1.0133e+05 0.10133 0.047993 0.13466 NaN NaN 1e-06 0.001 1.0133e+05 0.10133 0.049913 0.14963 NaN NaN 1.1e-06 0.0011 1.0133e+05 0.10133 0.049913 0.14963 NaN NaN 1.2e-06 0.0012 1.0133e+05 0.10133 0.053752 0.14065 NaN NaN 1.3e-06 0.0013 1.0133e+05 0.10133 0.053752 0.14065 NaN NaN 1.4e-06 0.0014 1.0133e+05 0.10133 0.053752 0.17057 NaN NaN 1.5e-06 0.0015 1.0133e+05 0.10133 0.054712 0.18554 NaN NaN 1.6e-06 0.0016 1.0133e+05 0.10133
VN = T1.Properties.VariableNames;
t{1} = T1{:,1};
p{1} = T1{:,2};
t{2} = T1{:,6};
p{2} = T1{:,8};
% Q1 = nnz(isnan([p{1}]))
% Q11 = nnz(isnan(t{1}))
% Q12 = find(isnan(p{1}))
% t{1}(Q12-1)
% find(diff(Q12)>1)
% Q2 = nnz(isnan([t{2},p{2}]))
figure
plot(t{1}, p{1}, 'DisplayName','First Set')
hold on
xline(t{1}(484), '--r', 'DisplayName','Finite Time Limit')
plot(t{2}, p{2}, 'DisplayName','Second Set')
hold off
grid
xlabel('Time (ms)')
ylabel('Pressure (MPa)')
title('Original')
legend('Location','best')
[min_time_ends,idx] = min([t{1}(end) t{2}(end)]) % Shortest Time Vector & Set Number ('idx')
min_time_ends = 0.5000
idx = 2
tq{1} = t{1}(~isnan(t{1})); % Eliminiate the 'NaN' Values So The Interpolation Will Work
[tq{1},idx] = unique(tq{1}); % Eliminate Duplicate Time Values, Return Unique Time Values & Associated Index
pq{1} = p{1}(idx); % Pressure Values At Unique Time Values
pq{1} = interp1(tq{1}, pq{1}, t{2}); % Interpolate Longer Pressure (p{2}) To Shorter Time Vector (t{1})
pdif = p{2} - pq{1}; % Pressure Difference
figure
plot(t{2}, pq{1}, 'DisplayName','First Set (Interpolated)')
hold on
plot(t{2}, p{2}, 'DisplayName','Second Set')
plot(t{2}, pdif, ':k', 'DisplayName','Pressure Difference')
hold off
grid
xlabel('Time (ms)')
ylabel('Pressure (MPa)')
title('Processed')
legend('Location','best')
Processing these data are not straightforward.
Now, use the rms function to find the RMS value of the difference between the two curves.
.

Chhayank Srivastava
Chhayank Srivastava on 31 Jul 2023
Edited: Chhayank Srivastava on 31 Jul 2023
I included the data with some cleaning to be imported by MATLAB and named the sheet "Data".
Few assumptions were made, as I didnt which pressure input you wanted to consider MPa or Pascals but nonetheless the code's logic will be similar.
clear; clc;
data = readtable('DENSITY_BASED_SOLVER.xlsx',"Sheet","Data");
%Pressure data 1 (pascals)
p_1(:,1) = data{:,1};
p_1(:,2) = data{:,2};
%Pressure data 2 (pascals)
p_2(:,1) = data{:,9};
p_2(:,2) = data{:,10};
%missing time ... Random points in time for 1D interpolation
time = rand(1,15);
%Interpolation .. I included all the data points but you can reduce data points closer to the relevant area for interpolation
% This can be done by changing interp1(p_1(:,1),p_1(:,2),time) to interp1(p_1(st_intv:end_intv,1),p_1(st_intv:end_intv,2),time)
val_1 = interp1(p_1(:,1),p_1(:,2),time);
val_2 = interp1(p_2(:,1),p_2(:,2),time);
%RMSE
error = rmse(val_1,val_2)

Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!