matlabequation-solvingfsolve

fsolve with multiple lineare equations


Please can someone explain me what is wrong with my code. I try to solve a system of multiple equation with fsolve.

`clear
 clc
 close all

 Modellpar.Sp11 = 0.025;
 Modellpar.Sp12 = 0.8;
 Modellpar.Sp21 = 0.04;
 Modellpar.Sp22 = 0.96;
 Modellpar.Sp31 = 0.04;
 Modellpar.Sp32 = 0.96;

 Sp11 = Modellpar.Sp11;
 Sp12 = Modellpar.Sp12;
 Sp21 = Modellpar.Sp21;
 Sp22 = Modellpar.Sp22;
 Sp31 = Modellpar.Sp31;
 Sp32 = Modellpar.Sp32;

 func = @(X)[(0.5) + X(11) + X(7) - X(1);
             (0.5) + X(12) + X(8) - X(2); 
             (1) - X (5) - X(3);
             (2) - X (6) - X(4);
             (5) - X (11) - X(13);
             (6) - X (12) - X(14); 
             (3) - X (9) - X(7);
             (4) - X (10) - X(8);
             (3) - (Sp11 * X (1));
             (4) - (Sp12 * X (2));
             (11) - (Sp21 * X (5));
             (12) - (Sp22 * X (6)); 
             (9) - (Sp31 * X (3));
             (10) - (Sp32 * X (4))];

 initial = [0;0;0;0;0;0;0;0;0;0;0;0;0;0];
 solns = fsolve(func, initial,  optimoptions('fsolve', 'Display', 'off'));`

I got the error :

Error in Script_ProjektWS (line 120)

solns = fsolve(func, initial, optimoptions('fsolve', 'Display', 'off'));

Caused by: Failure in initial objective function evaluation. FSOLVE cannot continue.


Solution

  • Problem seems to work fine form me:

    clear
    clc
    close all
    
    Sp11 = 0.025;
    Sp12 = 0.8;
    Sp21 = 0.04;
    Sp22 = 0.96;
    Sp31 = 0.04;
    Sp32 = 0.96;
    
    
    func = @(X)[(0.5) + X(11) + X(7) - X(1);
             (0.5) + X(12) + X(8) - X(2); 
             (1) - X(5) - X(3);
             (2) - X(6) - X(4);
             (5) - X(11) - X(13);
             (6) - X(12) - X(14); 
             (3) - X(9) - X(7);
             (4) - X(10) - X(8);
             (3) - (Sp11 * X(1));
             (4) - (Sp12 * X(2));
             (11) - (Sp21 * X(5));
             (12) - (Sp22 * X(6)); 
             (9) - (Sp31 * X(3));
             (10) - (Sp32 * X(4))];
    
    initial = [0;0;0;0;0;0;0;0;0;0;0;0;0;0];
    solns = fsolve(func, initial,  optimoptions('fsolve', 'Display', 'off'))
    

    enter image description here

    Only problem I found was that in some lines of your function func definition, the indice is separated with a space from the X variable, as in

    (6) - X (12) - X(14); 
    

    This actually creates a vector with more elements than what is intended. I corrected these spaces and the code worked fine.