MATLAB: Delivery of variables in function and fsolve afterwards doesn’t work

fsolve function

Hello,
I want to do something like this but without global variables. I want to run myfuncall.m and then I want to have the variables A and B available in myfun.m. and the result in command window .
In other programs I already deliviered variables successfully in the function in this style: [result1, result2]=myFunctionname(variable1, variable2) but here I tried but it didn't work.
%myfuncall.m
x0=[0 0];
A=3;
B=4;
x=fsolve(@myfun,x0)
%myfun.m
function F = myfun(x)
F = [2*x(1) - x(2) - exp(-x(1))-A;
-x(1) + 2*x(2) - exp(-x(2))-B];
end
Many thanks in advance

Best Answer

To pass in parameter parameterize your function(lookup in documentation)
%myfuncall.m
x0=[0 0];
A=3;
B=4;
x=fsolve(@(x)myfun(x,A,B),x0) %call it like this it will work
function F= myfun(x,A,B) %add additional parameters
F = [2*x(1) - x(2) - exp(-x(1))-A;
-x(1) + 2*x(2) - exp(-x(2))-B];
end
Gives:
x =
3.3647 3.6948