Interpolation for several values

advertisements

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 ]

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. 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. 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. 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.

Without further ado, here are the modifications. Note that they're commented with the text % 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. 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];