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

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

This line:
function Epar = E_field[V,R_s]
uses square brackets to group the input arguments, so is not valid MATLAB syntax. Read the function help to know how to define a function.
"Why does this happen if I defined V and R_s in my work space prior to running the script?"
Probably because you are not calling the function with its required input arguments. That some variables might exist in some workspace is irrelevant: if they are defined as input arguments to that function then you will need to supply those input arguments. How to call function with input and output arguments is explained in the introductory tutorials:
How are you calling the function E_field ? Please show us the exact code that you use to call it with.
Thanks for pointing that out. I forgot to update the code that included that fix. There are parentheses there actually but that is odd that I still need to call a function when the inputs are known values in the workspace. That did clear up the first error.
I haven't tried calling the E_field function yet, because I initially want to get this script file to work on its own. However, I would like to know a method to use in this function file to be able to do it. If it helps to solve this issue though, the way I want to use it in another script file is to input the equations into another equation and calculate the Lorentz Force.
Lorentz Equation: F = qE + (qv X B) [Where X = Cross Product]
This will then (hopefully) calculate the force at a specific coordinate and repeat calculations for multiple points as a particle moves through space.
In this line:
uintrp = interpolateSolution(results,X,Y,Z,[1,2,3]);
you need to pass ‘results’* as an argument to your ‘E_field’ function. You have not calculated it within it, and a function file will not pick them up from your workspace.
So I added this into line 15:
results = [sol.Ex,sol.Ey,sol.Ez];
I still get the same error though as the issue is with the "Index exceeds array bounds." and references line 7.
Code now looks like this:
function [Ex,Ey,Ez] = 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));
results = [sol.Ex,sol.Ey,sol.Ez];
[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
With respect to:
results = [sol.Ex,sol.Ey,sol.Ez];
Where is ‘sol’ defined? You have not passed it as an argument, or calculated it in the code you posted.
And what is ‘line 7’? That would appear to be your ‘r’ assignment, although when I run that much of your code, it doesn’t throw an error.
Also, this:
epnaut = 8.854187*(10^-12);
actually requires a calculation, slowing your code.
This:
epnaut = 8.854187E-12;
does not.
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)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 22 Jul 2018

Commented:

on 23 Jul 2018

Community Treasure Hunt

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

Start Hunting!