2017-03-07 120 views
1

鉴于我从教科书的附录中复制的此表,我想插入某个变量,然后将值打印到文本文件中。 Data Table多值插值

而不是必须运行程序多次插入多个变量我想记下一个温度列表插入。例如,我想内插用于所述范围是:

[50.5 62.4 79.78]

所以,如果我在程序中定义的范围内如何能i循环的功能,使得其内插为每个给定的温度,然后打印在一个文本文件中?在下面的代码中,我写的是我几个月前写的原始代码。我想操纵这个,所以我马上插了几个值:

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 
+0

如果数据被存储在一个矩阵,从截图中是'4x5'矩阵,那么你的问题可以简化为:在矩阵的每一列执行相同的操作,除了第一列是定义函数的'x'值。 – Yvon

回答

2

我会建议使用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]; 
1

假设你的温度数据存储在VarIn和4个的数据存放在VarOut 4个向量。

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 
1

我的解决方案的重点是插。

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

这里是我的代码示例:

%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