error in line 15 at initial conditions please help
3 次查看(过去 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;
0 个评论
回答(1 个)
Sulaymon Eshkabilov
2024-1-4
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!