I\'m not sure what code is required to perform the 1-D and 2-D interpolation for
ID: 3603581 • Letter: I
Question
I'm not sure what code is required to perform the 1-D and 2-D interpolation for this question.
. Download the file airDensity.csv from the Tutorial directory on Canvas. The file contains values of the air density (in lb/ft3) at a range of temperatures (in °F, the top data row) and pressures (in PSI. the first column). Address following requests by creating one MATLAB script file and executing it in the Command Window Read the data from the file into MATLAB and use the density matrix to perform a 1D interpolation to find the air density at a temperature of 100°F and pressures of 130, 230, 330 and 430 psi. Using ID interpolation, find the air density at 200 and 300 psi and temperatures between 80 and 90 °F with a step of 1°F. Determine the air density at 250 psi and 135 °F. and then at 850 psi and 450°F using the 2D interpolation 1)Explanation / Answer
new_c = interp1(error,c,0,'linear',extrap')
x = 0:10;
y1 = sin(x);
y2 = 2*sin(x);
y = [y1;y2]';
xi = 0:.25:10;
yi = interp1(x,y,xi);
figure
plot(x,y,'o',xi,yi)
function Yi = myLinearInterp(X, Y, Xi)
% X and Xi are column vectros, Y a matrix with data along the columns
[dummy, Bin] = histc(Xi, X); %#ok<ASGLU>
H = diff(X); % Original step size
% Extra treatment if last element is on the boundary:
sizeY = size(Y);
if Bin(length(Bin)) >= sizeY(1)
Bin(length(Bin)) = sizeY(1) - 1;
end
Xj = Bin + (Xi - X(Bin)) ./ H(Bin);
% Yi = ScaleTime(Y, Xj); % FASTER MEX CALL HERE
% return;
% Interpolation parameters:
Sj = Xj - floor(Xj);
Xj = floor(Xj);
% Shift frames on boundary:
edge = (Xj == sizeY(1));
Xj(edge) = Xj(edge) - 1;
Sj(edge) = 1; % Was: Sj(d) + 1;
% Now interpolate:
if sizeY(2) > 1
Sj = Sj(:, ones(1, sizeY(2))); % Expand Sj
end
Yi = Y(Xj, :) .* (1 - Sj) + Y(Xj + 1, :) .* Sj;
for i = 1:size(E, 1)
new_c(i) = interp1(E(i, :), C(i, :), 0, 'linear', 'extrap');
end
nRows = size(E,1);
your_array = cell2mat(arrayfun(@(x) {interp1(E(x, :), C(x, :), 0, 'linear', 'extrap')},(1:nRows)','uniformoutput',false);
e1=error(:,1);
c1=c(:,1);
e2=error(:,2);
c2=c(:,2);
slopes=(c2-c1)./(e2-e1);
new_c = c1-slopes.*e1;
figure
plot(x,real(v),'*r',xq,real(vq),'-r');
hold on
plot(x,imag(v),'*b',xq,imag(vq),'-b');
x = (datetime(2016,1,1):hours(4):datetime(2016,1,2))';
x.Format = 'MMM dd, HH:mm';
T = [31 25 24 41 43 33 31]';
WeatherData = table(x,T,'VariableNames',{'Time','Temperature'})
xq = (datetime(2016,1,1):minutes(1):datetime(2016,1,2))';
V = interp1(WeatherData.Time, WeatherData.Temperature, xq, 'spline');
vq = interp1(x,v,xq,'pchip');
figure
plot(x,v,'o',xq,vq);
h = gca;
h.XTick = -5:5;
function Yi = LeanInterp(X, Y, Xi)
X = X(:);
Xi = Xi(:);
Y = Y(:);
nY = numel(Y);
[dummy, Bin] = histc(Xi, X); %#ok<ASGLU>
H = diff(X);
if Bin(length(Bin)) >= nY
Bin(length(Bin)) = nY - 1;
end
Ti = Bin + (Xi - X(Bin)) ./ H(Bin);
% Interpolation parameters:
Si = Ti - floor(Ti);
Ti = floor(Ti);
% Shift frames on boundary:
d = (Ti == nY);
Ti(d) = Ti(d) - 1;
Si(d) = 1;
% Now interpolate:
Yi = Y(Ti) .* (1 - Si) + Y(Ti + 1) .* Si;
% make Y1 array for interpolation
logz = log(10e-8):.10:log(1080);
Y1 = d + exp(logz); % This is the 1 x 232 vector.
Y2 = ones(48,21); % Preallocating Y2 2D-array (size = 48 x 21).
Y2_array = ones(48,21,61); % Preallocating Y2 3D-array (size = 48 x 21 x 61)
% Interpolation
for S = 1:61
for T = 1:48
X1_row = X1(T,:,S);
Y2 = interp1(X1_row, Y1, X2(T,:,S));
% X2 is a 3D-array, size (48 x 21 x 61)
Y2_array(T,:,S) = Y2;
end
end
iy = find(v1==v1(1)); % e.g. [0,1,2,3,4,5,6,7,8,9,10,0,1,2...,9,10] for N points
ix = find(v2==v2(1)); % e.g. [0,0,...,2,2,...,4,4...,10,10] for N points
vx = v1(ix); %e.g. [0,1,2,3,4,5,6,7,8,9,10]
vy = v2(iy); %e.g. [0,2,4,6,8,10]
vx_sp = 0:0.005:10;
vy_sp = 0:0.005:10;
ypos_sq = reshape(ypos,length(vx),length(vy));
xpos_sq = reshape(xpos,length(vx),length(vy));
mesh(vy,vx,ypos_sq); %and same for the x_map plot
V = [-1 0; 0 0; 1 0; -1 1; 0 1; 1 1; -1 2; 0 2; 1 2];
P = [-14 -2; 0 0; 13.5 -1; -13 7; 0.5 8; 14 7; -14 12; 1 11; 14.5 11];
PV = (b,V) [[(b(1).*V(:,1)+b(2))] [(b(3).*V(:,2)+b(4))]]; % Objective Function
[B, rn, r] = lsqcurvefit(PV, ones(4,1), V, P);
Pc = PV(B,V);
component tlu_2d
inputs
u1 = 0;
u2 = 0;
end
outputs
f = 0;
end
parameters (Size=variable)
x1d = [1 2 3 4];
x2d = [1 2 3];
fd = [1 2 3; 3 4 5; 5 6 7; 7 8 9];
end
equations
f == tablelookup(x1d, x2d, fd, u1, u2, interpolation=smooth, extrapolation=nearest);
end
end
component TtoP
inputs
u = {0, 'K'}; % temperature
end
outputs
y = {0, 'Pa'}; % pressure
end
parameters (Size=variable)
xd = {[100 200 300 400] 'K'};
yd = {[1e5 2e5 3e5 4e5] 'Pa'};
end
equations
y == tablelookup(xd, yd, u);
end
end