bilinear interpolation of a 3D graph

Hi,
I am trying to do an interpolation between the lines for find the ball velcoity.
Eg, for a given and interpolated x and y, how do i find the z?
The data is persented in colombs for x and y at a given value, z. for which there are 10 colombs for x and y.
The z values for each a generated using the 'ones' function.
I appreciate that i have "hard loaded" in the data. but this is for a university project and have little experience with coding in general. but some understanding of how to interpolate in a table would be greatly appreciated
% Figure properties
figure1 = figure('Color',[1 1 1]);
grid on
hold on
title('Golf Ball Aeroynamics');
xlabel('Spin Rate, [R.P.M]');
ylabel('Drag Coefficent, C_D');
zlabel('Ball Velocity [m/s]');
% Load variables from file
u14ms = [10.0181584038799,0.497489996266905;1048.57744598596,0.465912907853750;1866.50469521945,0.368711068058607;2815.18206925372,0.407423263914084;3741.62610823528,0.410999691385817;4679.15315568930,0.520915473810196;6258.19729101443,0.547523261903284];
u21p9ms = [-0.159115527488822,0.356936519024359;1016.16683700188,0.290223662227460;1808.11541645867,0.299366073247347;2816.67530728093,0.351016197436142;3738.14392611444,0.367539121000365;4676.55222879764,0.444165487142678;6251.28188539660,0.458752709116708];
u30p5ms = [-2.49076998801287,0.270014766270655;970.525159924220,0.264338013836057;1859.98095859237,0.265145219146669;2803.92770559781,0.307556851016285;3744.09851873935,0.317604384594470;4669.24515418907,0.370190230481464;6257.62814701226,0.394022676585451];
u39ms = [6.92152544580862,0.264465306258049;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2823.26636201580,0.277039104826533;3740.15122969201,0.291713228357884;4679.63662209975,0.327652530155452;6253.99908901989,0.356110342247008];
u47p2ms = [-2.34389411648379,0.264466530223645;965.867970831152,0.265263331826690;1860.00543790429,0.264220513138833;2823.24188270388,0.277963810834368;3744.97977396853,0.284314968312405;4666.20359968282,0.310084951954979;6250.17419653215,0.325595655971247];
u55p5 = [-2.17253893303321,0.257993588168799;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2818.63365223466,0.277039716809331;3740.37154349930,0.283390874287368;4675.73829167625,0.299911961903196;6246.00659367752,0.308026853805178];
u64ms = [2.50912947195603,0.256143564170330;965.892450143074,0.264338625818855;1859.98095859237,0.265145219146669;2828.19282354001,0.265942020749714;3740.54289868275,0.276917932232522;4676.03204341930,0.288815489809175;6255.54128567095,0.297853863753395];
u72p8ms = [2.60704671964208,0.252444740138990;965.892450143074,0.264338625818855;1864.63814768544,0.264219901156035;2823.58459307078,0.265017926724677;3740.59185730659,0.275068520216852;4676.25235722660,0.280493135738659;6260.39430925938,0.289530897700081];
u81p1ms = [2.60704671964208,0.252444740138990;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2823.56011375886,0.265942632732512;3740.64081593044,0.273219108201181;4676.32579516236,0.277719017715153;6255.90847534977,0.283983273635868];
u89ms = [2.75392259117115,0.246896504091979;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2828.19282354001,0.265942020749714;3740.73873317812,0.269520284169841;4685.71361128426,0.273094263710382;6260.68806100244,0.278434425606060];
a = ones(1,7)'*14;
b = ones(1,7)'*21.9;
c = ones(1,7)'*30.5;
d = ones(1,7)'*39;
e = ones(1,7)'*47.2;
f = ones(1,7)'*55.5;
g = ones(1,7)'*64;
h = ones(1,7)'*72.8;
i = ones(1,7)'*81.1;
j = ones(1,7)'*89;
hold on
% Plots
x = ([u14ms(:,1),u21p9ms(:,1),u30p5ms(:,1),u39ms(:,1),u47p2ms(:,1),u55p5(:,1),u64ms(:,1),u72p8ms(:,1),u81p1ms(:,1),u89ms(:,1)]); % Spin
y = ([u14ms(:,2),u21p9ms(:,2),u30p5ms(:,2),u39ms(:,2),u47p2ms(:,2),u55p5(:,2),u64ms(:,2),u72p8ms(:,2),u81p1ms(:,2),u89ms(:,2)]); % Lift Coeffcient
z = ([a,b,c,d,e,f,g,h,i,j]); % Velocity
surf(x,y,z);

 Accepted Answer

