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

Matlab代码报错‘Inner matrix dimensions must agree’排查求助

Troubleshooting Matrix Dimension Mismatch in Your Lambert Function Code

Let's break down why you're hitting that Inner matrix dimensions must agree error and fix it step by step:

First, Identify the Core Issue

The error occurs at V1 = (R2-f*R1)/g; and V2 = (gdot*R1-R1)/g; because either:

  • Your input vectors R1/R2 have mismatched row/column dimensions,
  • The variables f, g, or gdot are accidentally matrices instead of scalars (as they should be in Lambert problem calculations),
  • There's a typo in your V2 calculation (more on that later).

Step 1: Fix Input Vector Dimensions

MATLAB is strict about row vs column vectors. If R1 is a 1x3 row vector and R2 is a 3x1 column vector, subtracting f*R1 from R2 will fail immediately.

Add this at the start of your function to force consistent column vectors:

function [V1, V2] = lambert(R1, R2, t, string)
    % Force input vectors to be column vectors
    R1 = R1(:);
    R2 = R2(:);
    r1 = norm(R1);
    r2 = norm(R2);
    % Rest of your code...

Step 2: Ensure Scalar Values for A, mu, and Helper Functions

Your code is missing critical definitions that are likely causing non-scalar outputs:

  1. Define mu as a scalar: This is the gravitational constant (e.g., 3.986e14 m³/s² for Earth). Add it at the top:
    mu = 3.986e14; % Replace with your target body's gravitational constant
    
  2. Fix A definition: In Lambert problems, A is a scalar related to the cross product of R1 and R2. Define it properly:
    A = norm(cross(R1, R2)); % Scalar magnitude of the angular momentum term
    
  3. Verify helper functions return scalars: Your S(z), C(z), F(z,t), and dFdz(z) functions must accept a scalar z and return a scalar. For example, here's the correct scalar implementation of Stumpff functions (required for Lambert calculations):
    function s = S(z)
        if z > 0
            s = (sqrt(z) - sin(sqrt(z)))/(sqrt(z))^3;
        elseif z < 0
            s = (sinh(sqrt(-z)) - sqrt(-z))/(sqrt(-z))^3;
        else
            s = 1/6;
        end
    end
    
    function c = C(z)
        if z > 0
            c = (1 - cos(sqrt(z)))/z;
        elseif z < 0
            c = (cosh(sqrt(-z)) - 1)/(-z);
        else
            c = 1/2;
        end
    end
    
    If these functions return matrices (e.g., if z accidentally becomes a matrix during iteration), y(z) will be a matrix, turning f, g, and gdot into matrices too—causing the dimension mismatch.

Step 3: Fix the Typo in V2 Calculation

Your current V2 line has a clear mistake:

V2 = (gdot*R1-R1)/g;

This should use R2 instead of the second R1 (per standard Lambert velocity formulas):

V2 = (gdot*R2 - R1)/g;

Even if dimensions worked, the original line would produce incorrect velocity results.

Step 4: Validate Iteration Logic

Double-check that z remains a scalar throughout your loops. You initialize it as -100 (a scalar), so as long as F(z,t) and dFdz(z) return scalars, z will stay a scalar—keeping y(z), f, g, and gdot as scalars.

Final Check

Test with a simple, known Lambert problem case to verify:

% Example input: Circular orbit transfer
R1 = [7000e3; 0; 0];
R2 = [0; 7000e3; 0];
t = 5000; % Transfer time in seconds
[V1, V2] = lambert(R1, R2, t, '');

This should resolve the dimension mismatch error and produce valid velocity vectors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:12:47