Question regarding MATLAB code to solve for electron in constant electric and magnetic field

9 ビュー (過去 30 日間)
Song Hang Chai
Song Hang Chai 2021 年 10 月 14 日
コメント済み: Alan Stevens 2021 年 10 月 15 日
Hello all the kind soul, I am trying to simulate the trajectory of an electron in constant electric and magnetic field. However, when I am running the code, I keep getting the error "Unable to perform assignment because the left and right sides have a different number of elements." Can anyone please help me, any kind of help will do. Thank you so much. Below is my code.
function dsdt=ExBfunc(~, s_vect)
%s_vect(1) = x
%s_vect(2) = y
%s_vect(3) = z
%s_vect(4) = dx/dt
%s_vect(5) = dy/dt
%s_vect(6) = dz/dt
global Bx; global By; global Bz;
global Ex; global Ey; global Ez;
global q; global m;
dsdt=zeros(6, 1);
dsdt(1) = s_vect(4);
dsdt(2) = s_vect(5);
dsdt(3) = s_vect(6);
dsdt(4) = (q/m)*(Ex + s_vect(5)*Bz-s_vect(6)*By);
dsdt(5) = (q/m)*(Ey + s_vect(6)*Bx-s_vect(4)*Bz);
dsdt(6) = (q/m)*(Ez + s_vect(4)*Bz-s_vect(5)*Bx);
end
clear
close all
clc
%Define constant variables q and m
q=1.602*10^(-19);
m=9.11*10^(-31);
%Define the value for constant electric field
Ex=0;
Ey=0;
Ez=1;
%Define the value for constant magnetic field
Bx=1;
By=0;
Bz=0;
%Define the initial position and initial velocity of the electron as a
%column vector
r0=[0 0 0]';
v0=[0 0 0]';
%Pass in the constants to the defined function E_3D and B_3D
%E=E_3D(Ex, Ey, Ez);
%B=B_3D(Bx, By, Bz);
s0=[r0; v0];
%Define the timespan
t0=0;
T=10;
tspan=[t0 T];
disp('Simulation begins');
%Solve the differential equation by numerical method using ode45
[t, s_vect]=ode45(@ExBfunc, tspan, s0');
disp('Simulation done');
%Plot the trajectory of electron of on each axis against time
plot(t,s_vect(:,1));
plot(t,s_vect(:,2));
plot(t,s_vect(:,3));
disp('Plotting done');

回答 (1 件)

Alan Stevens
Alan Stevens 2021 年 10 月 14 日
  1. If you are going to use global variables they need to be declared outside the function as well as inside.
  2. Your values of q and m are such that the simulation takes forever, so scale S = s*m/q and then rescale s_vector*q/m at the end.
global Bx; global By; global Bz;
global Ex; global Ey; global Ez;
%Define constant variables q and m
q=1.602*10^(-19);
m=9.11*10^(-31);
%Define the value for constant electric field
Ex=0;
Ey=0;
Ez=1;
%Define the value for constant magnetic field
Bx=1;
By=0;
Bz=0;
%Define the initial position and initial velocity of the electron as a
%column vector
r0=[0 0 0]';
v0=[0 0 0]';
%Pass in the constants to the defined function E_3D and B_3D
%E=E_3D(Ex, Ey, Ez);
%B=B_3D(Bx, By, Bz);
s0=[r0; v0];
%Define the timespan
t0=0;
T=10;
tspan=[t0 T];
disp('Simulation begins');
Simulation begins
%Solve the differential equation by numerical method using ode45
[t, s_vect]=ode45(@ExBfunc, tspan, s0);
disp('Simulation done');
Simulation done
s_vect = s_vect*q/m;
%Plot the trajectory of electron of on each axis against time
subplot(3,1,1)
plot(t,s_vect(:,1));
subplot(3,1,2)
plot(t,s_vect(:,2));
subplot(3,1,3)
plot(t,s_vect(:,3));
disp('Plotting done');
Plotting done
function dsdt=ExBfunc(~, s_vect)
%s_vect(1) = x
%s_vect(2) = y
%s_vect(3) = z
%s_vect(4) = dx/dt
%s_vect(5) = dy/dt
%s_vect(6) = dz/dt
global Bx; global By; global Bz;
global Ex; global Ey; global Ez;
dsdt=zeros(6, 1);
dsdt(1) = s_vect(4);
dsdt(2) = s_vect(5);
dsdt(3) = s_vect(6);
dsdt(4) = (Ex + s_vect(5)*Bz-s_vect(6)*By);
dsdt(5) = (Ey + s_vect(6)*Bx-s_vect(4)*Bz);
dsdt(6) = (Ez + s_vect(4)*Bz-s_vect(5)*Bx);
end
  2 件のコメント
Alan Stevens
Alan Stevens 2021 年 10 月 15 日
The ratio of q/m is around 10^11. I think this might give ode45 convergence problems, though I don't know for sure.

サインインしてコメントする。

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by