How do I create an arbitrary function that can be referenced in another script correctly?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Assumptions 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!