Passing parameters too via fsolve?

Suppose I need to solve a system of equations such as

ax + by + c = 0
(a/x)^2 + (b/y)^2 - d^2 = 0

where (x, y) are the variables to solve for, and (a, b, c, d) are all input parameters.

The function fsolve allows only guess values of (x, y) to be passed, e.g. result = fsolve(@f, [1;-1]), where f is the function containing those two equations.

If values of (a, b, c, d) change, the function f needs to know and solve accordingly.

One proven way is to define (a,b,c,d) as global parameters. However, if (a,b,c,d) are rather localized parameters, it is strange to define them as global parameters. It would be best if they could somehow be passed into function f without being defined as global parameters.

Can this be done?

I have tried using a nested function, i.e. pass the guess values of (x, y) AND the parameters (a,b,c,d) into a parent function. Inside it is nested function defining the equations, for example:

function result = parent(x,y,a,b,c,d)

result = fsolve(@f, [1;-1]);

function res = f(z)

  res(1) =...;
  res(2) =...;


But calling the nested function with a function handle @f is not supported.

Any help is much appreciated! Thanks!

The easiest way to do this is to use function handles or anonymous functions

Here is a simple example in which the two variables copy the parameters:

function res = problem(x, p)
  res = [p(1) - x(1); p(2) - x(2)];

> fsolve(@(x)problem(x, [1,2]), [0;0])
ans =

> fsolve(@(x)problem(x, [-5,6]), [0;0])
ans =


Dear Kakila,

Wonderful! That’s what I have been looking for in vain in the documentation!

My initial attempts put the parameters in the wrong place … no wonder. Thanks a lot, really appreciate it!