Is it possible to pass additional constants to fsolve functions?

26 views (last 30 days)
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!

Accepted Answer

Torsten
Torsten on 4 May 2017
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.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!