Euler's method for two first order differential equations?
45 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Yuhong Jin
el 27 de Nov. de 2019
Respondida: Eng
el 23 de Abr. de 2023
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 comentarios
Respuesta aceptada
Jim Riggs
el 27 de Nov. de 2019
Editada: Jim Riggs
el 27 de Nov. de 2019
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 comentarios
Chris Horne
el 29 de Mzo. de 2022
I figured it out. The function definition delimiters typo. The code produces then 2D plot of the approximating functions.
Chris Horne
el 31 de Mzo. de 2022
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
Más respuestas (1)
Eng
el 23 de Abr. de 2023
% 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')
0 comentarios
Ver también
Categorías
Más información sobre Numerical Integration and Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!