Trouble plotting Second Order Equation
조회 수: 2 (최근 30일)
이전 댓글 표시
I'm trying to plot the numerical solution of the second order equation: y'' + sin(y) =0, with the intital values of y(0) being 0.1, 0.7, 1.5, and 3.0, and y'(0) =0. However, the following code is giving me an incorrect graph, and I'm not sure how to resolve this.
rhs = @(t, y) [y(2); -sin(y(1))];
[xa, ya] = ode45(rhs, [0 10], [0.1 0]);
figure, hold on
for y0 = 0:0
for yp0 = 0.1:0.7:1.5:3.0
[tfor, yfor] = ode45(rhs, [0 6.28], [y0 yp0]);
[tbak, ybak] = ode45(rhs, [0 6.28], [y0 yp0]);
plot(tfor, yfor(:,1))
plot(tbak, ybak(:,1))
end
end
댓글 수: 0
채택된 답변
Sam Chak
2023년 4월 19일
Hi @Nina
It appears that you tried to simulate the undamped pendulum-like ODE with multiple initial angles.
If I'm correct, then you can fix your code like the following:
% undamped pendulum
odefcn = @(t,y) [y(2); -sin(y(1))];
% settings
y10 = [0.1 0.7 1.5 3.0]; % initial angle (in radian)
y20 = 0; % initial angular rate
tspan = [0 20];
% solving the ODE in loops
for j = 1:length(y10)
[t, y] = ode45(odefcn, tspan, [y10(j) y20]);
plot(t, y(:,1))
hold on
end
hold off
% display graph properties
grid on
legend(strcat('y_{1}(0) = ', num2str(y10')), 'location', 'Best')
xlabel({'$t$'}, 'Interpreter', 'latex')
ylabel({'$y_{1}(t)$'}, 'Interpreter', 'latex')
title({'Solutions of $\ddot{y} = - \sin(y)$, with multiple initial conditions'}, 'Interpreter', 'latex')
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!