# Help Plotting Symbolic Vector Fields with Substitutions

11 messages
Open this post in threaded view
|

## Help Plotting Symbolic Vector Fields with Substitutions

 https://www.mathworks.com/help/symbolic/create-plots.htmlI am attempting a quiver plot of symbolic functions starting with the following example from MatLab.  It appears the substitution gets hung up some reason. syms x y u = sin(x^2 + y^2); v = cos(x*y); [X, Y] = meshgrid(-1:.1:1,-1:.1:1); U = subs(u, [x y], {X,Y}); V = subs(v, [x y], {X,Y}); quiver(X, Y, U, V) I get the following error messages: warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: called from     sym at line 221 column 7     numeric_array_to_sym at line 36 column 14     sym at line 199 column 7     cell_array_to_sym at line 16 column 11     sym at line 195 column 7     subs at line 126 column 7 warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?) warning: Using rat() heuristics for double-precision input (is this what you wanted?)
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 On Mon, Apr 17, 2017 at 16:13:04 -0700, WARDEVIL_UFO wrote: > https://www.mathworks.com/help/symbolic/create-plots.html>   > > > I am attempting a quiver plot of symbolic functions starting with the > following example from MatLab.  It appears the substitution gets hung up > some reason. There are many shortcomings with the subs function. Quoting from the end of "help @sym/subs":      Note: There are many possibilities that we don’t support (FIXME) if      you start mixing scalars and matrices.  We support one simple case      of subbing a matrix in for a scalar in a scalar expression:           f = sin(x);           g = subs(f, x, [1 2; 3 4])             ⇒ g = (sym 2×2 matrix)                 ⎡sin(1)  sin(2)⎤                 ⎢              ⎥                 ⎣sin(3)  sin(4)⎦      If you want to extend support to more cases, a good place to start,      as of July 2014, is the Sympy Issue #2962      [https://github.com/sympy/sympy/issues/2962]. This is probably an area where the symbolic package maintainer (and maybe even the SymPy folks) could use some help. But it's probably not going to be able to do this kind of operation as it stands today. > syms x y > u = sin(x^2 + y^2); > v = cos(x*y); > [X, Y] = meshgrid(-1:.1:1,-1:.1:1); > U = subs(u, [x y], {X,Y}); > V = subs(v, [x y], {X,Y}); > quiver(X, Y, U, V) I can sort of work around this with the following     U = arrayfun (@ (a, b) double (subs (u, [x, y], {a, b})), X, Y);     V = arrayfun (@ (a, b) double (subs (v, [x, y], {a, b})), X, Y); Note that this is extremely slow because it is performing a symbolic substitution for every pairwise X and Y, casting the result to a numeric value, and collecting the results into the resulting arrays. But the end result looks correct. -- mike _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 Hi Mike, and everyone!  After some more hours of digging, and putting puzzle pieces together, one by one, I was able to  construct another way!It looks like the key to converting a symbolic function to a form that is usable by quiver3 is by using function_handle() or equivalently, matlabFunction().  My corrected code is below: clear syms x y z phi = atan2(y,x); r = sqrt(x.^2+y.^2); trans = [cos(phi), -sin(phi), 0; sin(phi), cos(phi), 0; phi.*0, 0, 1]; cyl = [z ./ r;r; r.^2.*z]; #cyl = [1 ./ r;0; 0]; rec = trans * cyl; u = matlabFunction(rec(1,1)); v = matlabFunction(rec(2,1)); w = matlabFunction(rec(3,1)); [X,Y,Z] = meshgrid([-1:.5:1]); figure quiver3(X,Y,Z,u(X,Y,Z),v(X,Y,Z),w(X,Y,Z))
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 http://https://octave-online.net/When I run the above code on my local installation of Octave it works just fine.  But some reason its quirky with Octave Online.  Particularly, when one of the components of "cyl" does not contain a z, an r, or a phi.  Tricking the vector component into being zero, by something like Multiplying by 0*r doesn't seem to work with Octave Online either.  Anyone might know why that is?
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 In reply to this post by WARDEVIL_UFO On 18/04/17 06:23 PM, WARDEVIL_UFO wrote: > Hi Mike, and everyone!  After some more hours of digging, and putting puzzle > pieces together, one by one, I was able to  construct another way!It looks > like the key to converting a symbolic function to a form that is usable by > quiver3 is by using function_handle() or equivalently, matlabFunction().  My > corrected code is below: Nice!  "function_handle(...)" is indeed the preferred way to numerically evaluate a symbolic expression for a large number of inputs. I think it would be good if the documentation of "subs" pointed to "function_handle" to make this more discoverable. I filed a bug to do that: https://github.com/cbm755/octsympy/issues/779.   Since this is fresh on your mind, perhaps you could suggest a short paragraph for "help subs" perhaps with a simple example? Another option: we could overload @sym/quiver3. thanks, Colin _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 In reply to this post by WARDEVIL_UFO On 18/04/17 08:09 PM, WARDEVIL_UFO wrote: > http://https://octave-online.net/  > > When I run the above code on my local installation of Octave it works just > fine.  But some reason its quirky with Octave Online.  Particularly, when > one of the components of "cyl" does not contain a z, an r, or a phi. > Tricking the vector component into being zero, by something like Multiplying > by 0*r doesn't seem to work with Octave Online either.  Anyone might know > why that is? Can you give an example of exactly what fails and what the error is? That system seems to have an older version of the symbolic package so maybe that is related? Or maybe its https://github.com/cbm755/octsympy/issues/8, but I'm just guessing without knowing more detail. Please find a "minimal nonworking example" and share it. thanks, Colin _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 In reply to this post by Colin Macdonald-2 I have had trouble posting on Nabble's Octave Forum the past 2 days.  I had this response written Wednesday, but haven't had any luck until now. Colin Macdonald-2 wrote I filed a bug to do that: https://github.com/cbm755/octsympy/issues/779.   Since this is fresh on your mind, perhaps you could suggest a short paragraph for "help subs" perhaps with a simple example? Another option: we could overload @sym/quiver3. thanks, Colin I am new to Matlab/Octave.  I don't know what overloading is.  Also, to date I have never successfully called subs in Octave.  The code you guys saw was my first attempt to do just that.  I know next to nothing about the approved applications for subs.  In the above case I attempted copying code I saw from a MatLab help example.  As far as a simple example, I would be honored to help!!!! The following code will produce the same vector field plot as Figure 1.14 from Example 1.6 (pg. 39) from A Student's Guide to Maxwell's Equations by Dr. Daniel Fleisch.  Beware that this code is somewhat custom tailored to this particular example. ________________________________________________________________________________________________ pkg load symbolic; syms x y vector = [sin(pi*y ./ 2); -sin(pi*x ./ 2)]; iComponent = function_handle(vector(1,1)); jComponent = function_handle(vector(2,1)); [X,Y] = meshgrid([-.5:.05:.5]); figure quiver(X,Y,iComponent(Y),jComponent(X)) ________________________________________________________________________________________________ After I tackled this example, I went on to write a routine that will take in a Vector Field in cylindrical coordinates and plot the field (my previous post), and another routine that does the same for spherical coordinates.  For reasons I do not yet understand, my spherical and cylindrical routines seem to be immune to the following  2 quirks that happen when I attempt to extend the above simpler example to certain cases. (1) Using a floating point numbers inside my vector (2)  Calling quiver like" quiver(X,Y,iComponent(X,Y),jComponent(X,Y)) when iComponent does not include variable 'x' somewhere, and jComponent does not include variable 'y' somewhere. The spherical and cylindrical routines are more flexible in their current form.  Perhaps if I mimic some of the steps done in my cylindrical and spherical examples for the pure cartesian case, the same flexibility just might   be extended to this example.  However, doing so might convolute the basic illustration of how to use function_handle() to convert symbolic expressions from within a matrix to a form usable by quiver or quiver3. I will reply later on the Octave Online issue in another post.
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 On 21/04/17 12:38 AM, WARDEVIL_UFO wrote: > iComponent = function_handle(vector(1,1)); > jComponent = function_handle(vector(2,1)); Perhaps these should be: iComponent = function_handle (vector(1), 'vars', [x y]); (otherwise, you may get different behaviour if for example `vector(1)` depends only on y). Colin _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave
Open this post in threaded view
|

