How to Create Vector Function File

7 views (last 30 days)
Tom Keaton
Tom Keaton on 25 Sep 2019
Edited: madhan ravi on 25 Sep 2019
Hello,
I wish to create a vector function defined as the following:
Capture.JPG
The code I have so far:
function [Ex, Ey, Ez] = E_field()
syms x y z
con = -2.8E+8;
E = con*z.^3;
Ex = matlabFunction(E(1));
Ey = matlabFunction(E(2));
Ez = matlabFunction(E(3));
end
However, I get this error when this function is being referenced when Matlab's ode solver is running:
Index exceeds the number of array elements (1).
Error in sym/subsref (line 890)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in E_field (line 7)
Ey = matlabFunction(E(2));
I do not see why this wouldn't work as I have a very similar function file that is also being referenced but doesn't throw and error:
function [Bx, By, Bz] = B_test()
%Bfieldstrength = 0.64; %In (Teslas)
magvol = 3.218E-6; %In (m)
mu0 = (4*pi)*10^-7;
magnetization = (Bfieldstrength*magvol)/mu0;
syms x y z
m = [0,0,magnetization];
r = [x, y, z];
B = mu0*(((dot(m,r)*r*3)/norm(r)^5) - m/norm(r)^3);
Bx = matlabFunction(B(1));
By = matlabFunction(B(2));
Bz = matlabFunction(B(3));
end
Suggstions?

Answers (1)

madhan ravi
madhan ravi on 25 Sep 2019
Edited: madhan ravi on 25 Sep 2019
I don’t have any suggestions on how to improve it for as for your confusion regarding the error message:
In your first file E only have one element where as in your second file B has three elements.

Categories

Find more on Symbolic Math Toolbox 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!