Euler's method for two first order differential equations?

조회 수: 75 (최근 30일)
Yuhong Jin
Yuhong Jin 2019년 11월 27일
답변: Eng 2023년 4월 23일
I was trying to solve two first order differential equations like below using the Euler's method and plot two graphs with x and y as a function of t.
The differential equations are:
dxdt = @(x,t) -1.*y-0.1.*x;
dydt = @(y,t) x-0.5.*y;
I tried this script below:
a=0; %initial time
b=2000; %final time
h = 0.01; % time step
N = (b-a)./h;
t=a:h:b;
t(1)=a;
x(1)=0;
y(1)=0;
dxdt = @(x,t) -1.*y-0.1.*x;
dydt = @(y,t) x-0.5.*y;
for n=1:N
x(n+1)=x(n)+h.*dxdt(x(n),t(n));
y(n+1)=y(n)+h.*dydt(y(n),t(n));
t(n+1)=a+n*h;
end
plot(t,x,'-',t,y,'--')
But there was an error saying
Unable to perform assignment because the left and right sides have a different
number of elements.
Error in Q6 (line 15)
x(n+1)=x(n)+h.*dxdt(x(n),t(n));
I did not quite understand why.
Could anyone help me with this please?
Thanks in advance.
  댓글 수: 2
darova
darova 2019년 11월 27일
Where is t?
123Capture.PNG
Yuhong Jin
Yuhong Jin 2019년 11월 27일
t is in the differential equations, dxdt and dydt.
I only have the range of t and relationships between dxdt, dydt and x, y.

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

채택된 답변

Jim Riggs
Jim Riggs 2019년 11월 27일
편집: Jim Riggs 2019년 11월 27일
Try preallocating x and y
nsteps = (b-a)/h + 1; % this is the number of elements in t(a:h:b) (It is = N+1)
x=zeros(1,nsteps);
y=zeros(1,nsteps);
Also, when you define t as
t=a:h:b;
This creates a vector with T(1)=a and t(end)=b, so the following line
t(1)=a
is not necessary.
Inside your for loop, t has already been defined, above, as t=a:h:b, so you don't need to re-define it.
x and y need to be preallocated to size "nsteps", which is 1 greater than N, in order for your loop to work.
  댓글 수: 10
Chris Horne
Chris Horne 2022년 3월 29일
I figured it out. The function definition delimiters typo. The code produces then 2D plot of the approximating functions.
Chris Horne
Chris Horne 2022년 3월 31일
Is the term 'forward Euler' the same as 'Euler' in terms of the algorithm? Here is my method for solving 3 equaitons as a vector:
% This code solves u'(t) = F(t,u(t)) where u(t)= t, cos(t), sin(t)
% using the FORWARD EULER METHOD
% Initial conditions and setup
neqn = 3; % set a number of equations variable
h=input('Enter the step size: ') % step size will effect solution size
t=(0:h:4).';%(starting time value 0):h step size
nt = size(t,1); % size of time array
%(the ending value of t ); % the range of t
% define the function vector, F
F = @(t,u)[t,cos(t),sin(t)]; % define the function 'handle', F
% with hard coded vector functions of time
u = zeros(nt,neqn); % initialize the u vector with zeros
v=input('Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: ')
u(1,:)= v; % the initial u value and the first column
%n=numel(u); % the number of u values
% The loop to solve the ODE (Forward Euler Algorithm)
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
end

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

추가 답변 (1개)

Eng
Eng 2023년 4월 23일
% Define the differential equation dydx = @(x,y) x + y;
% Define the initial condition y0 = 1;
% Define the step size and interval h = 0.1; x = 0:h:1;
% Initialize the solution vector y = zeros(size(x)); y(1) = y0;
% Apply the Modified Euler Method for i = 1:length(x)-1 k1 = dydx(x(i), y(i)); k2 = dydx(x(i+1), y(i)+hk1); y(i+1) = y(i) + h/2(k1+k2); end
% Plot the solution plot(x,y) xlabel('x') ylabel('y')

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by