ode45 results won't plot
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to plot the solution for a differential equation using ode45. However, when I run the code a plot is not generated. I don't get any error messages and have altered the plot code many times, and still no figure is being generated. I have tried calling the figure in the command window using figure(1) and get a blank plot. I have restarted the program as well, making sure that no other plots are open and that MATLAB isn't resuing old plots. I have also put a drawnow; in at the end of the plot code and still no figure is genereated. Below is my code:
m1 = 5;
L1 = 0.5;
T1 = 0.25;
function solveODE_SecondOrder()
t = 0:.001:10;
q0 = [0.1; 0.3];
[t,q] = ode45(@ode_SecondOrder, t, q0);
function [dqdt] = ode_SecondOrder(t,q)
dqdt = [q(2); (T1-1/2*m1*L1*9.8*cos(q(1)))/(m1*L1^2+1/12*m1*L1)];
end
figure(1);
subplot(1, 2, 1);
plot(t, q(:, 1)); %are my input variables off perhaps?
xlabel('time(s)');
ylabel('q1');
subplot(1,2,2);
plot(t, q(:, 2));
xlabel('time(s)');
ylabel('q2');
end
Any advice would be greatly appreciated.
댓글 수: 0
답변 (1개)
Bora Eryilmaz
2022년 12월 20일
편집: Bora Eryilmaz
2022년 12월 20일
Your functions are a bit intertwined:
solveODE_SecondOrder()
function solveODE_SecondOrder()
t = 0:.001:10;
q0 = [0.1; 0.3];
[t,q] = ode45(@ode_SecondOrder, t, q0);
figure(1);
subplot(1, 2, 1);
plot(t, q(:, 1)); %are my input variables off perhaps?
xlabel('time(s)');
ylabel('q1');
subplot(1,2,2);
plot(t, q(:, 2));
xlabel('time(s)');
ylabel('q2');
end
function [dqdt] = ode_SecondOrder(t,q)
m1 = 5;
L1 = 0.5;
T1 = 0.25;
dqdt = [q(2); (T1-1/2*m1*L1*9.8*cos(q(1)))/(m1*L1^2+1/12*m1*L1)];
end
댓글 수: 2
Torsten
2022년 12월 20일
@Connor comment moved here:
Ok so it appears the issue were my variables defined at the start of the code. They need to be put within the function in order to generate a plot. I'm not sure why defining a set of variables before ode45 causes no figure to be generated, but when I put the variables in the function, plots were generated fine.
Bora Eryilmaz
2022년 12월 20일
편집: Bora Eryilmaz
2022년 12월 20일
Generating the plots and the function needing its variables are not related issues.
Your original code put the plotting commands into the solveODE_SecondOrder() function, but you did not call that function anywhere in your code. So plots were not generated. Your ode_SecondOrder() function inside solveODE_SecondOrder() was not called either: so you did not see the errors related to missing variables in that function.
Defining the variables m1, L1, etc. at the top of your code does not make them available to the function ode_SecondOrder() automatically.
Hope this explanation helps.
참고 항목
카테고리
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!