Question has been removed

Removed

1 Comment

Stephen23
Stephen23 on 30 Apr 2021
Edited: Stephen23 on 30 Apr 2021
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.

Sign in to comment.

 Accepted Answer

DGM
DGM on 30 Apr 2021
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 Comments

DGM
DGM on 30 Apr 2021
Edited: DGM on 30 Apr 2021
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.
MATLAB has absolutely no implied multiplication. G(something) is indexing, not multiplication.

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Asked:

on 30 Apr 2021

Edited:

on 30 Apr 2021

Community Treasure Hunt

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

Start Hunting!