简体   繁体   中英

Matlab: Nonlinear equation solver

How do I solve these sets of equations and can matlab find a solution? I'm solving for x1,x2,x3,x4,c1,c2,c3,c4.

syms c1 c2 c3 c4 x1 x2 x3 x4;
eqn1 = c1 + c2 + c3 + c4 == 2;
eqn2 = c1*x1 + c2*x2 + c3*x3 + c4*x4 == 0; 
eqn3 = c1*x1^2 + c2*x2^2 + c3*x3^2 + c4*x4^2 == 2/3;
eqn4 = c1*x1^3 + c2*x2^3 + c3*x3^3 + c4*x4^3 == 0;
eqn5 = c1*x1^4 + c2*x2^4 + c3*x3^4 + c4*x4^4 == 2/5; 
eqn6 = c1*x1^5 + c2*x2^5 + c3*x3^5 + c4*x4^5 == 0;
eqn7 = c1*x1^6 + c2*x2^6 + c3*x3^6 + c4*x4^6 == 2/7;
eqn8 = c1*x1^7 + c2*x2^7 + c3*x3^7 + c4*x4^7 == 0;

From what I understand, matlab has fsolve, solve, and linsolve, but I'm uncertain how to use them.

You have a system of non-linear equations, so you can use fsolve to find a solution.

First of all you need to create a function, say fcn , of a variable x , where x is a vector with your initial point. The function defines an output vector, depending on the current vector x .

You have eight variables, so your vector x will consist of eight elements. Let's rename your variables in this way:

%x1 x(1)      %c1 x(5)
%x2 x(2)      %c2 x(6)
%x3 x(3)      %c3 x(7)
%x4 x(4)      %c4 x(8)

Your function will look like this:

function F = fcn(x)

    F=[x(5) +          x(6)         + x(7)         + x(8)        - 2   ;
       x(5)*x(1)    +  x(6)*x(2)    + x(7)*x(3)    + x(8)*x(4)         ;
       x(5)*x(1)^2  +  x(6)*x(2)^2  + x(7)*x(3)^2  + x(8)*x(4)^2 - 2/3 ;
       x(5)*x(1)^3  +  x(6)*x(2)^3  + x(7)*x(3)^3  + x(8)*x(4)^3       ;
       x(5)*x(1)^4  +  x(6)*x(2)^4  + x(7)*x(3)^4  + x(8)*x(4)^4 - 2/5 ;
       x(5)*x(1)^5  +  x(6)*x(2)^5  + x(7)*x(3)^5  + x(8)*x(4)^5       ;
       x(5)*x(1)^6  +  x(6)*x(2)^6  + x(7)*x(3)^6  + x(8)*x(4)^6 - 2/7 ;
       x(5)*x(1)^7  +  x(6)*x(2)^7  + x(7)*x(3)^7  + x(8)*x(4)^7
       ];
end

You can evaluate your function with some initial value of x :

x0  = [1; 1; 1; 1; 1; 1; 1; 1];

F0 = fcn(x0);

Using x0 as initial point your function returns:

F0 =

    2.0000
    4.0000
    3.3333
    4.0000
    3.6000
    4.0000
    3.7143
    4.0000

Now you can start fsolve which will try to find some vector x , such as your function returns all zeros:

[x,fval]=fsolve(@fcn, x0);

You will get something like this:

x =

    0.7224
    0.7224
   -0.1100
   -0.7589
    0.3599
    0.3599
    0.6794
    0.5768

fval =

   -0.0240
    0.0075
    0.0493
    0.0183
   -0.0126
   -0.0036
   -0.0733
   -0.0097

As you can see, the function values are really close to zeros, but you probably noticed that the optimization algorithm was stopped because of the limited count of the function evaluation steps stored in options.MaxFunEvals (by default 800 ). Another possible reason is the limited number of iterations stored in MaxIter (by default 400 ).

Redefine these value using the parameter options :

options = optimset('MaxFunEvals',2000, 'MaxIter', 1000);

[x,fval]=fsolve(@fcn, x0, options);

Now your output is much better:

x =

    0.7963
    0.7963
   -0.0049
   -0.7987
    0.2619
    0.2619
    0.9592
    0.5165
fval =

   -0.0005
   -0.0000
   -0.0050
    0.0014
    0.0208
   -0.0001
   -0.0181
   -0.0007

Just play with different parameter values, in order to achieve a tolerable precision level for your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM