2
$\begingroup$

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).

Parametric

However, I want to be able to plot multiple graphs on the same plot with k = 0, 0.01, 0.05... Something like this Intended

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]
$\endgroup$

1 Answer 1

1
$\begingroup$

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]

enter image description here

$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.