Referencing Arbitrary Functions from other Script Files to Solve System of Equations

CODE UPDATED:
I am re-asking this question and this time giving all the context so that those reading this will better understand what I am trying to do. I have a main script file called "trajectsolve" that attempts to solve a system of coupled ODEs for particle trajectory. However, the equations are trying to reference e-field components and b-field components from two separate function files called "efield" and "bfield". I want these field function files to output vector components that then can be used in the ODE system. Moreover, the bfield should not have any input arguments and the efield should ("V" = potential and "R_s" = Radius). I attached the function field files (And zipped STL file "mag with vac space" used by bfield) but the main script body is explicitly pasted here:
syms x(t) y(t) z(t) Y
%Using SI units
q = 1.60217662E-19;
m_e = 1.60217662E-31;
B = 0.5;
[Bx, By, Bz] = bfield();
[Ex, Ey, Ez] = efield(1500,0.1);
%DiffiQ to solve (Lorentz Forces)
ode1 = diff(x,2) == (q/m_e)*(Ex + diff(y)*Bz - diff(z)*By);
ode2 = diff(y,2) == (q/m_e)*(Ey - diff(z)*Bx + diff(x)*Bz);
ode3 = diff(z,2) == (q/m_e)*(Ez + diff(x)*By - diff(y)*Bx);
odes = [ode1; ode2; ode3];
%Initial Conditions
condx1 = x(0) == 1;
condy1 = y(0) == 1;
condz1 = z(0) == 0;
condxv1 = diff(x,2) == (1E+6);
condyv1 = diff(y,2) == (2E+6);
condzv1 = diff(z,2) == (2E+6);
%Solutions
conds = [condx1; condy1; condz1; condvx1; condvy1; condvz1];
S = dsolve(odes,conds);
%Create arbitrary functions for plotting
xSol(t) = S.x;
ySol(t) = S.y;
zSol(t) = S.z;
Sx = matlabFunction(xSol);
Sy = matlabFunction(ySol);
Sz = matlabFunction(zSol);
x = Sx(t,Cx);
y = Sy(t,Cy);
z = Sz(t,Cz);
figure
t = linspace(0,16*pi*m_e*(1/(q*B)),5000);
plot3(xSol(t),ySol(t),zSol(t),'r','LineWidth',3)
xlabel 'x';
ylabel 'y';
zlabel 'z';
grid on
And let's say when I import these fields, how can I make sure that Matlab is able to orientate them in the way I want? Will I need to figure out a coordinate transformation matrix manually? Or is there some function that can handle that for me? Currently, when I do a run by line, I get the error message I posted below in a separate comment:

5 Comments

Code is now just stalling at first ODE. There is no error code being displayed. Just continues to run infinitely.
Thanks @Stephen Cobeldick. I forgot to do this.
There actually is an error, it just takes a very long time to run. I simplified the code so it could do less calculations and display the error message more quickly:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 181-by-181-by-171.
Error in sym/privsubsasgn (line 1085)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/cat>catMany (line 44)
y = privsubsasgn(y,arrays{k},subs{:});
Error in sym/cat (line 27)
ySym = catMany(dim, args);
Error in sym/vertcat (line 19)
ySym = cat(1,args{:});
Error in btraject (line 14)
odes = [ode1; ode2; ode3];
So essentially I am wondering how I should tell Matlab that the size of the object I am setting equal to these equations is of equivalent size? (IE how can I tell Matlab that "diff(x,2)" = matrix size: 181x181x171?
Tom Keaton comments
I think my questions in regard to my code have been too arbitrary and in the future I will ask more specific questions, each with their own individualized thread. I thank everyone though who has provided feedback so far.

Sign in to comment.

Answers (1)

Hi,
This looks like your script
bfield
is missing a
function [what ever comes out] = bfield (whatever goes in)
declaration, since you call it as a function when you code:
... = bfield ()
This is a function call and not a script call.
Best regards
Stephan

3 Comments

I am a little confused by what you mean. In the bfield script file I do have:
function [Bx,By,Bz] = bfield()
So it is a function file and I originally wanted it to be called as a function. However, if I were to just call it as a script instead, this is what I think I would add:
%Run bfield script
tmp = tempname;
mkdir(tmp)
newFile = fullfile(tmp,'bfieldscript.m');
run(bfieldscript);
Right after defining a meshgrid. Would this be correct? (I made another copy called "bfieldscript" here and replaced "bfield" with it)
No you don't. bfield does not show up anywhere in the file you attached. You can make a script that does not take any arguments into a function simply by adding a function name at the top, like this:
function bfieldscript()
Then you can just call it by it's name - no need to put it inside a run() function.
I updated the code since this part does work now @Image Analyst.

Sign in to comment.

Tags

Asked:

on 22 Jul 2018

Commented:

on 22 Dec 2018

Community Treasure Hunt

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

Start Hunting!