フィルターのクリア

Is it possible to pass additional constants to fsolve functions?

11 ビュー (過去 30 日間)
Manuela Gräfe
Manuela Gräfe 2017 年 5 月 4 日
コメント済み: Matt J 2019 年 7 月 29 日
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!

採用された回答

Torsten
Torsten 2017 年 5 月 4 日
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 件のコメント
Agnidhra Gain
Agnidhra Gain 2019 年 7 月 29 日
thank you..
Matt J
Matt J 2019 年 7 月 29 日

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by