Clear Filters
Clear Filters

Linear Interpolation listed data points on to anothe list

4 views (last 30 days)
I have range of measurments as shown
x_data = (0.7127, 0.6954, 0.6789, 0.6744, 0.6697, 0.6649, 0.6600, 0.6551, 0.6501, 0.6450,
0.7127, 0.7091, 0.6988, 0.6827, 0.6626, 0.6574, 0.6520, 0.6466, 0.6411, 0.6356,
0.6300, 0.6245, 0.6190)
I want to interpolate (linear) the above x data on to new unformly spaced z value given by:
z_new = 0:10/6371:0.244
Given that the x list has differnet length than the new data (z) how do we do this interpolation?
I tried this from mathworks online but not working:
"vq = interp1(x,v,xq) returns interpolated values of a 1-D function at specific query points using linear interpolation. Vector x contains the sample points, and v contains the corresponding values, v(x). Vector xq contains the coordinates of the query points"
Thank you

Accepted Answer

Image Analyst
Image Analyst on 3 Dec 2023
You're mixing up x_data, x, z_new, and v. I think the nomenclature is confusing you. Try this:
x_data = [0.7127, 0.6954, 0.6789, 0.6744, 0.6697, 0.6649, 0.6600, 0.6551, 0.6501, 0.6450,...
0.7127, 0.7091, 0.6988, 0.6827, 0.6626, 0.6574, 0.6520, 0.6466, 0.6411, 0.6356,...
0.6300, 0.6245, 0.6190];
x = 1 : numel(x_data);
v = x_data;
subplot(2, 1, 1);
plot(x, v, 'b.--', 'LineWidth', 2, 'MarkerSize', 20)
grid on;
xlabel('Index', 'Interpreter','none');
ylabel('x_data', 'Interpreter','none')
% This z_new is no good since the interpolation x values are
% not in the range of x_data indexes.
z_new = 0:10/6371:0.244; % BAD!
size(z_new)
ans = 1×2
1 156
% Let's use a z_new that goes from the first index
% to the first index + 0.244, in other words in the range [1, 1.244]
% And since the original z_new had 156 points, let's use 156 points
% with the new z_new.
z_new = linspace(1, 1.244, 156);
vq = interp1(x,v,z_new);
subplot(2, 1, 2);
x = 1 : numel(vq);
plot(x, vq, 'r-', 'LineWidth', 2)
grid on;
title('vq', 'Interpreter','none');
xlabel('Index', 'Interpreter','none');
ylabel('Interpolated x_data', 'Interpreter','none')
If that is not the interpolation region you want, feel free to set the z_new range anywhere from 1 to 23 (which is the number of elements in x_data).
  4 Comments
root
root on 3 Dec 2023
Thanks for your clarification .
The data to be interpolated is the given x_data. I want to interpolate this on znew where znew has a value if 0:max(x_data) with stepsize of 10/6371.
Image Analyst
Image Analyst on 3 Dec 2023
That does not make any sense. Please show on a numberline or a graph the actual x_data points, and the values for which you want interpolated as your output. Keep in mind that any points specified outside the range of your input data will be NaN.

Sign in to comment.

More Answers (0)

Categories

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