Interpolation of data that depends on two variables

41 次查看(过去 30 天)
Hi guys,
I have the following set of data:
sigma=[0;0;0;0;0;1;1;1;1;1;2;2;2;2;2];
alpha=[0;1;2;3;4;0;1;2;3;4;0;1;2;3;4];
C=[10;11;12;13;14;15;16;17;18;19;20;21;22;23;24];
Now I want to know wat the value of C is if my calculate alpha is 2.5 and sigma is 1.5.
To solve this problem, I have tried to use the function interp2. But I get an error.
Can somebody help me with this?
Thanks in advance, Bas Siebers

采纳的回答

Star Strider
Star Strider 2015-5-21
Use the scatteredInterpolant function:
sigma=[0;0;0;0;0;1;1;1;1;1;2;2;2;2;2];
alpha=[0;1;2;3;4;0;1;2;3;4;0;1;2;3;4];
C=[10;11;12;13;14;15;16;17;18;19;20;21;22;23;24];
F = scatteredInterpolant(sigma, alpha, C);
Cnew = F(1.5, 2.5);
figure(1)
stem3(sigma, alpha, C)
hold on
stem3(2.5, 1.5, Cnew, 'r')
hold off
grid on
xlabel('\sigma')
ylabel('\alpha')
It plots ‘Cnew’, your interpolated value for ‘C’, in red.

更多回答(2 个)

John D'Errico
John D'Errico 2015-5-21
编辑:John D'Errico 2015-5-21
sigma=[0;0;0;0;0;1;1;1;1;1;2;2;2;2;2];
alpha=[0;1;2;3;4;0;1;2;3;4;0;1;2;3;4];
C=[10;11;12;13;14;15;16;17;18;19;20;21;22;23;24];
You have nicely gridded data already. Reshape will suffice to make it into a 2-d array. This is probably why you had an error, because you had the data strung out into vectors.
sigma = reshape(sigma,5,3);
alpha = reshape(alpha,5,3);
C = reshape(C,5,3);
So now we can plot C.
surf(sigma,alpha,C)
And interp2 will now work properly.
interp2(sigma,alpha,C,1.5,2.5)
ans =
20
It is important to understand that scatteredInterpolant is not needed, because your data is completely gridded already. That makes scatteredInterpolant less efficient than need be otherwise. As well, interp2 allows you to use a spline interpolant if you so desire, whereas scatterdInterpolant is limited to at most a linear interpolant. griddedInterpolant does allow the alternative (smoother) methods for interpolation.
In fact, interp2 looks to be something that MAY eventually be replaced by griddedInterpolant, at least they seem to be making hints along those lines in the help.

Andrei Bobrov
Andrei Bobrov 2015-5-21
编辑:Andrei Bobrov 2015-5-21
[y,x] = ndgrid(unique(alpha),unique(sigma));
v = reshape(C,size(x));
f = griddedInterpolant(x,y,v);
example of using:
>>f(1.5,2.5)

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by