Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How can I solve this set of nonlinear equations using MATLAB?

The equations are:

4xy + 4y^2 - 3cos(y) = -2

5x^2 - 2xy^2 - 5sin(x) = -5

However, I also have the following constraints:

-10 < x < 10

-10 < y < 10

I know I could use the solve function, but I'm confused about how to incorporate the constraints as well.

Thank you!

2 Answers

Relevance
  • 10 years ago
    Favorite Answer

    you could use solve and pick out the solutions manually or discard them using code if they are not in the specified range. solve should give you one or more answers. it will not, however, allow you to specify constraints.

    [x,y] = solve('4*x*y + 4*y^2 -3*cos(y) = -2','5*x^2 - 2*x*y^2 -5*sin(x) = -5')

    the output is:

    x =

    3.5299627007561095062706032568187

    y =

    -3.1307059243938364157208807249664

    I decided to go for a more sophisticated technique here, but this will only give you one answer, not multiple. it does, however, allow you to specify linear or nonlinear constraints. I used the function fmincon. this does a constrained optimization on a given objective function. you could use as your objective function the sum of the squares of the two different equations, which will always be greater than or equal to zero.

    the problem here is that it uses a newton-based algorithm and depending on the guess x0 given will converge to different answers, perhaps not a good idea if we have nonlinear functions.

    the idea here is that we want to have the sum of squares to be zero, and the only way this can happen is if each square is also zero meaning each equation is satisfied. the answer may not be exact using this method but it can be very close provided a good initial guess. see the code below. note that you will need the matlab optimization toolbox to run it.

    you will want to make 2 script files. note that x(1) is your x and x(2) is your y.

    %-------file objective.m-----------

    function f = objective(x)

    f = (4*x(1)*x(2) + 4*x(2)^2 - 3*cos(x(2)) + 2)^2 + (5*x(1)^2 - 2*x(1)*x(2)^2 -5*sin(x(1)) + 5)^2

    %-----------end file objective.m-------------

    %-------main file - run the code from here, call it whatever you want-------------

    x0 = [5,-3]

    [x,fval] = fmincon(@objective,x0,[],[],[],[],[-10 -10],[10 10]) %bounds are included on x and y

    check1 = 4*x(1)*x(2) + 4*x(2)^2 - 3*cos(x(2)) %just to see if our answer is right, if not try different x0

    check2 = 5*x(1)^2 - 2*x(1)*x(2)^2 -5*sin(x(1))

    %-------end main file------------

    and after some different tries of initial guesses (took a few tries to get the function value close to zero), our output is:

    x =

    3.5300 -3.1307 (this means x= 3.5300 and y= -3.1307)

    fval =

    1.1249e-008 (our objective function value, which is close to zero)

    check1 =

    -2.0000

    check2 =

    -4.9999

    if you wanted to go deeper into optimization, there are global optimization techniques such as the genetic algorithm that will find a global optimum instead of the local optimum found by fmincon.

  • ?
    Lv 4
    4 years ago

    Matlab Solve Nonlinear Equation

Still have questions? Get your answers by asking now.