教科書の付録からコピーしたこの表では、特定の変数を補間して、その値をテキストファイルに出力したいと考えています。 複数の値の補間
複数の変数を補間するためにプログラムを何回か実行する代わりに、補間する温度のリストを書き留めたいと思います。例えば、私は補間する範囲は次のとおりです。
[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
場合関数が定義されている第1列の「x」値を除いて、行列の各列に対して同じ演算を実行する。 – Yvon