why am i getting grey figure box can someone fix please

3 views (last 30 days)
% 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 Comments
Voss
Voss on 4 Jan 2024
Edited: Voss on 4 Jan 2024
Unable to run the code: The function pendulumODE is undefined (see above).
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 4 Jan 2024
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.

Sign in to comment.

Answers (1)

Alan Stevens
Alan Stevens on 5 Jan 2024
Edited: Alan Stevens on 5 Jan 2024
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

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!