RobinSchmidt / GNUPlotCPP

Plotting facilities for your C++ coding environment.
The Unlicense
6 stars 1 forks source link

plotting vector fields #1

Open RobinSchmidt opened 5 years ago

RobinSchmidt commented 5 years ago

i just added some features that let us plot 2D vector fields. they look like this: image

RobinSchmidt commented 5 years ago

yay! i can now superimpose the field lines over the vector field:

image

...but the user must provide a function to generate the field lines. generating them involves solving an ODE system - which i did for this plot analytically. the vector field is generated by the complex function f(z) = z^2 which corresponds to the vector valued function f(x,y) = (x^2-y^2, 2*x*y). wolfram alpha could solve the system analytically via DSolve[ {x'[t] == x[t]^2 - y[t]^2, y'[t] == 2 x[t] y[t]}, {x, y}, t] and i implemented the resulting formulas. they are already quite messy, although it's perhaps the simplemost nonlinear example, one can think of (or...well...maybe not - only when you want to use a complex function to base the vector field on - which i want because i actually want to gain a better understanding of complex functions). i guess, a general field-line plotter will need a numerical ODE solver. ...i have some such stuff in the main RS-MET codebase but i probably don't want it here in the plotter. dunno...

RobinSchmidt commented 5 years ago

...the vector valued function f(x,y) = (x^2-y^2, 2xy). wolfram alpha could solve the system analytically

i also tried to solve for the streamlines of the so called "Polya vector field" for z^2 - this just takes the complex conjugate, so we would just have to add a minus sign: f(x,y) = (x^2-y^2, -2xy). wolfram alpha says that it doesn't understand the input. wolfram cloud can do it - but the result is a total mess!

image

...i'm certainly not going to implement these formulas! :-O

RobinSchmidt commented 5 years ago

...this wolfram cloud seems to be quite cool! https://www.wolframcloud.com/obj/robin13/Published/ZedSquaredFieldLines.nb ...these are the analytic formulas that i implemented for the field-line plot above. it's not pretty but still managable - this is actually a pair of two parametric equations for x and y in the parameter t and it was still tricky to figure out the appropriate range for the parameter t and when to switch between the formulas. ...but the polya field - nope - just nope!

RobinSchmidt commented 5 years ago

phase portrait of a damped pendulum with one trajectory drawn in: image ...this is basically the same kind of plot as a 2D vector field with one particular field-line drawn. the trajectory (pseudo field-line) was computed numerically with the simple forward euler method. i think, i'll integrate that simple solver into the plotter class. who wants to use more sophisticated solvers may have to generate the data themselves

RobinSchmidt commented 5 years ago

the trajectory (pseudo field-line) was computed numerically with the simple forward euler method. i think, i'll integrate that simple solver into the plotter class.

ok, done. for quick-and-dirty plotting applications, the forward euler solver should be good enough. this plotter is not supposed to be a sophisticated ode solver. i think, i'm happy with the feature now and added this plot to the collection of demos:

image

RobinSchmidt commented 5 years ago

i'd really like to be able to plot things like the field lines and equipotentials like at the very bottom here:

http://www.feynmanlectures.caltech.edu/II_04.html

...but i had to realize that i need a more sophisticated ode solver for such things (at the very least with adaptive stepsize control). and for the equipotentials probably an implicit equation solver. ...should i ever get around to implement these things, they'll probably go into a separate "add-on" file. maybe someday - but now, i have actual work to do :-)

RobinSchmidt commented 5 years ago

...okay...i implemented adaptive stepsize control and can now do the field lines:

image

RobinSchmidt commented 5 years ago

now with equipotentials:

image

...it almost starts to feel like creating an artwork - haha!