How make an linear interpolation using intervals?
조회 수: 13 (최근 30일)
이전 댓글 표시
Dear all. I am writing to request your help, I am try to interpolate a big vector with values at specifict time;
example (data from excel file)
date Value Interval
25-Mar-16 0.065
14-Apr-16 0.34 20
24-Apr-16 0.477 10
4-May-16 0.604 10
3-Jun-16 0.788 31
13-Jun-16 0.959 9
the idea is fill the values between 25-mar-16 to 14Apr-16 ...... until 13-Jun-16 in this example. but I hade create a script to make one by one.
but I need make this process from 2016 to 2021 so to interpolate one by one is hard.
clear; close all;
D = 20;% empty values betwen first and end value
f1 = 0.065;% first value
f2 = 0.34;% end value
for i=1:D-1
A=(i)/D;
B = 1-A;
vi(i) = (f1*A)+(f2*B);
end
vi = flip(vi);
vi = vi';
Please can help me how I can create a script to automatize this process?
I will appreciate your valuable support
답변 (1개)
Nipun
2024년 5월 17일
Hi Orlando,
I understand that you are trying to interpolate values between given data points over specific intervals for a range of dates from 2016 to 2021. Your goal is to automate this process rather than doing it manually for each interval. To achieve this, you can use MATLAB's interpolation functions along with a loop to process each interval in your dataset.
First, you'll need to ensure your dates are in a format that MATLAB can work with, and then you can use linear interpolation to fill in the values between your specified dates. Here's a step-by-step guide to automating this process:
- Convert Dates to MATLAB Datetime: Convert your date strings to datetime objects for easy manipulation.
- Create a Continuous Date Range: Generate a continuous range of dates from your start to end date.
- Interpolate Values: Use MATLAB's interpolation functions to fill in the missing values.
Here's an example script based on your data:
% Sample data (assuming it's loaded from an Excel file)
dates = ["25-Mar-16", "14-Apr-16", "24-Apr-16", "4-May-16", "3-Jun-16", "13-Jun-16"];
values = [0.065, 0.34, 0.477, 0.604, 0.788, 0.959];
% Convert date strings to datetime format
dates = datetime(dates, 'InputFormat', 'dd-MMM-yy');
% Create a continuous date range from start to end
startDate = dates(1);
endDate = dates(end);
allDates = startDate:endDate; % Daily intervals, adjust if necessary
% Convert both the original dates and the full date range to serial date numbers for interpolation
originalDatesNum = datenum(dates);
allDatesNum = datenum(allDates);
% Interpolate values for all dates
interpolatedValues = interp1(originalDatesNum, values, allDatesNum, 'linear');
% Plot the results
figure;
plot(dates, values, 'o', allDates, interpolatedValues, '-');
xlabel('Date');
ylabel('Value');
legend('Original Data', 'Interpolated Data', 'Location', 'best');
title('Data Interpolation');
For more information on the "datetine" function in MATLAB, refer to the following MathWorks documentation: https://in.mathworks.com/help/matlab/ref/datetime.html#d126e341937
Hope this helps.
Regards,
Nipun
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!