2017-03-07 4 views
1

教科書の付録からコピーしたこの表では、特定の変数を補間して、その値をテキストファイルに出力したいと考えています。 Data TableData TableData Table複数の値の補間

複数の変数を補間するためにプログラムを何回か実行する代わりに、補間する温度のリストを書き留めたいと思います。例えば、私は補間する範囲は次のとおりです。

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

場合関数が定義されている第1列の「x」値を除いて、行列の各列に対して同じ演算を実行する。 – Yvon

答えて

2

それは、一度に複数の入力値を扱うことができるように私はinterp1を使用することをお勧めします。しかし、私はあなたが望む補間を達成するためにのみこのコードを修正したいと思う。あなたができることは、inputを実際に値の配列に入力し、配列をループして各値を補間することです。また、補間する値をすべて使い果たしたら、別の配列に補間した値を格納してからファイルに書き込むこともできます。

このようなものが動作します。簡単にするために、配列をファイルに書き込むための便利な関数としてdlmwriteを使用します。あなたが対応する必要があることの1つは、配列内の値が範囲外であるかどうかをチェックすることです。これを達成するために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; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

結果:データは、スクリーンショットから、あなたの質問ができ、 `4x5`行列で1つの行列に格納されて

Y = 

    15 
    25 
    35