# Help Plotting Symbolic Vector Fields with Substitutions Classic List Threaded 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

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
|