Using trigonometric function in Matlab

2 visualizaciones (últimos 30 días)
John Draper
John Draper el 16 de Dic. de 2015
Comentada: Star Strider el 16 de Dic. de 2015
Hi, I am trying to model a dipole magnetic field using the quiver3 function. However when I use a trigonometric function in one of my arrow direction vectors i get the error:
Error using * Inputs must be 2-D, or at least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.
Here is the code i'm using
if true
[X,Y,Z] = meshgrid(-10:.5:10, -10:.5:10, -10:.5:10); %defines meshgrid coverage
r=(X.^2+Y.^2+Z.^2).^0.5 ;
k=50;
T=Z./r;
U=3*k*Z.*sin(acos(T))*r.^-4;
V=3*k*Z.*sin(acos(T))*r.^-4;
W=2*k*((3*Z.^2 * r.^-2)-1)*r.^-3;
%magnetic field function
figure
quiver3(X,Y,Z,U,V,W,0.5)% no defines arrow length, other define direction
%defines arrow direction (u,v,w) at point (x,y,z)
hold on
%surf(X,Y,Z) %produces surface
axis equal %defines axis parameters
hold off
% code
end
Does anyone have any idea how i can rectify this? Thanks in advance, John

Respuesta aceptada

Star Strider
Star Strider el 16 de Dic. de 2015
When in doubt, vectorise everything:
U=3*k*Z.*sin(acos(T))./r.^4;
V=3*k*Z.*sin(acos(T))./r.^4;
W=2*k*((3*Z.^2 ./ r.^2)-1)./r.^3;
This produces your plot, but you may want to tweak the arrow lengths in order to see them.
  2 comentarios
John Draper
John Draper el 16 de Dic. de 2015
Thanks, It still gives me an unusual plot for a magnetic dipole but at least it works!
Star Strider
Star Strider el 16 de Dic. de 2015
My pleasure!
That’s not my current area of expertise, so I can’t otherwise help to troubleshoot your code.
I’m not certain this will improve things a great deal, but an easier meshgrid call would be:
vec = linspace(-10, 10, 75);
[X,Y,Z] = meshgrid(vec); %defines meshgrid coverage
The third argument to linspace is the number of points, so you can experiment to vary them at will. The difference between linspace and the colon operator is that linspace varies the interval betwen points to create a specified length, and the colon operator varies the length at a constant interval.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by