Effacer les filtres
Effacer les filtres

Is it possible to pass additional constants to fsolve functions?

11 vues (au cours des 30 derniers jours)
Manuela Gräfe
Manuela Gräfe le 4 Mai 2017
Commenté : Matt J le 29 Juil 2019
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!

Réponse acceptée

Torsten
Torsten le 4 Mai 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 commentaires
Agnidhra Gain
Agnidhra Gain le 29 Juil 2019
thank you..

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by