Is it possible to pass additional constants to fsolve functions?

27 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Torsten
Torsten el 4 de Mayo de 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.
  3 comentarios

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by