How do I run the function for each value in array?
显示 更早的评论
Hello,
the following code calculates two time constants to correct thermocouple measurements. At the moment it works for one pair of measured temperature at the time x, Tth1 and Tth2. The goal is to import columns of data pairs out of excel and let the code run for each data pair and give the time constants for each pair to get the mean time constant afterwards with least squares method.
Problem is that the code can not handle imported column vectors and just gives an error message:
Error using fmincon
Supplied objective function must return a scalar value.
Error in versuch (line 53)
[tau,fval] = fmincon(fun,tau0,[],[],[],[],[0,0],[],[]);
Everything I tried until now will not give me any results, it just works if I type in each pair seperatly.
An example of the excel data pairs is attached.
clear
global Tth1 Tth2 A B
data=
%Tth1=1. thermocouple bead temperature
Tth1= 1747.15;
%Tth2=2. thermocouple bead temperature
Tth2= 1704.15;
%d1= 1. bead diameter (m)
d1=0.000125;
%d2= 2. bead diameter (m)
d2=0.000325;
%epsilon= bead emissivity(assumed 0.95 for soot coated)
epsilon=0.95;
%sigma= Stefan Boltzman constant (W/(m^2*K^4)
sigma=5.67*10^-8;
%rho= bead density(kg/m^2)
rho=21460;
%cp= [J/(kg*K)]
cp=133;
A=(6*epsilon*sigma)/(rho*cp*d1);
B=(6*epsilon*sigma)/(rho*cp*d2);
% calculating the results
fun = @(tau)(Tth1 + tau(1)*A*Tth1^4 - (Tth2 + tau(2)*B*Tth2^4))^2;
tau0 = [0,0];
[tau,fval] = fmincon(fun,tau0,[],[],[],[],[0,0],[],[]);
% display of results
disp(['tau1 = ',num2str(tau(1))])
disp(['tau2 = ',num2str(tau(2))])
% check
% y1 and y2 should be identical at best
y1 = Tth1 + tau(1)*A*Tth1^4;
y2 = Tth2 + tau(2)*B*Tth2^4;
deviation = abs(y1-y2);
disp(['Abweichung: ',num2str(deviation)]);
deviation
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!