필터 지우기
필터 지우기

Refer to an anonymous equation within the function for the fsolve() method

조회 수: 1 (최근 30일)
Manuela Gräfe
Manuela Gräfe 2017년 4월 25일
답변: Steven Lord 2017년 4월 25일
Hi,
I would like to define a global anonymous equation and refer to it within my external function which I use for the fsolve() method.
This are my two files (This is just a minimal working example of a bigger project):
fsolve_test_1.m
firstGuess = [1;
1;
1];
ate = @(x,y) x + y; %%%%%%%%%%%ate for 'anonymous test equation'
[x, fval] = fsolve(@fsolve_test_1_function_1, firstGuess)
and fsolve_test_1_function_1
function F = fsolve_test_1_function_1( x )
F = [3 * 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) + ate(5,6)]; %%%%%%%%%%%Here I want to call the defined ate() equation with random parameters
end
This gives me the following error code:
Undefined function or variable 'ate'.
Error in fsolve_test_1_function_1 (line 5)
x(1) - x(2) - x(3) + ate(5,6)];
Error in fsolve (line 230)
fuser = feval(funfcn{3},x,varargin{:});
Error in fsolve_test_1 (line 7)
[x, fval] = fsolve(@fsolve_test_1_function_1, firstGuess)
So, what would be the correct way to use the anonymous equation in this case?
Thank you and enjoy your day!

답변 (1개)

Steven Lord
Steven Lord 2017년 4월 25일
Don't make it global; that gives anyone with access to the global workspace (so any function that you run in your MATLAB session) the ability to change the problem you're solving. Instead, pass it into the function as an additional parameter using one of the techniques given in this section of the documentation.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by