How to Create Vector Function File

9 次查看(过去 30 天)
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?

回答(1 个)

madhan ravi
madhan ravi 2019-9-25
编辑:madhan ravi 2019-9-25
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.

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by