why am i getting grey figure box can someone fix please

3 ビュー (過去 30 日間)
Jack
Jack 2024 年 1 月 4 日
編集済み: Alan Stevens 2024 年 1 月 5 日
% Constants
g = 9.81; % Acceleration due to gravity
L = 1.0; % Length of the pendulum
t = linspace(0, 20, 1000); % Time vector
% Loop through each initial condition
initial_conditions_list = [0.5; 1.0; 1.5; pi];
for i = 1:length(initial_conditions_list)
figure; % Create a new figure for each initial condition
theta0 = initial_conditions_list(i);
% Analytical solution (from A4)
theta_A4 = theta0 * cos(sqrt(g/L) * t);
% Numerical solution (from A6)
initial_conditions = [theta0; 0];
[t_numerical, Y] = ode45(@(t,Y) pendulumODE(t,Y,g,L), [0 20], initial_conditions);
theta_A6 = Y(:, 1);
% Plotting analytical solution
plot(t, theta_A4, 'r-', 'LineWidth', 2);
hold on;
% Plotting numerical solution
plot(t_numerical, theta_A6, 'b--', 'LineWidth', 2);
hold off;
xlabel('Time (s)');
ylabel('Theta (radians)');
title(['Initial Theta = ', num2str(theta0), ' radians']);
legend('Analytical Solution', 'Numerical Solution');
grid on;
end
Unrecognized function or variable 'pendulumODE'.

Error in solution>@(t,Y)pendulumODE(t,Y,g,L) (line 19)
[t_numerical, Y] = ode45(@(t,Y) pendulumODE(t,Y,g,L), [0 20], initial_conditions);

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
  2 件のコメント
Voss
Voss 2024 年 1 月 4 日
編集済み: Voss 2024 年 1 月 4 日
Unable to run the code: The function pendulumODE is undefined (see above).
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024 年 1 月 4 日
As @Voss pinpointed the function file or function handle (anonymous function) called pendulumODE(t,Y,g,L) is missing.
It can be defined as an anonymous function or function file per se.

サインインしてコメントする。

回答 (1 件)

Alan Stevens
Alan Stevens 2024 年 1 月 5 日
編集済み: Alan Stevens 2024 年 1 月 5 日
Making some assumptions about your function pendulumODE, I think the following is more like what you expect to see:
% Constants
g = 9.81; % Acceleration due to gravity
L = 1.0; % Length of the pendulum
t = linspace(0, 20, 100); % Time vector
% Loop through each initial condition
initial_conditions_list = [0.5; 1.0; 1.5; pi];
for i = 1:length(initial_conditions_list)
figure; % Create a new figure for each initial condition
theta0 = initial_conditions_list(i);
% Analytical solution (from A4)
w = sqrt(g/L);
theta_A4 = theta0 * cos(w * t);
% Numerical solution (from A6)
initial_conditions = [theta0; 0];
[t_numerical, Y] = ode45(@(t,Y) pendulumODE(t,Y,g,L), [0 20], initial_conditions);
theta_A6 = Y(:,1);
% Plotting analytical solution
plot(t, theta_A4, 'r-', 'LineWidth', 2);
hold on;
% Plotting numerical solution
plot(t_numerical, theta_A6, 'b--', 'LineWidth', 2);
hold off;
xlabel('Time (s)');
ylabel('Theta (radians)');
title(['Initial Theta = ', num2str(theta0), ' radians']);
legend('Analytical Solution', 'Numerical Solution');
grid on;
end
function dthetavdt = pendulumODE(~,Y,g,L)
theta = Y(1); v = Y(2);
w = sqrt(g/L);
dthetavdt = [v; -w^2*theta];
end

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by