MATLAB Answers

Interpolation Between Two known data points

44 views (last 30 days)
Cordelia David
Cordelia David on 13 May 2020
Commented: dpb on 13 May 2020
Hi everyone, I'm trying to do a complicated interpolation. I have two matrices and I'm trying to find the data points between these two.
One of my matrices is like this:
[80000 80000 80000 80000 80000 80000 ....
1.273 1.346 1.419 1.4926 1.5658 1.63897 ....]
and the second is like this:
[69000 69000 69000 69000 69000 69000 ....
5.022 5.367 5.712 6.058 6.4038 6.749 .....]
I'm trying to interpolate what the values of the second column would be given this data if the first column of data was all 75000, similar in structure to the other two. I want it to look like this:
[75000 75000 75000 75000 75000 75000...
___ ___ ___ ___ ____ ___ .....]
However anytime I try to use the interp1 function I get the error "Sample points must be unique and sorted in ascending order." because the first columns are not unique. I'm not sure how to go about this. Any suggestions are appreciated!

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 13 May 2020
You're not looking across the variations in your two variables--another illustration why it's better to not create multiple variables for related data but to put into an array or other data structure that can reference programmatically -- see what happens if you were to use
X=[A(1,:);B(1,:)];
Y=[A(2,:);B(2,:)];
XO=75000;
YO=Y(1,:)+(XO-X(1,:))./diff(X).*diff(Y);
yields
>> YO
YO =
2.98 3.17 3.37 3.57 3.76 3.96
>>
for a single interpolant there's not much need for interp1
OTOH, you could use interp1 either in an anonymous function using the desired X0 and column(s) over which to interpolate as it isn't vectorized to accept more than a single vector X,Y input.

  4 Comments

Show 1 older comment
Cordelia David
Cordelia David on 13 May 2020
Hey dqb I am actually getting an error when I apply to all of my data. The error is "Matrix dimensions must agree". Both matrices have the same number of points, so I'm not sure what the problem is.
dpb
dpb on 13 May 2020
Would have to see what the code and data look like...if it's as you described above, it will work...if you get the error about the dimensions aren't the same, then they aren't even if you think they are or should be...or you may have an orientation problem w/ trying to catenate in the wrong direction even if same size.
Use whos on the variable list you're trying to catenate to find out who's out of line.
dpb
dpb on 13 May 2020
Nothing says you can't use interp1, it's just not much of a savings if table has only two elements. But, the route would look something like:
fnInterp=@(i,xo)interp1(X(:,i),Y(:,i),xo); % define the anonymous function with table
% Example to use...
>> fnInterp(1,XO)
ans =
2.98
% To do all columns...
>> arrayfun(fnInterp,1:size(X,2),repelem(XO,size(X,2)))
ans =
2.98 3.17 3.37 3.57 3.76 3.96
% Compare to the explicit results from above...
>> YO=Y(1,:)+(XO-X(1,:))./diff(X).*diff(Y)
YO =
2.98 3.17 3.37 3.57 3.76 3.96
>>

Sign in to comment.

More Answers (0)

Tags


Translated by