# Euler's Method with changing acceleration

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;
v = 0;
y = 0;
P = 17696;
P[y] = P*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*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!

• You set y=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.
– Bill
Mar 20, 2021 at 23:45
• Do you need to use a While method? Or could you use NDSolve[] ? Mar 20, 2021 at 23:45
• While is required Mar 21, 2021 at 0:22
• Maybe you wanted to define a = -g - b*v; 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. Mar 21, 2021 at 9:27

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;


and in the while loop:

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

y would never change. So I change this to y=1; This is certainly not right together with P = 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 = -g - b*v;):

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;
v = 0;
y = 1;
P = 17696;
a = -g - b*v;
P[y_] := P*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*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] Now you can try to adapt the initial conditions or if you do not succeed, explain your problem.