
Numerical solution for 2nd order ODE using Euler Method
조회 수: 10 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
Sam Chak
2022년 3월 27일
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:

댓글 수: 0
SUBRATA
2023년 3월 23일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!