MATLAB: How to interpolate from a table using an input value

interpolationMATLABmatlab functiontable

%Number of Blades
B = 3;
%Relative Velocity
U = 10; %m/s
rho = 1.2754; % kg/m^3
a = (1/3);
ar = (1-3*a)/(4*a-1);
om = 11/60; %rps
%Wind Velocity at the rotor
Vw = U*(1-a);
%% Read csv files
filename = 'DBAeroprop.csv'
filename2 = 'AirfoilData.csv'
A = readtable(filename);
A.Properties.VariableNames = {'Node' 'RNodes' 'AeroTwst' 'DRNodes' 'Chord'};
B = readtable(filename2);
B.Properties.VariableNames = {'Angle' 'Cl' 'Cd'}
%% Finding Torque
dr = 1;
r = [0:dr:100];
Q=0; %initialization
for i=1:length(r)
% velocity
v = om*2*pi*r(i);
% Circuferential velocity
Vr = v*r(i)*(1+ar);
% chord
c = interp1(A.RNodes, A.Chord, r(i));
% phi angle
phi = atan((U*(1-a)) / (v*r(i)*(1+ar)));
% Twist angle
theta = interp1(A.RNodes, A.AeroTwst, r(i));
% Angle of attack
alpha = phi - theta;
% Cl and Cd (start assuming Cl and Cd fixed then you have to find the way to define Cl and Cd as function of alpha using the tables)
Cl = find_cl(alpha);
Cd = find_cd(alpha);
% Relative velocity
Ur = sqrt(Vr^2 + Vw^2);
% Torque
dQ = B*(1/2)*rho*Ur^2*(Cl*sin(phi)-Cd*cos(phi))*c(i)*r(i)*dr;
Q = Q+dQ;
%% Finding Cd and Cl based on the angle alpha
function Cl = find_cl(x)
Cl = interp1(B.Angle, B.Cl, x)
function Cd = find_cd(x)
Cd = interp1(B.Angle, B.Cd, x)
I believe I am either misusing the interpolation function or have something confused when utilizing it in a for loop. My error reads:
Unable to resolve the name B.Angle.
Error in BET_review>find_cl (line 56)
Cl = interp1(B.Angle, B.Cl, x)
Error in BET_review (line 45)
Cl = find_cl(alpha);
But I don't see any utilization of the interpolation function working. Any help appreciated. Thanks!

Best Answer

  • You have a script, followed in the same file by some functions.
    You assign some variables in the script, and you want to use them in the functions.
    Variables created in a script are not "shared variables" for the functions defined in the script.
    Variables are only shared when you have nested functions.
    If you were to put a function heading on the top of the file, and you were to put another end at the end of the file, so that the two other functions would be nested inside the first function, then that should work.
    function testtab
    XYZ = sortrows(randi(99, 10, 3), 1);
    B = array2table(XYZ, 'VariableNames', {'Angle' 'Cl' 'Cd'});
    x = 3.8;
    pqr = find_cl(x);
    function Cl = find_cl(x)
    Cl = interp1(B.Angle, B.Cl, x, 'linear', 'extrap');