Removed

1 Kommentar

Stephen23
Stephen23 am 30 Apr. 2021
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

DGM
DGM am 30 Apr. 2021

0 Stimmen

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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2019a

Gefragt:

am 30 Apr. 2021

Bearbeitet:

am 30 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by