Using trigonometric function in Matlab
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2015-12-16
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 个评论
Star Strider
2015-12-16
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!