Author Topic: Mathematica Code for Plotting Vector Fields and Trajectories  (Read 2420 times)

0 Members and 1 Guest are viewing this topic.

Offline Recon

  • Serf
  • *
  • Posts: 46
  • Cookies: 23
  • Arguing with Computer
    • View Profile
Mathematica Code for Plotting Vector Fields and Trajectories
« on: January 03, 2014, 11:29:29 pm »
Just thought I'd share a little tool I threw together while doing my Calculus homework. It plots vector fields and trajectories within them, as well as estimating the end point of the trajectory. I know hardly anyone uses Mathematica except actual mathematicians and college students, but perhaps someone will find it useful. As a side note, I should add that this was programmed in a web-based variant of Mathematica. I expect that it should be compatible with the full software or other web-based versions, but I can't promise anything.


Code: [Select]
(* VECTOR FIELD GRAPHING TOOL 1.0 *)
(* by Recon *)

(* This tool plots any or all of the following: vector fields, trajectories, and starting points for those trajectories. It is also capable of estimating the end point of the plotted trajectory.*)


(* Setup *)

Clear[xEqn, yEqn, xInit, yInit, x, y, a, b, m, n, steps, pointSize, pointColor, trajectoryThickness, trajectoryColor, vectorColor, vectorScale, xMin, xMax, yMin, yMax, approxSolutions, trajectoryPlot, starterPlot, xStep, yStep, fieldPlot];


(* Inputs *)

{a, b} = {0, 0};
steps = 200;
m[x_, y_] =
n[x_, y_] =


(* Display Options *)

pointSize = 0.03;
pointColor = Red;
trajectoryThickness = 0.01;
trajectoryColor = Red;
vectorColor = Red;
vectorScale = 0.5;
xMin = -10;
xMax = 10;
xStep = 1;
yMin = -10;
yMax = 10;
yStep = 1;


(* Program Logic *)

xEqn = x'[t] == m[x[t], y[t]];
yEqn = y'[t] == n[x[t], y[t]];
xInit = x[0] == a;
yInit = y[0] == b;
approxSolutions = NDSolve[{xEqn, yEqn, xInit, yInit}, {x[t], y[t]}, {t, 0, steps}];
trajectory[t_] = {x[t] /. approxSolutions[[1]], y[t] /. approxSolutions[[1]]};
trajectoryPlot = ParametricPlot[trajectory[t], {t, 0, steps}, PlotStyle -> {{trajectoryColor, Thickness[trajectoryThickness]}}];
starterPlot = Graphics[{pointColor, PointSize[pointSize], Point[{a, b}]}];
fieldPlot = Table[Vector[{m[x, y], n[x, y]} vectorScale, Tail -> {x, y}], {x, xMin, xMax, xStep}, {y, yMin, yMax, yStep}];


(* Output *)

Show[trajectoryPlot, starterPlot, fieldPlot, Axes -> True, AxesLabel -> {"x", "y"}, AxesOrigin -> {0, 0}, PlotRange -> {{xMin, xMax}, {yMin, yMax}}]
trajectory[steps]
« Last Edit: January 04, 2014, 12:04:59 am by Recon »

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Mathematica Code for Plotting Vector Fields and Trajectories
« Reply #1 on: January 23, 2014, 09:29:57 am »
Eh this code is in which language? in my eyes it only looks like these are library functions slammed together.
~Factionwars

Offline Recon

  • Serf
  • *
  • Posts: 46
  • Cookies: 23
  • Arguing with Computer
    • View Profile
Re: Mathematica Code for Plotting Vector Fields and Trajectories
« Reply #2 on: January 26, 2014, 10:49:17 pm »
Eh this code is in which language? in my eyes it only looks like these are library functions slammed together.

It's input code to be parsed by a program called Mathematica, which is primarily used for higher-level mathematics, including some college courses. I make the course more bearable for myself by trying to code the best solution possible for a given problem or assignment (i.e., a re-useable tool instead of a one-off solution). I figure that if I liked Evilzone enough to stay, and I'm also the kind of person to be in a course that uses Mathematica, then perhaps there are others like me who this might be useful to that would also frequent this board.