6

I am having trouble generating a plot for a solution to my differential equations. I always end up with just blank axes.

Clear["Global`*"] 
Remove["Global`*"]
m x''[t] == -k m x'[t];
m y''[t] == -k m y'[t] - m g;
U == v Cos[\[Theta]];
V == v Sin[\[Theta]];
soln = DSolve[{m x''[t] == -k m x'[t], m y''[t] == -k m y'[t] - m g, 
  x[0] == 0, y[0] == 0, x'[0] == U, y'[0] == V}, {x[t], y[t]}, 
 t][[1]] // Simplify;
soln
ParametricPlot[{x[t], y[t]}, {t, 0, 5}]

The solution comes out fine but the graph always comes out blank. I'm sure I'm making some rookie mistake somewhere and would appreciate if someone can point it out.

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Jimmy
  • 61
  • 1
  • 2

2 Answers2

7

In order plot your solution you have to replace all parameters with explicit values. One possible way is to use Manipulate.

m x''[t] == -k m x'[t];
m y''[t] == -k m y'[t] - m g;
U == v Cos[\[Theta]];
V == v Sin[\[Theta]];
soln = DSolve[{m x''[t] == -k m x'[t], m y''[t] == -k m y'[t] - m g, 
  x[0] == 0, y[0] == 0, x'[0] == U, y'[0] == V}, {x[t], y[t]}, 
 t][[1]] // Simplify;

With[{expr = {x[t], y[t]} /. soln},
 Manipulate[
  ParametricPlot[expr, {t, 0, 5}, PlotRange -> {{0, 10}, {-5, 5}}],
  {{k, 1}, .1, 5},
  {{g, 3}, 1, 10},
  {{U, 6}, 2, 10},
  {V, 5, 10}
  ]
 ]

Mathematica graphics

halirutan
  • 112,764
  • 7
  • 263
  • 474
6
  1. I believe the main issue for you is that Mathematica does not know what x[t] and y[t] are in your ParametricPlot command. An excellent way to solve this is by using ReplaceAll (a.k.a. /.) with the Rules already included in the solution produced by DSolve.
  2. Ensure that your parameters (e.g. U and V) are assigned values with Set (=); do not use Equal (==) here, which is an operator used to define equations and do logical comparisons of two expressions.
  3. Assign values (using Set) to all of your parameters, as mentioned by user halirutan.

I propose the following improved code:

Clear["Global`*"]
m = k = v = θ = g = 1;
m x''[t] == -k m x'[t];
m y''[t] == -k m y'[t] - m g;
U = v Cos[θ];
V = v Sin[θ];
soln = DSolve[{m x''[t] == -k m x'[t], m y''[t] == -k m y'[t] - m g, 
x[0] == 0, y[0] == 0, x'[0] == U, y'[0] == V}, {x[t], y[t]}, t][[1]];
ParametricPlot[{x[t], y[t]} /. soln, {t, 0, 3}]

enter image description here

As a further example, I've included a direction field and a parametric plot of a specific solution for a different, first-order differential equation. The specific solution corresponds to a single value (in this case C[1] = 0) for the constant of integration which is in the general solution.

soln=DSolve[y'[x]==(x^2)/(1-y[x]^2),y[x],x];
plotone=ParametricPlot[{x,y[x]/.soln[[1]]/.C[1]->0},{x,-10,10}, PlotStyle->{Red, Thickness[0.01]}];
plottwo=StreamPlot[{(1 - y^2),x^2},{x,-10,10}, {y,-10,10}, VectorScale->.2, StreamStyle-> Blue];
Show[plottwo,plotone]

enter image description here

rm -rf
  • 88,781
  • 21
  • 293
  • 472
TransferOrbit
  • 3,547
  • 13
  • 26