Search code examples
matlabmatrixsystem

How to numerically solve a system with two matrices in Matlab?


I'm trying to numerically find the solution to A*cos x +B*sin x = C where A and B are two known square matrices of the same size (for example 100x100), and C is a known vector (100x1). Without the second term (i.e. with a single matrix), I will use Jacobi or Gauss-Seidel to solve this problem and get x but here, I don't see how to proceed to solve this problem in Matlab. May be, it would be useful to solve the problem as : A*X + B*sqrt(1-X^2) = C.

I would greatly appreciate any help, ideas or advices Thanks in advance


Solution

  • If I understood you correctly, you could use fsolve like this (c and X are vectors):

    A = ones(2,2);
    B = ones(2,2);
    c = ones(2,1);
    
    % initial point
    x0 = ones(length(A), 1);
    % call to fsolve
    sol = fsolve(@(x) A * cos(x) + B*sin(x) - c, x0);
    

    Here, we solve the nonlinear equation system F(x) = 0 with F: R^N -> R^N and F(x) = A * cos(x) + B*sin(x) - c.

    Only for the sake of completeness, here's my previous answer, i.e. how one could do it in case C and X are matrices instead of vectors:

    A = ones(2,2);
    B = ones(2,2);
    C = ones(2,2);
    
    % initial point
    x0 = ones(numel(A), 1);
    
    % call to fsolve
    fsolve(@(x) fun(x, A, B, C), x0)
    
    
    function [y] = fun(x, A, B, C)
        % Transform the input vector x into a matrix
        X = reshape(x, size(A));
        % Evaluate the matrix equation
        Y = A * cos(X) + B*sin(X) - C;
        % flatten the matrix Y to a row vector y
        y = reshape(Y, [], 1);
    end
    

    Here, the idea is to transform the matrix equation system F: R^(N x N) -> R^(N x N) into a equivalent nonlinear system F: R^(N*N) -> R^(N*N).