generating 3D vector in MATLAB

11 Ansichten (letzte 30 Tage)
Daz
Daz am 12 Dez. 2015
Bearbeitet: Stephen23 am 12 Dez. 2015
I want to write a script to generate a 3D vector field of the electric flux density of 8 different point charges in a [-2,2]x[-2,2]x[-2,2] box in 3D space.
I have a function definition in a separate .m file as follows:
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;
And then in my main script I have the function calls:
%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');
I receive the following errors when I try to run my function file:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> question3function at 11
Rx=(XX)-Loc([]);
Could somebody please help me and explain how I can fix this? I will add I am not very experienced with using MATLAB.

Antworten (1)

Stephen23
Stephen23 am 12 Dez. 2015
Bearbeitet: Stephen23 am 12 Dez. 2015
The error message tells us the error occurs on this line:
Rx=(XX)-Loc([]);
Those variables are hardcoded as:
Loc=[];
XX=(2,-2);
Presumably you have not copied your code correctly, because that XX definition is not valid MATLAB code, and will immediately cause an error:
>> XX=(2,-2);
??? XX=(2,-2);
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Once we fix that by replacing the parentheses by square brackets we can try that code out. The code Loc([]) takes no elements of an empty vector, giving an empty vector. So your code is equivalent to this:
>> [2,2] - []
??? Error using ==> minus
Matrix dimensions must agree.
What output do you expect to get when you subtract an empty vector from a 1x2 vector?
When doing matrix arithmetic either:
  • one array can be scalar, or
  • both arrays must be the same size.
This is clearly stated in the minus/subtraction documentation. You should should read it.

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by