error in line 15 at initial conditions please help

4 次查看(过去 30 天)
function dydt = pendulum_system(t, y)
g = 9.81; % Acceleration due to gravity
L = 1.0; % Length of the pendulum
theta = y(1);
omega = y(2);
dtheta_dt = omega;
domega_dt = -omega - (g/L) * sin(theta) + cos(0.2*t);
dydt = [dtheta_dt; domega_dt];
end
% Initial conditions
initial_conditions = [pi/4; 0];
% Solve the ODE system
[t, Y] = ode45(@pendulum_system, [0 200], initial_conditions);
% Plotting
figure;
plot(t, Y(:, 1));
xlabel('Time (s)');
ylabel('Theta (radians)');
title('Solution of Pendulum ODE for Theta');
grid on;

回答(1 个)

Sulaymon Eshkabilov
This is how the code should be put together and executed:
% Initial conditions
initial_conditions = [pi/4; 0];
% Solve the ODE system
[t, Y] = ode45(@pendulum_system, [0 200], initial_conditions);
% Plotting
figure;
plot(t, Y(:, 1));
xlabel('Time (s)');
ylabel('Theta (radians)');
title('Solution of Pendulum ODE for Theta');
grid on;
%% pendulum_system is a nested function:
function dydt = pendulum_system(t, y)
g = 9.81; % Acceleration due to gravity
L = 1.0; % Length of the pendulum
theta = y(1);
omega = y(2);
dtheta_dt = omega;
domega_dt = -omega - (g/L) * sin(theta) + cos(0.2*t);
dydt = [dtheta_dt; domega_dt];
end

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by