How do I run the function for each value in array?

4 views (last 30 days)
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.
global Tth1 Tth2 A B
%Tth1=1. thermocouple bead temperature
Tth1= 1747.15;
%Tth2=2. thermocouple bead temperature
Tth2= 1704.15;
%d1= 1. bead diameter (m)
%d2= 2. bead diameter (m)
%epsilon= bead emissivity(assumed 0.95 for soot coated)
%sigma= Stefan Boltzman constant (W/(m^2*K^4)
%rho= bead density(kg/m^2)
%cp= [J/(kg*K)]
% 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)]);
  1 Comment
Matheo Schaaf
Matheo Schaaf on 7 Aug 2022
Thanks for the help! I'm relatively new to MATLAB and for now I'm unsure how to write the drivers script so that each of my 34000 values gets used.

Sign in to comment.

Accepted Answer

dpb on 6 Aug 2022
global Tth1 Tth2 A B
Therein lies your problem -- you wrote a script instead of a function -- get rid of the clear statement; wrap the code in a function with the needed inputs passed as arguments.
Then, write a driver script that calls the function for each pair of inputs in turn, saving the results returned as outputs of the function, NOT as global variables.
You undoubtedly could also vectorize the function itself to accept an array or pair of vectors and do the looping internally or using MATLAB builtin vectorized operations, but I didn't look at the code enough to see how much trouble that might turn out to be; the first option is almost a trivial exercise.
dpb on 7 Aug 2022
<Is-arrayfun-faster-than-for-loop?> (in which thread you also participated, Walter :) ) shows that depending on the function and whether in m-file or command line, the overhead with arrayfun may be sizable. Probably even with OPs size of some 35K elements the time won't be but fraction of to maybe seconds (depending on how the fmincon handles the solution) so I opted for the loop...besides being less abstract for the newcomer.
I thought about but hadn't tried to see if perhaps fsolve wouldn't perhaps be faster.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!