필터 지우기
필터 지우기

Using trigonometric function in Matlab

조회 수: 2 (최근 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 CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by