Code is now just stalling at first ODE. There is no error code being displayed. Just continues to run infinitely.
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 个评论
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
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
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 Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!