## Re: Help Plotting Symbolic Vector Fields with Substitutions

 In reply to this post by WARDEVIL_UFO On 21/04/17 12:38 AM, WARDEVIL_UFO wrote: > I have had trouble posting on Nabble's Octave Forum the past 2 days.  I had > this response written Wednesday, but haven't had any luck until now. > > > Colin Macdonald-2 wrote >> I filed a bug to do that: https://github.com/cbm755/octsympy/issues/779. >>    Since this is fresh on your mind, perhaps you could suggest a short >> paragraph for "help subs" perhaps with a simple example? >> >> Another option: we could overload @sym/quiver3. >> >> thanks, >> Colin > > I am new to Matlab/Octave.  I don't know what overloading is.  Also, to date > I have never successfully called subs in Octave.  The code you guys saw was > my first attempt to do just that.  I know next to nothing about the approved > applications for subs.  In the above case I attempted copying code I saw > from a MatLab help example.  As far as a simple example, I would be honored > to help!!!! Cool, maybe we can take that discussion to https://github.com/cbm755/octsympy/issues/779re, your example below, my first thought was a scalar or 1D example---but I'm not sure.  It is nice to show at least partial output (our examples are also part of our test suite). You could try editing `/some/path/to/octave_packages/@sym/subs.m` directly and see how it looks/reads? > The following code will produce the same vector field plot as Figure 1.14 > from Example 1.6 (pg. 39) from A Student's Guide to Maxwell's Equations by > Dr. Daniel Fleisch.  Beware that this code is somewhat custom tailored to > this particular example. > ________________________________________________________________________________________________ > pkg load symbolic; > > syms x y > > vector = [sin(pi*y ./ 2); -sin(pi*x ./ 2)]; > > iComponent = function_handle(vector(1,1)); > jComponent = function_handle(vector(2,1)); > > [X,Y] = meshgrid([-.5:.05:.5]); > > figure > > quiver(X,Y,iComponent(Y),jComponent(X)) > ________________________________________________________________________________________________ This example (with fixes per my previous email) would be very good for the wiki. http://wiki.octave.org/Symbolic_packageCould you post it there?  Maybe after the first example in "I'm trying to substitute a double value into an expression, how can I avoid the rats warning?". Colin _______________________________________________ Help-octave mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/help-octave