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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by