繁体   English   中英

插值多个值

[英]Interpolation for multiple values

给定我从教科书附录中复制的表格,我想对某个变量进行插值,然后将值打印到文本文件中。 数据表

无需多次运行该程序以对多个变量进行插值,我想写下要进行插值的温度列表。 例如,我要插入的范围是:

[50.5 62.4 79.78]

因此,如果我在程序中定义一个范围,如何循环该函数,以便为每个给定温度进行插值,然后将其打印在文本文件中? 在下面的代码中,我写的是几个月前写的原始代码。 我想操纵这个,所以我一次插值几个值:

clear all
%Format Long is used to ensure the maximum amount of displayed digits%
format long g 
%A prompt is used to enter the name of the file name that will be used for interpolation%
prompt='Please Enter the Exact Name of the File Being Interpolated: ';
File_Name= input(prompt,'s');
%File is read and distibuted in 1X1 Matrices with the corresponding variable%
[T, K, p, a, Pr] = textread(File_Name, '%f%f%f%f%f','headerlines' ,4);
%Prompt to ask  user for the variable to interpolate%
prompt2='Please Enter the Variable You Wish To Interpolate (T,K,p,a,Pr): ';
VarIn= input(prompt2);
prompt3='Please Enter the Value of Interpolation: ';
Val= input(prompt3);
prompt4='Please Enter the Desired Output Variable: ';
VarOut= input(prompt4);
%If statement used when value is out of the range% 
if Val<VarIn(1,1)
    disp('Error: The inputted value is out of range.')
elseif Val>VarIn(size(VarIn,1),1)
    disp('Error: The inputted value is out of range.')
else
%The for statement is used to make sure the loop is independent of the matrix size%
    for i= 1:size(VarIn,1)
       if Val<VarIn(i+1,1)
           %Interpolation Formula%
       Y=((VarOut(i+1,1)-VarOut(i,1))/(VarIn(i+1,1)-VarIn(i,1)))*(Val-VarIn(i,1))+VarOut(i,1);
       answer=Y; 
       %Display Answer%
       fprintf('The Interpolated value is: %f\n',answer)
       break  
      end
   end
end

我建议使用interp1因为它可以一次处理多个输入值。 但是,我怀疑您只想修改此代码以实现所需的插值。 您可以做的是使用input实际输入值数组,然后在数组中循环以内插每个值。 一旦将所有要插值的值用尽,也可以将插值存储到另一个数组中,然后写入文件。

这样的事情可以工作。 为简化起见,我将使用dlmwrite作为将数组写入文件的便捷功能。 您还需要适应的一件事是检查数组中的任何值是否超出范围。 您可以使用any来帮助您实现这一目标。

事不宜迟,这里有修改。 请注意,他们用文本% New注释:

clear all
%Format Long is used to ensure the maximum amount of displayed digits%
format long g 
%A prompt is used to enter the name of the file name that will be used for interpolation%
prompt='Please Enter the Exact Name of the File Being Interpolated: ';
File_Name= input(prompt,'s');
%File is read and distibuted in 1X1 Matrices with the corresponding variable%
[T, K, p, a, Pr] = textread(File_Name, '%f%f%f%f%f','headerlines' ,4);
%Prompt to ask  user for the variable to interpolate%
prompt2='Please Enter the Variable You Wish To Interpolate (T,K,p,a,Pr): ';
VarIn= input(prompt2);

% New - Enter in an array of values
% Example: [50.5 62.4 79.78];
% Val is now an array of values
prompt3='Please Enter the Values of Interpolation: ';
Val = input(prompt3);
prompt4='Please Enter the Desired Output Variable: ';
VarOut= input(prompt4);

% New - Check if any values in the array are out of range
%If statement used when value is out of the range% 
if any(Val<VarIn(1,1))
    disp('Error: An inputted value is out of range.')
elseif any(Val>VarIn(size(VarIn,1),1))
    disp('Error: An inputted value is out of range.')
