Interpolation of data that depends on two variables

35 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 21 May 2015
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.

More Answers (2)

John D'Errico
John D'Errico on 21 May 2015
Edited: John D'Errico on 21 May 2015
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 on 21 May 2015
Edited: Andrei Bobrov on 21 May 2015
[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)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!