Missing data, so I can’t run the code to demonstrate the approach.
Since the data appear to be gridded (not scattered), the griddedInterpolant function is likely the most appropriate approach. For any ‘x’ and ‘y’ within the grid, it is the possible to return ‘z’.
If the data are initially scattered, use the scatteredInterpolant function instead. For any ‘x’ and ‘y’ within the grid, it is the possible to return ‘z’.
EDIT — (5 Jan 2022 at 13:26)
Having the data and the rest of the relevant code makes this possible, however the interpolation functions are having problems with the data. I opted for scatteredInterpolant here because the data appear to be such.
I took two approaches — the first with the original data and interpolation, and the second with an interpolated mesh and the same interpolation. The results are not as good as I would have liked them to be, since scatteredInterpolant usually gives better results.
Neverhteless, this is an appropriate way to interpolate the data, although there are similar approaches that could be worth experimenting with (although I doubt they would give different results).
% Figure properties
figure1 = figure('Color',[1 1 1]);
grid on
hold on
title('Golf Ball Aeroynamics');
xlabel('Spin Rate, [R.P.M]');
ylabel('Drag Coefficent, C_D');
zlabel('Ball Velocity [m/s]');
% Load variables from file
u14ms = [10.0181584038799,0.497489996266905;1048.57744598596,0.465912907853750;1866.50469521945,0.368711068058607;2815.18206925372,0.407423263914084;3741.62610823528,0.410999691385817;4679.15315568930,0.520915473810196;6258.19729101443,0.547523261903284];
u21p9ms = [-0.159115527488822,0.356936519024359;1016.16683700188,0.290223662227460;1808.11541645867,0.299366073247347;2816.67530728093,0.351016197436142;3738.14392611444,0.367539121000365;4676.55222879764,0.444165487142678;6251.28188539660,0.458752709116708];
u30p5ms = [-2.49076998801287,0.270014766270655;970.525159924220,0.264338013836057;1859.98095859237,0.265145219146669;2803.92770559781,0.307556851016285;3744.09851873935,0.317604384594470;4669.24515418907,0.370190230481464;6257.62814701226,0.394022676585451];
u39ms = [6.92152544580862,0.264465306258049;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2823.26636201580,0.277039104826533;3740.15122969201,0.291713228357884;4679.63662209975,0.327652530155452;6253.99908901989,0.356110342247008];
u47p2ms = [-2.34389411648379,0.264466530223645;965.867970831152,0.265263331826690;1860.00543790429,0.264220513138833;2823.24188270388,0.277963810834368;3744.97977396853,0.284314968312405;4666.20359968282,0.310084951954979;6250.17419653215,0.325595655971247];
u55p5 = [-2.17253893303321,0.257993588168799;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2818.63365223466,0.277039716809331;3740.37154349930,0.283390874287368;4675.73829167625,0.299911961903196;6246.00659367752,0.308026853805178];
u64ms = [2.50912947195603,0.256143564170330;965.892450143074,0.264338625818855;1859.98095859237,0.265145219146669;2828.19282354001,0.265942020749714;3740.54289868275,0.276917932232522;4676.03204341930,0.288815489809175;6255.54128567095,0.297853863753395];
u72p8ms = [2.60704671964208,0.252444740138990;965.892450143074,0.264338625818855;1864.63814768544,0.264219901156035;2823.58459307078,0.265017926724677;3740.59185730659,0.275068520216852;4676.25235722660,0.280493135738659;6260.39430925938,0.289530897700081];
u81p1ms = [2.60704671964208,0.252444740138990;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2823.56011375886,0.265942632732512;3740.64081593044,0.273219108201181;4676.32579516236,0.277719017715153;6255.90847534977,0.283983273635868];
u89ms = [2.75392259117115,0.246896504091979;970.525159924220,0.264338013836057;1860.00543790429,0.264220513138833;2828.19282354001,0.265942020749714;3740.73873317812,0.269520284169841;4685.71361128426,0.273094263710382;6260.68806100244,0.278434425606060];
a = ones(1,7)'*14;
b = ones(1,7)'*21.9;
c = ones(1,7)'*30.5;
d = ones(1,7)'*39;
e = ones(1,7)'*47.2;
f = ones(1,7)'*55.5;
g = ones(1,7)'*64;
h = ones(1,7)'*72.8;
i = ones(1,7)'*81.1;
j = ones(1,7)'*89;
hold on
% Plots
x = ([u14ms(:,1),u21p9ms(:,1),u30p5ms(:,1),u39ms(:,1),u47p2ms(:,1),u55p5(:,1),u64ms(:,1),u72p8ms(:,1),u81p1ms(:,1),u89ms(:,1)]); % Spin
y = ([u14ms(:,2),u21p9ms(:,2),u30p5ms(:,2),u39ms(:,2),u47p2ms(:,2),u55p5(:,2),u64ms(:,2),u72p8ms(:,2),u81p1ms(:,2),u89ms(:,2)]); % Lift Coeffcient
z = ([a,b,c,d,e,f,g,h,i,j]); % Velocity
% Qsz = [size(x); size(y); size(z)]
% format longG
% Ranges = [min(x(:)) max(x(:)); min(y(:)) max(y(:)); min(z(:)) max(z(:))]
F1 = scatteredInterpolant(x(:), y(:), z(:)); % Interpolatiion Function
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
F1.Method = 'linear';
Xq = pi*1000
Xq = 3.1416e+03
% Xq = x(3,5)
Yq = pi/10
Yq = 0.3142
% Yq = y(3,5)
% Zr = z(3,5)
Zi = F1(Xq, Yq) % Interpolated 'z' Value At (3141.6, 0.31416)
Zi = 55.3334
surfc(x,y,z, 'FaceAlpha',0.5)
stem3(Xq, Yq, Zi, '^r', 'filled', 'LineWidth',2)
view(120,30)
N = 50;
xv = linspace(min(x(:)), max(x(:)), N);
yv = linspace(min(y(:)), max(y(:)), N);
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(x(:), y(:), z(:), Xm, Ym, 'linear'); % Interpolate & Plot Surface
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
F2 = scatteredInterpolant(Xm(:), Ym(:), Zm(:));
F2.Method = 'linear';
Xq = pi*1000
Xq = 3.1416e+03
Yq = pi/10
Yq = 0.3142
Zi = F2(Xq, Yq) % Interpolated 'z' Value At (3141.6, 0.31416)
Zi = 54.1900
figure
surfc(Xm, Ym, Zm, 'FaceAlpha',0.5)
hold on
stem3(Xq, Yq, Zi, '^r', 'filled', 'LineWidth',2)
hold off
view(120,30)
Interpolating the values on the interpolated surface with a much finer mesh gives much better results than interpolating on the original data matrices.
Experiment to get different results.
.

3 Comments

Thank you for the speedy reply. I will take a look and get back to you if any of thoese work.
Meanwhile I have included the missing data and now runs as intended. Any futer help would be super helpful.
Tom.
Thank you so much! Amazing work.
As always, my pleasure!
.

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!