ode45 results won't plot

6 visualizaciones (últimos 30 días)
Connor
Connor el 20 de Dic. de 2022
Editada: Bora Eryilmaz el 20 de Dic. de 2022
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.

Respuestas (1)

Bora Eryilmaz
Bora Eryilmaz el 20 de Dic. de 2022
Editada: Bora Eryilmaz el 20 de Dic. de 2022
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 comentarios
Torsten
Torsten el 20 de Dic. de 2022
@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
Bora Eryilmaz el 20 de Dic. de 2022
Editada: Bora Eryilmaz el 20 de Dic. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by