How to solve a nonlinear system in Matlab without saving a function

3.3k Views Asked by At

Peace be upon you,

I have a system of equations to be solved. I know that I can solve my system in Matlab like this:

function F = myfun(x)
F = [2*x(1) - x(2) - exp(-x(1));
    -x(1) + 2*x(2) - exp(-x(2))];

and save it as "myfun.m" and then in command line I write

x0 = [-5; -5];
[x,fval] = fsolve(@myfun,x0)

But I would like to know how I can do this without defining a function. Because I am using Matlab command line via a proxy provided from another software.

1

There are 1 best solutions below

0
On BEST ANSWER

You can do it by defining an inline anonymous function:

fsolve(@(x) [2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))],
      [-5; -5])

will do exactly what you want, in a single line that can be evaluated in the interactive prompt.