Removed

댓글 수: 1

Stephen23
Stephen23 2021년 4월 30일
편집: Stephen23 2021년 4월 30일
Original question by E RS retrieved from Bing Cache:
I can't solve this error
clear
G = 6.67408*10^-11;
M1 = 5.9722*10^24;
x1x = 5*10^6;
x1y = 0;
M2 = 3.3*10^24;
x2x = -12*10^6;
x2y = 0;
ft = 15000;
dt = 1;
t = 0:dt:ft;
V = zeros(1,length(t));
Vx = 0;
Vy = 7058.6;
Px = 13*10^6;
Py = 0;
r1x = 0;
r1y = 0;
r2x = 0;
r2y = 0;
while ft>=t;
r1x = Px - x1x;
r1y = Py - x1y;
r2x = Px - x2x;
r2y = Py - x2y;
r1mag = sqrt(r1x^2 + r1y^2);
r2mag = sqrt(r2x^2 + r2y^2);
ax = -G(((M1/r1mag^3)*r1x)+((M2/r2mag^3)*r2x));
ay = -G(((M1*r1y)/r1mag^3)+((M2*r2y)/r2mag^3));
Px = Px + Vx*dt;
Py = Py + Vy*dt;
Vx = Vx + ax*dt;
Vy = Vy + ay*dt;
plot(P);
end
That's my code an I keep getting the following error:
Index exceeds the number of array elements (1).
Error in AnotherTry (line 35)
ax = -G(((M1/r1mag^3)*r1x)+((M2/r2mag^3)*r2x));
How can I solve this?
Thanks.
p.s. If the V = zeros etc.. part looks odd, that's normal. I've been experimenting.

댓글을 달려면 로그인하십시오.

 채택된 답변

DGM
DGM 2021년 4월 30일

0 개 추천

This needs to be a test which returns a scalar logical, but t is a vector.
while ft>=t
G is a scalar, and you're trying to address the 98 billionth element of it.
ax = -G(((M1/r1mag^3)*r1x)+((M2/r2mag^3)*r2x));
are you sure you don't mean to be multiplying?
ax = -G*(((M1/r1mag^3)*r1x)+((M2/r2mag^3)*r2x));

댓글 수: 2

DGM
DGM 2021년 4월 30일
편집: DGM 2021년 4월 30일
These are two unrelated problems.
First, since t is a vector, so is the result of the test. The exit condition won't work correctly because of this.
size(ft>=t)
ans =
1 15001
Normally, you'd want this to be an expression which evaluates to a scalar logical output. If it's a vector, it will only exit if all 15001 elements are true.
Second, you're setting
ax = -G(index)
where said index is
((M1/r1mag^3)*r1x)+((M2/r2mag^3)*r2x)
ans =
9.8596e+10
Which makes no sense. G has only one element.
Walter Roberson
Walter Roberson 2021년 4월 30일
MATLAB has absolutely no implied multiplication. G(something) is indexing, not multiplication.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2021년 4월 30일

편집:

2021년 4월 30일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by