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

1 次查看(过去 30 天)
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 个评论
Tom Keaton
Tom Keaton 2018-7-23
编辑:Tom Keaton 2018-7-23
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?
Walter Roberson
Walter Roberson 2018-12-22
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.

请先登录,再进行评论。

回答(1 个)

Stephan
Stephan 2018-7-22
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 个评论
Image Analyst
Image Analyst 2018-7-23
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by