Hi @Ruben,
To efficiently apply fzero across multiple input pairs in MATLAB, you can leverage cellfun. This function allows you to apply a specified function to each cell in a cell array, which is ideal for your use case. Here’s how you can modify your code:
miny = setting.Y.miny; maxy = y(end); xrep = reshape(repmat(x', [y_sz 1]), [x_sz*y_sz 1]); yrep = repmat(y, [x_sz 1]); xy = mat2cell(num2cell([xrep yrep]), ones([x_sz*y_sz 1]), 2); Fmin_bar = MW_mw.Fmin_bar; Smin = griddedInterpolant({x,y}, MW_mw.S_xy_min, "linear", "spline"); dSdy = @(c) (Smin({c{1}, c{2}+1e-9}) - Smin(c)) ./ 1e-9 .* MS_tilde_f(c{:}); t_thres = @(t, c) dSdy(c) - c{1} ./ (r + delta + s .* lamb_min .* Fmin_bar(t));
% Using cellfun to apply fzero across all pairs tr = cellfun(@(z) tryCatchFzero(@(t) t_thres(t, z), [miny maxy*10]), xy);
% Function to handle try-catch for fzero function result = tryCatchFzero(func, interval) try result = fzero(func, interval); catch result = NaN; % Return NaN if fzero fails end end
In this code, cellfun applies the tryCatchFzero function to each element of xy, which contains your input pairs. This approach eliminates the need for explicit loops, enhancing code readability and performance.
Hope this helps. Please let me know if you have any further questions.