An error of unable to perform assignment because the size of the left side is 1-by-1-by-3 and the size of the right side is 1-by-3-by-3 when I am using the following:
11 次查看(过去 30 天)
显示 更早的评论
% Vectors and Matrices Initialization:
v1_2=zeros(360,76,3); % each element of the 360x76 matrix is a velocity vector
v2_prime_2=zeros(360,76,3);
r1=zeros(360,3);
r2_prime=zeros(360,76,3);
delta_t=zeros(360,76);
string='pro';
% Main Code:
for i=1:1:360
for j=1:1:76
% The inputs in the following function are non-zero(
% r1, r2_prime, delta_t and string). Previous calculations have been
% made inside the double for loop with these matrices and vectors, but they do not affect the error
% of this code and thus they are not mentioned.
[v1_2(i,j,:), v2_prime_2(i,j,:)] = lambert_equation(r1(i,:), r2_prime(i,j,:), delta_t(i,j), string);
end
end
%The error is probably in the calculations made for V1 and V2 of the following function:
function [V1, V2] = Lambert(R1, R2, t, str)
% This function solves Lambert's problem.
% INPUTS:
% R1 = position vector at position 1 (km)
% R2 = position vector at position 2 (km)
% t = time of flight (s)
% OUTPUTS:
% V1 = velocity vector at position 1 (km/s)
% V2 = velocity vector at position 2 (km/s)
%
% VARIABLES DESCRIPTION:
% r1, r2 - magnitudes of R1 and R2
% c12 - cross-product of R1 and R2
% theta - change in true anomaly between position 1 and 2
% z - alpha*x^2, where alpha is the reciprocal of the
% semimajor axis and x is the universal anomaly
% y(z) - a function of z
% F(z,t) - a function of the variable z and constant t
% dFdz(z) - the derivative of F(z,t)
% ratio - F/dFdz
% eps - tolerence on precision of convergence
% C(z), S(z) - Stumpff functions
%% Gravitational parameter:
global mu
%% Magnitudes of R1 and R2:
r1 = norm(R1);
r2 = norm(R2,'fro');
%% Theta:
c12 = cross(R1, squeeze(R2));
theta = acos(dot(R1,squeeze(R2))/r1/r2);
%Assume prograde or retrograde orbit:
if strcmp(str,'pro')
if c12(3) <= 0
theta = 2*pi - theta;
end
elseif strcmp(str,'retro')
if c12(3) >= 0
theta = 2*pi - theta;
end
end
%% Solve equation for z:
A = sin(theta)*sqrt(r1*r2/(1 - cos(theta)));
%Check where F(z,t) changes sign, and use that
%value of z as a starting point:
z = -100;
while F(z,t) < 0
z = z + 0.1;
end
%Tolerence and number of iterations:
eps= 1.e-8;
nmax = 5000;
%Iterate until z is found under the imposed tolerance:
ratio = 1;
n = 0;
while (abs(ratio) > eps) && (n <= nmax)
n = n + 1;
ratio = F(z,t)/dFdz(z);
z = z - ratio;
end
%Maximum number of iterations exceeded
if n >= nmax
fprintf('\n\n **Number of iterations exceeds %g \n\n ',nmax)
end
%% Lagrange coefficents:
f = 1 - y(z)/r1;
g = A*sqrt(y(z)/mu);
g_dot = 1 - y(z)/r2;
%% V1 and V2:
V1 =1/g*(R2 - f*R1);
V2 =1/g*(g_dot*R2 - R1);
return
%% Subfunctions used:
function dum = y(z)
dum = r1 + r2 + A*(z*S(z) - 1)/sqrt(C(z));
end
function dum = F(z,t)
dum = (y(z)/C(z))^1.5*S(z) + A*sqrt(y(z)) - sqrt(mu)*t;
end
function dum = dFdz(z)
if z == 0
dum = sqrt(2)/40*y(0)^1.5 + A/8*(sqrt(y(0)) + A*sqrt(1/2/y(0)));
else
dum = (y(z)/C(z))^1.5*(1/2/z*(C(z) - 3*S(z)/2/C(z)) ...
+ 3*S(z)^2/4/C(z)) + A/8*(3*S(z)/C(z)*sqrt(y(z)) ...
+ A*sqrt(C(z)/y(z)));
end
end
%Stumpff functions:
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
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
end
1 个评论
Walter Roberson
2024-3-5
We are missing lambert_equation
It is not the same as your Lambert -- your Lambert function is not designed to handle vectors.
回答(1 个)
Drishti
2024-9-3
Hi Loannis,
I reproduced the error on my end by considering ‘Lambert’ as ‘lambert_equation’. The error you're encountering is due to a discrepancy in the dimensions of the variables being assigned from the 'lambert_equation' function.
I managed to fix the error by adjusting the shape of the variables. You can refer to the implemented code:
% Main Code:
for i=1:1:360
for j=1:1:76
% The inputs in the following function are non-zero(r1, r2_prime, delta_t and string).
% Ensure r2_prime(i,j,:) is correctly reshaped to a 1-by-3 vector
r2_prime_vector = squeeze(r2_prime(i, j, :))';
[v1_2(i,j,:), v2_prime_2(i,j,:)] = lambert_equation(r1(i,:), r2_prime_vector, delta_t(i,j), string);
end
end
I hope this resolves the error.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!