Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How do I properly nicely comment this code?

1 vue (au cours des 30 derniers jours)
The Merchant
The Merchant le 28 Oct 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
My code:
v0 = 10
gamma = 0.1
for k=1:101
theta = 0.8444 * pi/4 + (k-51)*0.0001;
dt = 0.0000001;
x = 0;
y = 0;
vx = v0 * cos(theta);
vy = v0 * sin(theta);
x = x + dt * vx;
y = y + dt * vy;
while (y>0)
v = sqrt(vx*vx+vy*vy);
vx = vx - dt * gamma * vx;
vy = vy - dt * 9.81 - dt * gamma * vx;
x = x + dt * vx;
y = y + dt * vy;
end;
t(k) = theta
a(k) = x
end;
plot(a)
[vv, jv] = max(a)
t(jv) / (pi/4)
  3 commentaires
Adam
Adam le 28 Oct 2019
Modifié(e) : Adam le 28 Oct 2019
It's impossible for someone else to say. Depends entirely who needs to understand it. Some code doesn't need commenting at all - if it does it's badly written. Other code needs comments however well written it is. I would recommend using variables with meaningful names instead of hard-coded 'magic numbers' like 9.81 and 0.8444, but I suppose comments on those would work well enough. I assume the 9.81 represents gravity so personally I would define
gravity = 9.81;
somewhere at the top and use that. Normally I define such things on the line before they are first used, but obviously not if that is in the middle of a loop.
Adam Danz
Adam Danz le 15 Déc 2019
Original question by OP in case it is deleted (this user has deleted many questions after being answered).
------------------------------------------------------------------
My code:
v0 = 10
gamma = 0.1
for k=1:101
theta = 0.8444 * pi/4 + (k-51)*0.0001;
dt = 0.0000001;
x = 0;
y = 0;
vx = v0 * cos(theta);
vy = v0 * sin(theta);
x = x + dt * vx;
y = y + dt * vy;
while (y>0)
v = sqrt(vx*vx+vy*vy);
vx = vx - dt * gamma * vx;
vy = vy - dt * 9.81 - dt * gamma * vx;
x = x + dt * vx;
y = y + dt * vy;
end;
t(k) = theta
a(k) = x
end;
plot(a)
[vv, jv] = max(a)
t(jv) / (pi/4)

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by