鉴于我从教科书的附录中复制的此表,我想插入某个变量,然后将值打印到文本文件中。 多值插值
而不是必须运行程序多次插入多个变量我想记下一个温度列表插入。例如,我想内插用于所述范围是:
[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
如果数据被存储在一个矩阵,从截图中是'4x5'矩阵,那么你的问题可以简化为:在矩阵的每一列执行相同的操作,除了第一列是定义函数的'x'值。 – Yvon