How do I create an arbitrary function that can be referenced in another script correctly?
Show older comments
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.
Tom Keaton
on 22 Jul 2018
Edited: Tom Keaton
on 22 Jul 2018
Star Strider
on 22 Jul 2018
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.
Tom Keaton
on 22 Jul 2018
Tom Keaton
on 22 Jul 2018
Star Strider
on 22 Jul 2018
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.
Tom Keaton
on 22 Jul 2018
Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!