1
$\begingroup$

The goal is basically to create a plot of both altitude and velocity vs time . Given equations are: d^2y/dt^2=-g-b*v P(y)=P(0)*e^(-ky)

This is that I've attempted so far:

k = 1.256*10^-4;
b = 0.3266;
g = 9.8;
e = 2.71828;
inc = 0.1;
t[0] = 0;
v[0] = 0;
y[0] = 0;
P[0] = 17696;
P[y] = P[0]*e^(-k*y[t]);
y[t] = -g*y[t]*-b*y[t]*0.5*y^2;
v[t] = -g*v[t]*-b*v[t]*y[t];
a[t] = -g*-b*v[t];

i = 0;

While[
  y[i] > 0,
  P[i + 1] = P[0]*e^(-k*y[i]);
  t[i + 1] = t[i] + inc;
  y[i + 1] = v[i]*inc + y[i];
  v[i + 1] = a[i]*inc + v[i];
  a[i + 1] = -g*-b*v[i];
  i++
  ];
ptsvt = Table[{t[j], v[j]}, {j, 0, i}];
ptspt = Table[{t[j], P[j]}, {j, 0, i}];
ListLinePlot[ptsvt]
ListLinePlot[ptspt]

I'm pretty sure the main mistake is in the definition of y[t] and v[t]. I've tried several different equations and nothing seems to work. I'm not even sure if defining y[t] and v[t] is the right way to do it but I can't think of any other way. Any help would be greatly appreciated!

$\endgroup$
4
  • 1
    $\begingroup$ You set y[0]=0 and i=0 and then While[y[i]>0... So think a minute. What does that mean happens with your While loop? Think carefully about those three things. Maybe you could put a Print before and another one inside your While and another one after it is done to show you what is happening at each step. $\endgroup$
    – Bill
    Mar 20, 2021 at 23:45
  • 1
    $\begingroup$ Do you need to use a While method? Or could you use NDSolve[] ? $\endgroup$
    – Teabelly
    Mar 20, 2021 at 23:45
  • $\begingroup$ While is required $\endgroup$ Mar 21, 2021 at 0:22
  • 1
    $\begingroup$ Maybe you wanted to define a[0] = -g - b*v[0]; and a[i + 1] = -g - b*v[i]; (i.g., gravity g and magnetic accelertion b*v[i] of charged particle? Otherwise, acceleration, velocity, and position remain 0 all the time. $\endgroup$ Mar 21, 2021 at 9:27

1 Answer 1

1
$\begingroup$

I do not think you intend to declare y[t] implicitly:

y[t] = -g*y[t]*-b*y[t]*0.5*y^2;

I think this should rather read:

y[t_] = -g*y[t]*-b*y[t]*0.5*y^2;

Then by specifying:

y[0] = 0;

and in the while loop:

y[i + 1] = v[i]*inc + y[i];

y would never change. So I change this to y[0]=1; This is certainly not right together with P[0] = 17696; but at least it gets you going. Change the other initial conditions according to your problem. These changes together with the correction from Henrik (a[0] = -g - b*v[0];):

Clear[t, y, v, P, a];
k = 1.256*10^-4;
b = 0.3266;
g = 9.8;
e = 2.71828;
inc = 0.1;
t[0] = 0;
v[0] = 0;
y[0] = 1;
P[0] = 17696;
a[0] = -g - b*v[0];
P[y_] := P[0]*e^(-k*y[t]);
y[t_] := -g*y[t]*-b*y[t]*0.5*y^2;
v[t_] := -g*v[t]*-b*v[t]*y[t];
a[t_] := -g*-b*v[t];

i = 0; inc = .01;

While[y[i] > 0,
  P[i + 1] = P[0]*e^(-k*y[i]);
  t[i + 1] = t[i] + inc;
  y[i + 1] = v[i]*inc + y[i];
  v[i + 1] = a[i]*inc + v[i];
  a[i + 1] = -g*-b*v[i];
  i++];
ptsvt = Table[{t[j], v[j]}, {j, 0, i}];
ptspt = Table[{t[j], P[j]}, {j, 0, i}];
ListLinePlot[ptsvt]
ListLinePlot[ptspt]

enter image description here

Now you can try to adapt the initial conditions or if you do not succeed, explain your problem.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.