error in line 15 at initial conditions please help
1 回表示 (過去 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!