error in line 15 at initial conditions please help

1 回表示 (過去 30 日間)
Jack
Jack 2024 年 1 月 4 日
編集済み: Walter Roberson 2024 年 1 月 5 日
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
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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by