I'm trying to solve a 2nd order ode with ode45, but have no idea where to start.

3 views (last 30 days)
This the ode with conditions I'm trying to solve and the code below is as far as I got. It would be appreciated if I could get a detailed step by step to help solve this.
%initial conditions
y0 = [0 1];
tspan = [1 4];

Accepted Answer

Milan Bansal
Milan Bansal on 10 Sep 2024
Edited: Milan Bansal on 10 Sep 2024
Hi Jhryssa,
Here is how you can solve the given ODE.
Convert the second-order ODE into a system of first-order ODEs: MATLAB's ode45 solver is designed for systems of first-order ODEs, so you will need to rewrite the second-order ODE into a system of two first-order equations.
The given ODE is:
with the initial conditions and
Introduce new variables: Let . Then, the system of first-order equations becomes:
Set up initial conditions: From the problem, and .
Here is the MATLAB implementaion:
% Define the system of first-order ODEs
function dydt = ode_system(t, y)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = (2*y(1) - t^2*y(2)) / t;
end
% Set initial conditions
y0 = [0; 1]; % y(1) = 0, y'(1) = 1
% Time span (t = 1 to t = 4)
tspan = [1 4];
% Solve the ODE using ode45
[t, y] = ode45(@ode_system, tspan, y0);
% Plot the result
plot(t, y(:,1))
xlabel('t')
ylabel('y(t)')
title('Solution of the ODE')
Please refer to the documentation link to learn more about ode45.
Hope this helps!

More Answers (1)

Torsten
Torsten on 10 Sep 2024
After dividing your equation by t, you can just follow the example
Solve Nonstiff Equation
under

Tags

Community Treasure Hunt

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

Start Hunting!