Mathematica Asked by Choop on July 22, 2021
I wrote a working code that plots what I need using DSolve and parametric plot. Here’s an example of what it plots (and code is at the bottom).
However, I want to be able to plot multiple graphs on the same plot with k = 0, 0.01, 0.05… Something like this
I’m not sure where to vary the code without changing it too much so I can do this.
Original Code
(* Constants *)
g = 9.8;
(* Differential Equation *)
xcomp := x''[t] == -k x'[t];
ycomp := y''[t] == -k y'[t] - g;
diff := {xcomp, ycomp}
(* Initial Conditions *)
v0 = 600; [Theta] = 60 Degree; k = 0.05;
initcond = {x[0] == 0, x'[0] == v0 Cos[[Theta]], y[0] == 0,
y'[0] == v0 Sin[[Theta]]}
(* Solve *)
eqn := Append[diff, initcond];
s = DSolve[eqn, {x[t], y[t]}, t] // Simplify
y[t_] = y[t] /. s[[1]]
(* Time of Flight *)
tof = Solve[y[t] == 0, t]; // Quiet
T = t /. tof[[2]]
(* Plot *)
ParametricPlot[{x[t], y[t]} /. s, {t, 0, T}, PlotRange -> All]
Here is a quick and dirty adaptation of your code for different k values:
(* different k values *)
ks = {0.001, 0.025, 0.05, 0.075};
(* constants*)
sol = (
Clear["Global`*"];
k = #;
g = 9.8;
(*Differential Equation*)
xcomp := x''[t] == -k x'[t];
ycomp := y''[t] == -k y'[t] - g;
diff := {xcomp, ycomp};
(*Initial Conditions*)
v0 = 600; [Theta] = 60 Degree;
initcond = {x[0] == 0, x'[0] == v0 Cos[[Theta]], y[0] == 0,
y'[0] == v0 Sin[[Theta]]};
(*Solve*)
eqn := Append[diff, initcond];
s = DSolve[eqn, {x[t], y[t]}, t] // Simplify;
x[t_] = x[t] /. s[[1]];
y[t_] = y[t] /. s[[1]];
(*Time of Flight*)
tof = Solve[y[t] == 0, t]; // Quiet;
T = t /. tof[[2]];
{x[t], y[t], T, k}
) & /@ ks;
Show[ParametricPlot[{#[[1]], #[[2]]} /. s, {t, 0, #[[3]]},
PlotRange -> All, PlotLabels -> Placed[#[[4]], Above]] & /@ sol]
Answered by Daniel Huber on July 22, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP