How to plot a Vector Field from symbolic Matrix?

This code will be used to help me study Maxwell's equations using vector fields presented in Cylindrical Coordinates. I am a lowly Electronics Technician reviewing physics, so that I may one day have a shot at completing Electrical Engineering Curriculum when I return back to Engineering School one day. I have consulted with 3 of the best engineers that use MatLab from my work place, and none of them have experience with the symbolic package. I have tinkered more around this code over the Easter weekend, but I haven't presented any of my modified variants.
INPUT: "cyl" - A vector in cylindrical coordinates.
"A Cylindrical-to-Cartesian transformation matrix is specified called "trans".
OUTPUT: "rec" - rectangular coordinate obtained by matrix multiplication.
So far, it LOOKS like the matrix obtained is correct, but something is happening. A 2D plot is made, but the following error message is given:
error: set: invalid number of arguments error: called from _quiver_ at line 301 column 7 quiver3 at line 83 column 10
It seems to me like I am using the correct number of arguments, so I don't understand why there is a problem.
_____________MY CODE:_______________
clear
syms x y z phi(x,y) r(x,y,z) u v w trans cyl rec
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];
rec = trans * cyl;
u = rec(1,1);
v = rec(2,1);
w = rec(3,1);
[x,y,z] = meshgrid([-1:.2:1]);
figure
quiver3(x,y,z,u,v,w)

3 个评论

Here is another Example that WORKS:
clear
pkg load symbolic;
syms x y z phi(x,y) r(x,y,z) u v w trans cyl rec
[x,y] = meshgrid([-1:.2:1]);
u = sin(pi*y/2);
v = -sin(pi*x/2);
#rec = [sin(pi*y/2), -sin(pi*x/2)]
#u = rec(1,1);
#v = rec(1,2);
#w = rec(3,1);
figure
quiver(x,y,u,v);
VS. SAME EXAMPLE THAT DOES NOT WORK:
clear
pkg load symbolic;
syms x y z phi(x,y) r(x,y,z) u v w trans cyl rec
[x,y] = meshgrid([-1:.2:1]);
#u = sin(pi*y/2);
#v = -sin(pi*x/2);
rec = [sin(pi*y/2), -sin(pi*x/2)]
u = rec(1,1);
v = rec(1,2);
#w = rec(3,1);
figure
quiver(x,y,u,v);
I have found the answer. 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))
Nicely done! Just for curiosity, what's with the hash marks? Is that the comment symbol on your machine? Normally it's a percent sign.

请先登录,再进行评论。

回答(1 个)

"pkg load" and use of # for comments tells us that you are using Octave rather than MATLAB.

2 个评论

I guess there isn't an Octave Answers!
I chose Octave as my weapon of choice for the high compatibility with MatLab, and having a better chance of finding documentation and help in the event that I get utterly stuck. If you guys like I can edit my code to reflect a MatLab compatible Script. I also have a Spherical Coordinate version, that takes in a spherical Vector Field, and then plots a Vector Field. I figure such an operation would be useful to allot of Physics and Engineering students. Would you guys like me to post this as well?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Geology 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by