else
%The for statement is used to make sure the loop is independent of the matrix size%
    % New - Store answers
    answer = zeros(numel(Val), 1);
    % New - Loop through each value
    for k = 1 : numel(Val)       
        for i= 1:size(VarIn,1)
           if Val(k)<VarIn(i+1,1) % New - Val is an array
               %Interpolation Formula%
               Y=((VarOut(i+1,1)-VarOut(i,1))/(VarIn(i+1,1)-VarIn(i,1)))*(Val-VarIn(i,1))+VarOut(i,1);
               answer(k)=Y; % New - Store answer
               %Display Answer%
               % New - edit so that we also display which value we are at
              fprintf('The Interpolated value #%d is: %f\n', k, answer)
              break  
          end
       end
   end
   % New - Now write to file
   dlmwrite('results.txt', answer);
end

results.txt现在应包含您所插入的所有指定值。 确保当需要输入值时,您实际上以数组表示法输入了所需的内容,因此在显示值时类似以下内容:

Please Enter the Values of Interpolation: [50 62.5 79.78];

假设温度数据存储在VarIn ,而4个数据集作为4个列向量存储在VarOut

if Val<VarIn(1,1) || Val>VarIn(end,1)
    error('The inputted value is out of range.')
else
    %The for statement is used to make sure the loop is independent of the matrix size%
    for ii= 1:size(VarIn,1)
        if Val<VarIn(ii+1,1)
            %Interpolation Formula%
            Y = zeros(1,size(VarOut,2));
            for jj = 1:size(VarOut,2)
                Y(jj)=((VarOut(ii+1,jj)-VarOut(ii,jj))/(VarIn(ii+1,1)-VarIn(ii,1)))*(Val-VarIn(ii,1))+VarOut(ii,jj);
            end
            answer=Y;
            break
        end
    end
    fprintf('The Interpolated value is: ')
    for jj = 1:length(answer)
        fprintf('%f', answer(jj));
    end
    fprintf('\n')
end

我的解决方案集中在插值上。

我从代码中删除了输入和输出处理,以使示例更加简单。
我修改了循环以支持多个输入的列表。

这是我的代码示例:

%Set some input values for testing:
VarIn = [50; 60; 70; 80];
Val = [55; 65; 75];
VarOut = [10; 20; 30; 40];

%Original if statement:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%If statement used when value is out of the range% 
% if Val<VarIn(1,1)
%     disp('Error: The inputted value is out of range.')
% elseif Val>VarIn(size(VarIn,1),1)
%     disp('Error: The inputted value is out of range.')
% else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Replace Original if statement with the following code:
%The code remove all values out of range from Val:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Val(Val<VarIn(1,1)) = []; %Remove values where Val<VarIn(1,1) from input list.
Val(Val>VarIn(size(VarIn,1),1)) = []; %Remove values where Val<>VarIn(size(VarIn,1),1)) from input list.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Original loop, performs interpolation of single value:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %The for statement is used to make sure the loop is independent of the matrix size%
% for i= 1:size(VarIn,1)
%     if Val<VarIn(i+1,1)
%         %Interpolation Formula%
%         Y=((VarOut(i+1,1)-VarOut(i,1))/(VarIn(i+1,1)-VarIn(i,1)))*(Val-VarIn(i,1))+VarOut(i,1);
%         answer=Y; 
%         %Display Answer%
%         fprintf('The Interpolated value is: %f\n',answer)
%         break  
%     end
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Replace Original loop with following code:
%The code computes interpolation of all values of Val:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idx1 = zeros(size(Val)); %Just initialize.

%Instead using a loop, use find function to return first index where Val<VarIn(i+1,1)
for j = 1:length(Val)
    idx1(j) = find(Val(j) < VarIn, 1);
end

%idx0 is the first index where Val>=VarIn(i,1)
idx0 = idx1 - 1;

VarOut0 = VarOut(idx0); %Get element above with index below index of Val.
VarOut1 = VarOut(idx1); %Get element above with index above index of Val.

VarIn0 = VarIn(idx0); %Below.
VarIn1 = VarIn(idx1); %Above.

%Vectoraise the computation - use ./ instead of / and .* instead of *
Y = (VarOut1-VarOut0)./(VarIn1-VarIn0).*(Val-VarIn0) + VarOut0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

结果:

Y =

    15
    25
    35

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM