MATLAB: Is it possible to pass additional constants to fsolve functions

fsolveMATLABworkspace

Hello,
I was wondering if it's possible to pass additional constant variables from base workspace to the fsolve function?
I have this minimal working example for the fsolve problem:
fsolve_test_1.m:
cfbw1 = 2;
cfbw2 = 3;
firstGuess = [1; 1; 1];
[x, fval] = fsolve(@fsolve_test_1_function_1, firstGuess)
And here is the corresponding fsolve function:
fsolve_test_1_function_1.m:
function F = fsolve_test_1_function_1(x)
cfbw1 = evalin('base', 'cfbw1'); % cfbw = constant from base workspace
cfbw2 = evalin('base', 'cfbw2');
F = [cfbw2 * x(1) * x(2) + x(2) - x(3) - 12;
x(1) + x(2) * x(1) * x(1) + x(3) - 12;
x(1) - x(2) - x(3) + cfbw1];
end
Right now I am using the evalin function to grab the constant variables from the base workspace but I read one should avoid this.
What would be a better solution for this problem?
Thank you!

Best Answer

cfbw1 = 2;
cfbw2 = 3;
firstGuess = [1; 1; 1];
[x, fval] = fsolve(@(x)fsolve_test_1_function_1(x,cfbw1,cfbw2), firstGuess)
function F = fsolve_test_1_function_1(x,cfbw1,cfbw2)
F = [cfbw2 * x(1) * x(2) + x(2) - x(3) - 12;
x(1) + x(2) * x(1) * x(1) + x(3) - 12;
x(1) - x(2) - x(3) + cfbw1];
end
Best wishes
Torsten.