Matlab: Nonlinear equation solver

后端 未结 1 553
孤城傲影
孤城傲影 2021-01-17 05:47

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         


        
相关标签:
1条回答
  • 2021-01-17 06:28

    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.

    0 讨论(0)
提交回复
热议问题