Numerical solution for 2nd order ODE using Euler Method

7 views (last 30 days)
Hello, i am trying to solve y''+4*y'+4y=t^3*e^(2*t) with initial values of y'(0) = 0 and y(0) = 0 using Euler's method to get the numeric solution but i don't know how to start

Answers (2)

Sam Chak
Sam Chak on 27 Mar 2022
Hi @KG
If you show us the Euler method formula, then we don't have to search in Google or Wikipedia. This also ensures that the formula you give to us is correct and reliable with source cited. Anyhow, here is the demo. Hope that this is the Euler solution that you are looking for and acceptable.
function Demo_Euler
close all;
clc
tStart = 0;
step = 1e-2; % if step is smaller, then numerical solution is more accurate
tEnd = 1;
t = [tStart:step:tEnd]; % simulation time span
y0 = [0, 0]; % initial values
f = @(t,x) [x(2); % system of 1st-order ODEs
(t^3)*exp(2*t) - 4*x(1) - 4*x(2)];
yEuler = EulerSolver(f, t, y0); % calling Euler Solver
% analytical solution (can be obtained by hand, or by using 'dsolve')
yExact = (1/128)*exp(-2*t).*(3*(1 + t) + exp(4*t).*(-3 + 9*t - 12*t.^2 + 8*t.^3));
% compare numerical solution with analytical solution
plot(t, yEuler(1,:), t, yExact)
grid on
xlabel('Time, t')
ylabel('y_{1} and y_{2}')
title('Time responses of the system')
legend('Euler solution', 'Exact solution', 'location', 'best')
end
function y = EulerSolver(f, x, y0)
y(:, 1) = y0; % initial condition
h = x(2) - x(1); % step size
n = length(x); % number of steps
for i = 1:n-1
y(:, i+1) = y(:, i) + h*f(x(i), y(:, i));
end
end
Result:

SUBRATA
SUBRATA on 23 Mar 2023
clc
close all
clear all
%All the function
f=@(t,y,z)z;
g=@(t,y,z)(t^3*(exp(2*t))-4*z-4*y);
t0=input('input of the initial t_0=');
y0=input('input of the initial y_0=');
z0=input('input of the initial z_0=');
tn=input('input of the t_n=');
h=input('step size=');
n=(tn-t0)/h;
t(1)=t0;y(1)=y0;z(1)=z0;
fprintf(' time \t\t y\t\t z \n');
for i=1:n
y(i+1)=y(i)+h*f(t(i),y(i),z(i));
z(i+1)=y(i)+h*g(t(i),y(i),z(i));
t(i+1)=t(i)+h;
fprintf(' %3.4f \t\t\t %3.4f \t\t\t %3.4f \n',t(i+1),y(i+1),z(i+1));
plot(t,y)
end

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!