简体   繁体   English

插值多个值

[英]Interpolation for multiple values

Given this table which I copied from an appendix in a textbook I want to interpolate for a certain variable then print the value into a text file. 给定我从教科书附录中复制的表格,我想对某个变量进行插值,然后将值打印到文本文件中。 数据表

Instead of having to run the program several times to interpolate for more than one variable I want to write down a list of temperatures to interpolate for. 无需多次运行该程序以对多个变量进行插值,我想写下要进行插值的温度列表。 For example, the range i want to interpolate for is: 例如,我要插入的范围是:

[ 50.5 62.4 79.78 ] [50.5 62.4 79.78]

So if I define a range in the program how can i loop the function so that it interpolates for each of the given temperatures and then print them in a text file? 因此,如果我在程序中定义一个范围,如何循环该函数,以便为每个给定温度进行插值,然后将其打印在文本文件中? In the following code i've written is my original code I wrote a few months back. 在下面的代码中,我写的是几个月前写的原始代码。 I want to manipulate this so i interpolate for several values at once: 我想操纵这个,所以我一次插值几个值:

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

I would recommend using interp1 as it can handle multiple input values at once. 我建议使用interp1因为它可以一次处理多个输入值。 However, I suspect you want to modify this code only to achieve the interpolation that you want. 但是,我怀疑您只想修改此代码以实现所需的插值。 What you can do is use input to actually enter in an array of values then loop through the array to interpolate each value. 您可以做的是使用input实际输入值数组,然后在数组中循环以内插每个值。 You can also write to file once you store the interpolated values in another array once you've exhausted all values you wish to interpolate. 一旦将所有要插值的值用尽,也可以将插值存储到另一个数组中,然后写入文件。

Something like this can work. 这样的事情可以工作。 To make things simpler, I'm going to use dlmwrite as a convenience function for writing arrays to file. 为简化起见,我将使用dlmwrite作为将数组写入文件的便捷功能。 One thing you're also going to need to accommodate for is checking if any values in your array are out of range. 您还需要适应的一件事是检查数组中的任何值是否超出范围。 You can use any to help you achieve this. 您可以使用any来帮助您实现这一目标。

Without further ado, here are the modifications. 事不宜迟,这里有修改。 Note that they're commented with the text % New : 请注意,他们用文本% 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 should now contain all of the values you interpolated that you specified. results.txt现在应包含您所插入的所有指定值。 Make sure that when it's time to enter in your values, you actually put in what you want in array notation, so something like this when you're presented it: 确保当需要输入值时,您实际上以数组表示法输入了所需的内容,因此在显示值时类似以下内容:

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

Supposing your temperature data is stored in VarIn and the 4 data sets are stored as 4 column vectors in VarOut . 假设温度数据存储在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

My solution focuses on the interpolation. 我的解决方案集中在插值上。

I removed the input and output handling from the code, to make the example more simple. 我从代码中删除了输入和输出处理,以使示例更加简单。
I modified your loop to support a list of multiple inputs. 我修改了循环以支持多个输入的列表。

Here is my code sample: 这是我的代码示例:

%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;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Result: 结果:

Y =

    15
    25
    35

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

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