why am i getting grey figure box can someone fix please
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% 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
2 commentaires
Sulaymon Eshkabilov
le 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.
Réponses (1)
Alan Stevens
le 5 Jan 2024
Modifié(e) : Alan Stevens
le 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
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!