Advertisement
Guest User

Untitled

a guest
May 20th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.71 KB | None | 0 0
  1. % The system of non-linear equations
  2. f1 = @(x1, x2) x1.^3 + x2.^3 - 8 * x1 * x2;
  3. f2 = @(x1, x2) x1 * log(x2) - x2 * log(x1);
  4.  
  5. % The target function
  6. Fi = @(x1, x2) f1(x1, x2).^2 + f2(x1, x2).^2;
  7.  
  8. % Accuracy
  9. epsilon = 0.001;
  10. % Maximum iterations
  11. maxits = 100;
  12. % Number of unknowns
  13. unknownsNumber = 2;
  14.  
  15. % Step for the grid
  16. shiftingStep = 1;
  17. % Grid  X Y
  18. grid = [1 1   % start
  19.         3 3]; % end
  20.  
  21. % What start point must be shifting
  22. mustShiftGridX = false;
  23.  
  24. % Stabilization for first shift condition
  25. grid(1, 2) = grid(1, 2) - shiftingStep;
  26.  
  27. fprintf(' x1     x2     x1:beg x2:beg\n');
  28.  
  29. % Break if grid so small
  30.  while grid(1, 1) < grid(2, 1) || grid(1, 2) < grid(2, 2)
  31.      
  32.       % Shifting for each dimension
  33.       if mustShiftGridX == true
  34.            grid(1, 1) = grid(1, 1) + shiftingStep;
  35.       else
  36.            grid(1, 2) = grid(1, 2) + shiftingStep;
  37.       end
  38.       mustShiftGridX = ~mustShiftGridX;
  39.      
  40.       % The start approximation
  41.       XYaprx = [grid(1, 1) grid(1, 2)];
  42.       detL = 0.5;
  43.      
  44.       X = zeros(3, 2); % The temp value like a lyambda
  45.       L = zeros(1, 3); % Lyambda
  46.       a = zeros(1, 3); % Result of system equations
  47.       F = zeros(1, 3); % Result of Fi functions
  48.       e = [1 0
  49.            0 1];
  50.        
  51.       % Break if count of iterations more than maximum
  52.       for i = 1:maxits
  53.         A = Fi(XYaprx(1), XYaprx(2));
  54.         for j = 1:unknownsNumber
  55.            
  56.             % Lyambdas and lyambda results calculations
  57.             L(2) = detL;
  58.             X(1, :) = XYaprx;
  59.             X(2, :) = XYaprx + L(2) * e(j, :);
  60.             F(1) = Fi(X(1, 1), X(1, 2));
  61.             F(2) = Fi(X(2, 1), X(2, 2));            
  62.             if F(2) < F(1)
  63.                 L(3) = L(1) + 2 * detL;
  64.             else
  65.                 L(3) = -detL;
  66.             end            
  67.             X(3, :) = XYaprx + L(3) * e(j, :);            
  68.             F(3) = Fi(X(3, 1), X(3, 2));  
  69.            
  70.             % Finding a's
  71.             a(1) = ( (F(2) - F(1)) * L(3) - (F(3) - F(1)) * L(2) ) / ( L(2)^2 * L(3) - L(2) * L(3) );
  72.                
  73.             a(2) = ( (F(3) - F(1)) * L(2)^2 - (F(2) - F(1)) * L(3)^2 ) / ( L(2)^2 * L(3) - L(2) * L(3) );
  74.                
  75.             a(3) = F(1);
  76.                        
  77.             Lmin = -a(2) / (2 * a(1));
  78.             % Make a step
  79.             XYaprx = XYaprx + Lmin * e(j, :);
  80.         end
  81.             B = Fi(XYaprx(1), XYaprx(2));
  82.             if abs(A - B) <= epsilon
  83.                 break;
  84.             else
  85.                 % Decrease the local step
  86.                 detL = detL / 2;
  87.                 j = 1;
  88.             end
  89.       end
  90.     fprintf(' %.4f %.4f %.4f %.4f \n', XYaprx(1), XYaprx(2), grid(1, 1), grid(1, 2));
  91.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement