Matlab代码报错‘Inner matrix dimensions must agree’排查求助
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/R2have mismatched row/column dimensions, - The variables
f,g, orgdotare accidentally matrices instead of scalars (as they should be in Lambert problem calculations), - There's a typo in your
V2calculation (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:
- Define
muas a scalar: This is the gravitational constant (e.g.,3.986e14m³/s² for Earth). Add it at the top:mu = 3.986e14; % Replace with your target body's gravitational constant - Fix
Adefinition: In Lambert problems,Ais a scalar related to the cross product ofR1andR2. Define it properly:A = norm(cross(R1, R2)); % Scalar magnitude of the angular momentum term - Verify helper functions return scalars: Your
S(z),C(z),F(z,t), anddFdz(z)functions must accept a scalarzand return a scalar. For example, here's the correct scalar implementation of Stumpff functions (required for Lambert calculations):
If these functions return matrices (e.g., iffunction 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 endzaccidentally becomes a matrix during iteration),y(z)will be a matrix, turningf,g, andgdotinto 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

