Removed

1 commentaire

Stephen23
Stephen23 le 30 Avr 2021
Modifié(e) : Stephen23 le 30 Avr 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.

Connectez-vous pour commenter.

 Réponse acceptée

DGM
DGM le 30 Avr 2021

0 votes

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 commentaires

DGM
DGM le 30 Avr 2021
Modifié(e) : DGM le 30 Avr 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 le 30 Avr 2021
MATLAB has absolutely no implied multiplication. G(something) is indexing, not multiplication.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by