How do I create an arbitrary function that can be referenced in another script correctly?

19 views (last 30 days)
I am trying to create an E-field of a sphere with a known radius and potential that has been induced on it. Here is the code I am trying to run:
function Epar = E_field(V,R_s)
syms x y z r
epnaut = 8.854187*(10^-12);
k = 1/(4*pi*epnaut);
Q = (V*R_s)/k;
%q_eff = 1*10^-9;
r = [x, y, z];
E_x = (Q*x)/(4*pi*epnaut*(x^2+y^2+z^2)^(3/2));
E_y = (Q*y)/(4*pi*epnaut*(x^2+y^2+z^2)^(3/2));
E_z = (Q*z)/(4*pi*epnaut*(x^2+y^2+z^2)^(3/2));
Ex = matlabFunction(E_x(1));
Ey = matlabFunction(E_y(2));
Ez = matlabFunction(E_z(3));
[X,Y,Z] = meshgrid(0:0.5:45,0:0.5:45,0:0.5:40);
uintrp = interpolateSolution(results,X,Y,Z,[1,2,3]);
sol1 = reshape(uintrp(:,1),size(X));
sol2 = reshape(uintrp(:,2),size(Y));
sol3 = reshape(uintrp(:,3),size(Z));
quiver3(X,Y,Z,Ex,Ey,Ez)
axis equal
xlabel 'x'
ylabel 'y'
zlabel 'z'
title('E-field Solution')
end
First error message:
Not enough input arguments.
Error in E_field (line 5)
Q = (V*R_s)/k;
Why does this happen if I defined V and R_s in my work space prior to running the script?
Second error message:
Index exceeds array bounds.
Error in sym/subsref (line 859)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in E_field (line 7)
Ey = matlabFunction(E(2));
I tried learning how to use the matlabFunction in previous code and read the online examples and guide, but I am still unable to understand what exactly it is doing and how to use it properly. In the end I want to be able to plot this solution in a quiver3 and then reference the solution in another script file when calling on the E-field.
  8 Comments
Tom Keaton
Tom Keaton on 22 Jul 2018
Sorry making mistakes all over. I meant to say this:
results = [Ex,Ey,Ez]
And thanks for pointing out the scientific format. That was also changed. The "r" defined object is actually not being used and I meant to note it out. I actually figured out that a lot of what was originally shown here is meaningless and I think I am plotting this field correctly now that I fixed most of it. I was just trying to do too many things at the same time. Here is the latest code:
function [Ex,Ey,Ez] = E_field(V,R_s)
epnaut = 8.854187E-12;
k = 1/(4*pi*epnaut);
Q = (V*R_s)/k;
[X,Y,Z] = meshgrid(-1:.5:1,-1:.5:1,-1:.5:1);
Ex = (X.*Q)./(4*pi*epnaut*(X.^2+Y.^2+Z.^2).^(3/2));
Ey = (Y.*Q)./(4*pi*epnaut*(X.^2+Y.^2+Z.^2).^(3/2));
Ez = (Z.*Q)./(4*pi*epnaut*(X.^2+Y.^2+Z.^2).^(3/2));
%Everything below this line is just for visual confirmation that the
%E-field looks correct
quiver3(X,Y,Z,Ex,Ey,Ez)
axis equal
xlabel 'x'
ylabel 'y'
zlabel 'z'
title('E-field Solution')
end
However, what I want to do now is make a new question on the forums that contains more context and includes the main script file that calls on this since I feel that this question was asked with too little information available.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!