How to implement Forward Euler Method on a system of ODEs

조회 수: 6 (최근 30일)
Cynthia Struijk
Cynthia Struijk 2019년 12월 13일
편집: Cynthia Struijk 2019년 12월 14일
Hi,
I was wondering if it is possible to solve a function using the forward Euler method in the form:
function [dydt] = ODEexample(t,y, AdditionalParameters)
dydt(1:2) = y(3:4);
dydt(3:4) = y(1:2)
dydt = dydt';
end
Or should I adjust the function?
  댓글 수: 4
darova
darova 2019년 12월 14일
Where are the original equations?
Cynthia Struijk
Cynthia Struijk 2019년 12월 14일
편집: Cynthia Struijk 2019년 12월 14일
function [dxdt] = ODEparticles(t,x, par_c)
dxdt(1:2) = x(3:4);
dxdt(3:4) = (par_c.k*par_c.qi*par_c.qj*(x(1:2)-par_c.xj))/(par_c.m*(norm(par_c.xj-x(1:2))^3));
dxdt = dxdt';
end
with
par_c.k = 14.39964548; % electronvolt per Angström
par_c.xj = [0;0]; % position of particle j
par_c.m = 1; % mass of particle in unified atomic mass units
par_c.qi = 1; % charge of electron i in Coulomb
par_c.qj = -1; % charge of electron j in Coulomb
tspan_particle = [0 20];
init_particle = [ [1;0] [0;-4.5] ];

댓글을 달려면 로그인하십시오.

채택된 답변

darova
darova 2019년 12월 14일
I've tried and reached a success
par_c.k = 14.39964548; % electronvolt per Angström
par_c.xj = [0 0]; % position of particle j
par_c.m = 1; % mass of particle in unified atomic mass units
par_c.qi = 1; % charge of electron i in Coulomb
par_c.qj = -1; % charge of electron j in Coulomb
T = 2; % simulation time
n = 100; % number of steps
X = zeros(n,4); % preallocation
dt = T/n; % time step
X(1,:) = [ 1 0 0 -4.5 ];% initial conditions?
f = @(x) [x(3:4) (par_c.k*par_c.qi*par_c.qj*(x(1:2)-par_c.xj))/(par_c.m*(norm(par_c.xj-x(1:2))^3))];
for i = 1:n-1
X(i+1,:) = X(i,:) + dt*f(X(i,:));
end
plot(X)
  댓글 수: 5
darova
darova 2019년 12월 14일
Impossible!
function main
par_c.k = 14.39964548; % electronvolt per Angstr?m
par_c.xj = [0 0]; % position of particle j
par_c.m = 1; % mass of particle in unified atomic mass units
par_c.qi = 1; % charge of electron i in Coulomb
par_c.qj = -1; % charge of electron j in Coulomb
T = 5; % simulation time
n = 1000; % number of steps
X = zeros(n,4); % preallocation
dt = T/n; % time step
X(1,:) = [ 1 0 0 -4.5 ];% initial conditions?
function dy = f(~,x)
x = x(:)';
dy = [x(3:4) (par_c.k*par_c.qi*par_c.qj*(x(1:2)-par_c.xj))/(par_c.m*(norm(par_c.xj-x(1:2))^3))];
dy = dy';
end
for i = 1:n-1
X(i+1,:) = X(i,:) + dt*f(1,X(i,:))';
end
plot(X(:,1),X(:,2))
[t,X] = ode45(@f,[0 T],[1 0 0 -4.5]);
hold on
plot(X(:,1),X(:,2),'r')
hold off
legend('euler method','ode45')
end
Cynthia Struijk
Cynthia Struijk 2019년 12월 14일
Oh probably I did something wrong then. This seems to work, thank you so much for all the help, hope you'll have a wonderful day :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by