Interpolation Between Two known data points

조회 수: 30 (최근 30일)
Cordelia David
Cordelia David 2020년 5월 13일
댓글: dpb 2020년 5월 13일
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!

채택된 답변

dpb
dpb 2020년 5월 13일
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
dpb
dpb 2020년 5월 13일
편집: dpb 2020년 5월 13일
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 2020년 5월 13일
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
>>

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by