error in line 15 at initial conditions please help

2 visualizaciones (últimos 30 días)
Jack
Jack el 4 de En. de 2024
Editada: Walter Roberson el 5 de En. de 2024
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;

Respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 4 de En. de 2024
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

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by