Using trigonometric function in Matlab

1 回表示 (過去 30 日間)
John Draper
John Draper 2015 年 12 月 16 日
コメント済み: Star Strider 2015 年 12 月 16 日
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

採用された回答

Star Strider
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 件のコメント
John Draper
John Draper 2015 年 12 月 16 日
Thanks, It still gives me an unusual plot for a magnetic dipole but at least it works!
Star Strider
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 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by