why am i getting grey figure box can someone fix please
조회 수: 3 (최근 30일)
이전 댓글 표시
% 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
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
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!