What is the most efficient way to "reverse" interpolate a 3D array?
21 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a 3-D array of data: it contains Fz values for different values of alpha angle, beta angle and Mach number.
I would like to know the best way to "reverse interpolate" the value of alpha for given/target values of beta, Mach and Fz.
The best method I can think of is a 2-step interpolation process:
1) For each value of alpha, and the given target values of beta and Mach, interpolate the "beta-Mach space" to extract Fz.
2) For the extracted Fz values from step 1, interpolate this space for the target Fz to find alpha.
Here is how it is implemented (data.mat is attached):
% load interpolation data
load data
% Set up interpolation target values
Fz_target = 19.4801;
beta_target = 0;
Mach_target = 0.1;
% Step 1: interpolate "beta-Mach space" for each alpha in alpha_array
[beta_points, Mach_points] = ndgrid(beta_array, Mach_array);
Fz_space = zeros(1,alphan);
for i = 1:alphan
Fz_space(i) = interpn(beta_points, Mach_points, reshape(Fz_basic(i,:,:), [betan, Machn]), beta_target, Mach_target);
end
% Step 2: interpolate Fz_space from step 1 for Fz_target to get alpha
alpha_extract = interpn(Fz_space, alpha_array, Fz_target);
Is there a more efficient, faster, less verbose way to do this?
If I pursue the above method, is there a way to vectorize the for loop?
0 commentaires
Réponse acceptée
Jan
le 10 Fév 2023
Creating the grid matrices is not useful. The interpolation is about 10 times faster using the original vectors.
% Omit:
% [beta_points, Mach_points] = ndgrid(beta_array, Mach_array);
% Use the vectors instead:
% Fz_space(i) = interpn(beta_points, Mach_points, reshape(Fz_basic(i,:,:), [betan, Machn]), beta_target, Mach_target);
Fz_space(i) = interpn(beta_array, Mach_array, reshape(Fz_basic(i,:,:), [betan, Machn]), beta_target, Mach_target);
0 commentaires
Plus de réponses (1)
Star Strider
le 10 Fév 2023
The loop would likely not be necessary interpolationg with the scatteredInterpolant function. Pass all the ‘_target’ values as column vectors (with the rows matching the desired matching values) for any of the three values to get the fourth. The code is a bit more involved initially, however after that, the interpolations are straightforward.
This defines scatteredInterpolant functions for all the values as functions of one of the other values —
LD = load(websave('data','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1291810/data.mat'))
alpha = LD.alpha_array;
beta = LD.beta_array;
Mach = LD.Mach_array;
Fz = LD.Fz_basic;
CheckSame = Fz(:,:,2)-Fz(:,:,1);
alphav = reshape(alpha(:)*ones(1,numel(beta)*numel(Mach)), [],1);
betav = reshape(beta(:)*ones(1,numel(alpha)*numel(Mach)),[],1);
Machv = reshape(Mach(:)*ones(1,numel(alpha)*numel(beta)),[],1);
Fzv = reshape(Fz, [], 1);
alphafcn = scatteredInterpolant(betav,Machv,Fzv,alphav);
betafcn = scatteredInterpolant(alphav,Machv,Fzv,betav);
Machfcn = scatteredInterpolant(alphav,betav,Fzv,Machv);
Fzfcn = scatteredInterpolant(alphav,betav,Machv,Fzv);
Fz_target = 19.4801;
beta_target = 0;
Mach_target = 0.1;
alpha_interp = alphafcn(beta_target, Mach_target, Fz_target)
figure
surfc(beta, alpha, Fz(:,:,1))
hold on
surfc(beta, alpha, Fz(:,:,2))
scatter3(beta_target, alpha_interp, Fz_target, 250, 'c', 'p', 'filled')
hold off
colormap(turbo)
xlabel('\beta')
ylabel('\alpha')
zlabel('F_z')
.
0 commentaires
Voir également
Catégories
En savoir plus sur Data Distribution Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!