| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- % 1. Clear the workspace and figures
- clear; clc; close all;
- % 2. Define the differential equation system
- % The solver expects a function with inputs (t, Y) where Y is a vector:
- % Y(1) represents y, and Y(2) represents dy/dt.
- ode_system = @(t, Y) [
- Y(2); % dY1/dt = Y2
- Y(3); % dY2/dt = Y3
- -3*Y(3)-(1-Y(1)^2)*Y(2)+Y(1) % dY3/dt =
- ];
- % 3. Set the time span for simulation [start_time, end_time]
- t_span = [0, 5];
- % 4. Define initial conditions: [initial_y, initial_dydt]
- % For example: y(0) = 0.5 and dy/dt(0) = 0.0
- initial_conditions = [0.5; 0.0; 0];
- % 5. Solve the system numerically using ode45
- [t, Y] = ode45(ode_system, t_span, initial_conditions);
- % 6. Extract the individual states from the solution matrix
- y_sol = Y(:, 1); % First column is y(t)
- dydt_sol = Y(:, 2); % Second column is dy/dt(t)
- d2ydt2_sol = Y(:, 3); % Third column is d2y/dt2(t)
- % 7. Plot the results
- figure;
- plot(t, y_sol, '-b', 'LineWidth', 2);
- hold on;
- plot(t, dydt_sol, '--r', 'LineWidth', 1.5);
- grid on;
- % Labeling the graph
- title('Numerical Solution of Higher-Order ODE');
- xlabel('Time (t)');
- ylabel('State Values');
- legend('y(t) (Position)', 'dy/dt (Velocity)', 'Location', 'best');
|