2017-03-07 13 views
1

Angesichts dieser Tabelle, die ich aus einem Anhang in einem Lehrbuch kopiert habe, möchte ich für eine bestimmte Variable interpolieren dann den Wert in eine Textdatei drucken. Interpolation für mehrere Werte

Anstatt das Programm mehrmals zu interpolieren, um mehr als eine Variable zu interpolieren, möchte ich eine Liste von Temperaturen aufschreiben, für die interpoliert werden soll. Zum Beispiel ist der Bereich i für interpolieren will:

[50.5 62.4 79.78]

Wenn ich also einen Bereich, in dem Programm definieren, wie kann ich Schleife die Funktion, so dass es für jeden interpoliert der gegebenen Temperaturen und dann drucken sie in einer Textdatei? Im folgenden Code habe ich meinen ursprünglichen Code geschrieben, den ich vor ein paar Monaten geschrieben habe. Ich möchte manipulieren diese so dass ich auf einmal für mehrere Werte interpolieren:

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

Wenn die Daten in einer Matrix gespeichert, die aus dem Screenshot eine '4x5' Matrix ist, dann können Sie Ihre Frage vereinfacht werden als: führe die gleiche Operation für jede Spalte der Matrix durch, außer dass die erste Spalte ein "x" Wert ist, auf dem die Funktionen definiert sind. – Yvon

Antwort

2

würde ich mit interp1 empfehlen, da es mehrere Eingangswerte auf einmal verarbeiten kann. Ich vermute jedoch, dass Sie diesen Code nur ändern möchten, um die gewünschte Interpolation zu erzielen. Was Sie tun können, ist input, um tatsächlich in ein Array von Werten einzugeben, dann durch das Array zu durchlaufen, um jeden Wert zu interpolieren. Sie können auch in eine Datei schreiben, sobald Sie die interpolierten Werte in einem anderen Array gespeichert haben, nachdem Sie alle Werte, die Sie interpolieren möchten, ausgeschöpft haben.

So etwas kann funktionieren. Um die Dinge einfacher zu machen, verwende ich dlmwrite als eine bequeme Funktion zum Schreiben von Arrays in Datei. Eine Sache, die Sie auch berücksichtigen müssen, ist zu überprüfen, ob irgendwelche Werte in Ihrem Array außerhalb des Bereichs liegen. Sie können any verwenden, um dies zu erreichen.

Ohne weitere Umschweife, hier sind die Änderungen. Beachten Sie, dass sie mit dem Text kommentiert sind % 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 sollte nun alle Werte Sie interpoliert, dass Sie angegeben haben. Stellen Sie sicher, dass, wenn es Zeit ist, in der Werte einzugeben, setzen Sie tatsächlich in dem, was Sie in Array-Notation wollen, so etwas wie dies, wenn Sie es präsentiert:

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

Gesetzt Ihre Temperaturdaten in VarIn und der 4-Datensätze wird als 4 Spaltenvektoren in VarOut gespeichert gespeichert werden.

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

Meine Lösung konzentriert sich auf die Interpolation.

Ich entfernte die Eingabe- und Ausgabeverarbeitung aus dem Code, um das Beispiel einfacher zu machen.
Ich modifizierte Ihre Schleife, um eine Liste von mehreren Eingängen zu unterstützen.

Hier ist mein Code Beispiel:

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

Ergebnis:

Y = 

    15 
    25 
    35 
Verwandte Themen