You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matlab中数值求解A*cosx + B*sinx = C型双矩阵非线性方程组?

Hey there! Let's work through solving this nonlinear matrix equation step by step—you're already on the right track with your initial thoughts, so let's refine them and cover actionable methods for MATLAB.

First, Clarify the Problem Form

Just to make sure we're aligned: since C is a 100×1 vector, x must also be a 100×1 vector, where cosx and sinx are element-wise operations (i.e., cosx(i) = cos(x(i)), same for sine). The full equation expands to:

For each index i, $\sum_{k=1}^{100} A_{ik}cos(x_k) + \sum_{k=1}^{100} B_{ik}sin(x_k) = C_i$

This is a system of 100 nonlinear equations with 100 variables—standard territory for numerical nonlinear solvers.

On Your X=cosx Transformation

Your idea to substitute X = cosx is mathematically valid, but it introduces a critical caveat: sinx = ±sqrt(1 - X²). For a 100-dimensional vector, this would mean $2^{100}$ possible sign combinations, which is completely infeasible to check exhaustively. This approach only works if you have prior knowledge that all sin(x_i) are positive (or all negative)—otherwise, it's not practical. Instead, let's focus on solving the original equation directly.

1. Use MATLAB's Built-in fsolve (Simplest Option)

If you have the Optimization Toolbox, fsolve is the easiest way to go—it handles the nonlinear solving logic for you. Here's how to implement it:

First, define the residual function (the difference between the left-hand side and right-hand side of your equation):

function res = nonlinear_residual(x, A, B, C)
    cos_x = cos(x);
    sin_x = sin(x);
    res = A * cos_x + B * sin_x - C;
end

Then call fsolve with an initial guess (pick something reasonable—even a zero vector works, but a better guess speeds convergence):

% Assume A, B (100x100) and C (100x1) are pre-defined
x0 = zeros(100, 1); % Initial guess
options = optimoptions('fsolve', ...
    'Display', 'iter', ... % Show iteration progress
    'FunctionTolerance', 1e-8); % Set convergence precision

x_solution = fsolve(@(x) nonlinear_residual(x, A, B, C), x0, options);

2. Manual Newton-Raphson Implementation (For Toolbox-Free Use)

If you can't use fsolve, the Newton-Raphson method is the gold standard for nonlinear systems. Here's how to code it:

The core iteration is:
x_{k+1} = x_k - J(x_k) \ F(x_k)
Where:

  • F(x) is the residual vector (A*cosx + B*sinx - C)
  • J(x) is the Jacobian matrix, where J(i,j) = -A(i,j)*sin(x(j)) + B(i,j)*cos(x(j)) (derivative of F(i) with respect to x(j))
% Initialize parameters
tol = 1e-8;
max_iter = 1000;
x = zeros(100, 1); % Initial guess
iter = 0;
res_norm = inf;

while res_norm > tol && iter < max_iter
    % Compute residual F(x)
    cos_x = cos(x);
    sin_x = sin(x);
    F = A * cos_x + B * sin_x - C;
    res_norm = norm(F);
    
    % Compute Jacobian J(x)
    J = -A .* sin(x') + B .* cos(x'); % Element-wise multiplication + matrix ops
    
    % Solve J * delta_x = -F (more stable than inverting J)
    delta_x = J \ (-F);
    
    % Update x
    x = x + delta_x;
    iter = iter + 1;
end

if iter == max_iter
    warning('Reached maximum iterations without convergence');
else
    fprintf('Converged in %d iterations\n', iter);
    x_solution = x;
end

Tips for Newton-Raphson:

  • If iterations diverge, try a smaller step size (e.g., x = x + 0.5*delta_x) or a better initial guess (like the solution to A*x + B*x = C as a linear approximation).
  • Using J \ (-F) instead of inv(J)*(-F) is faster and more numerically stable for large matrices.

Key Notes to Keep in Mind

  • Solution Existence/Uniqueness: Nonlinear systems can have 0, 1, or multiple solutions. For example, if A and B are diagonal, each equation A_ii cos(x_i) + B_ii sin(x_i) = C_i can have 0, 1, or 2 solutions—leading to thousands of possible combined solutions for 100 variables.
  • Initial Guess Matters: A good initial guess (e.g., the solution to the linearized equation A*x + B*x = C or your existing Jacobi solution for the B=0 case) will drastically improve convergence speed and reliability.
  • Convergence Checks: Always monitor the residual norm (norm(F)) to confirm the solution meets your precision needs.

内容的提问来源于stack exchange,提问作者Acosia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 20:12:29