在MATLAB上的3D矢量场

时间:2015-12-12 13:45:53

标签: matlab function vector

我想编写一个脚本,在3D中的[-2,2] x [-2,2] x [-2,2]框中生成8个不同点电荷的电通量密度的3D矢量场空间。

我在单独的.m文件中有一个函数定义,如下所示:

function[Dx,Dy,Dz]= question3function(Q,Loc,XX,YY,ZZ)

Q=1e-6;
Loc=[];

XX=(2,-2);
YY=[2,-2];
ZZ=[2,-2];

% Position vector from the point charge
Rx=(XX)-Loc([]);
Ry=(YY)-Loc([]);
Rz=(ZZ)-Loc([]);

% Distance between position in interest and the point charge
R=sqrt(Rx.*Rx+Ry.*Ry+Rz.*Rz);

% Unit Position vector
Ax=Rx./R;
Ay=Ry./R;
Az=Rz./R;

% Electric flux density XYZ components
K=Q./(4*pi*R.^2);
Dx=K.*Ax;
Dy=K.*Ay;
Dz=K.*Az;

然后在我的主脚本中我有函数调用:

%function calls
[Dx1,Dy1,Dz1]=question3function(Q,[1 1 1],XX,YY,ZZ);
[Dx2,Dy2,Dz2]=question3function(Q,[1 1 -1],XX,YY,ZZ);
[Dx3,Dy3,Dz3]=question3function(Q,[1 -1 1],XX,YY,ZZ);
[Dx4,Dy4,Dz4]=question3function(-Q,[1 -1 -1],XX,YY,ZZ);
[Dx5,Dy5,Dz5]=question3function(2*Q,[-1 1 1],XX,YY,ZZ);
[Dx6,Dy6,Dz6]=question3function(-2*Q,[-1 1 -1],XX,YY,ZZ);
[Dx7,Dy7,Dz7]=question3function(-Q,[-1 -1 1],XX,YY,ZZ);
[Dx8,Dy8,Dz8]=question3function(-Q,[-1 -1 1],XX,YY,ZZ);

Dx=Dx1+Dx2+Dx3+Dx4+Dx5+Dx6+Dx7+Dx8;
Dy=Dy1+Dy2+Dy3+Dy4+Dy5+Dy6+Dy7+Dy8;
Dz=Dz1+Dz2+Dz3+Dz4+Dz5+Dz6+Dz7+Dz8;

quiver3(XX,YY,ZZ,Dx,Dy,Dz);
axis square equal;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Electric Flux Density of the sum of 8 Point Charges');

当我尝试运行我的函数文件时收到以下错误:

??? Error using ==> minus
Matrix dimensions must agree.

Error in ==> question3function at 11
    Rx=(XX)-Loc([]);

请有人帮助我并解释我如何解决这个问题?我将补充说,我对使用MATLAB不是很有经验。

2 个答案:

答案 0 :(得分:0)

你在这里做了许多毫无意义的事情:

function[Dx,Dy,Dz]= question3function(Q,Loc,XX,YY,ZZ)

Q=1e-6; % this is meaningless
Loc=[]; % this is meaningless

XX=(2,-2); % this is meaningless ()
YY=[2,-2]; % this is meaningless
ZZ=[2,-2]; % this is meaningless

% Position vector from the point charge
Rx=(XX)-Loc([]); % this is meaningless ([])
Ry=(YY)-Loc([]); % this is meaningless ([])
Rz=(ZZ)-Loc([]); % this is meaningless ([])

% Distance between position in interest and the point charge
R=sqrt(Rx.*Rx+Ry.*Ry+Rz.*Rz);

% Unit Position vector
Ax=Rx./R;
Ay=Ry./R;
Az=Rz./R;

% above why do you need all of them to be unit vectors
% you can 
% Electric flux density XYZ components
K=Q./(4*pi*R.^2);
Dx=K.*Ax;
Dy=K.*Ay;
Dz=K.*Az;

此外,在%function calls中如何定义XXYYZZ

答案 1 :(得分:0)

你的函数声明的后半部分是合理的,但你忽略了函数的工作原理。函数参数的关键是函数使用这些变量作为输入。分配给输入参数通常是语义错误:它通常不是您想要做的。另一个问题是matlab中的向量是用方括号定义的,坦白地-o E:\\img.jpg 应该给你一个关于不平衡括号的错误... 总是确保在发布有关问题之前检查你的代码它在Stack Overflow上:如果您在发布之前更改它,您可能无意中删除了原始问题。

您可以通过删除开头的多余变量重新定义,并将作业固定为(2,-2) / Rx / Ry来使用原始功能。当您说Rz时,您使用空向量进行索引,结果是一个空变量。这显然不是你需要的。代替:

Loc([])

然后,您应该在调用函数/脚本中定义function [Dx,Dy,Dz]=question3function(Q,Loc,XX,YY,ZZ) %Q, Loc, XX, ZZ, YY: input! % Position vector from the point charge Rx = XX - Loc(1); %use first component of Loc for every x Ry = YY - Loc(2); %use second component of Loc for every y Rz = ZZ - Loc(3); %use third component of Loc for every z % Distance between position in interest and the point charge R=sqrt(Rx.^2+Ry.^2+Rz.^2); % .^2 takes less characters % Unit Position vector Ax=Rx./R; Ay=Ry./R; Az=Rz./R; % Electric flux density XYZ components K=Q./(4*pi*R.^2); Dx=K.*Ax; Dy=K.*Ay; Dz=K.*Az; XXYYZZQ。此外,如果您只对总磁通密度感兴趣,可以通过定义每个电荷及其在数组中的位置来节省一些键盘时间,并在循环中调用您的函数:

Loc

输出:

output of quiver3