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

1 次查看(过去 30 天)
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 中查找有关 Fluid Dynamics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by