Plotting a second order system using ode45()
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ryan Rizzo
el 18 de Mzo. de 2018
Comentada: Star Strider
el 22 de Dic. de 2021
The system I am modelling is a spring-mass-damper where m is mass, k is spring stiffness and c is the damping coefficient.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171364/image.png)
First of all I have a basic function splitting the second order differential equation:
function dydt=springmassdamper(t,y,m,c,k)
dydt=zeros(2,1);
dydt(1)=m*y(2);
dydt(2)=-c*y(2)-k*y(1) + m*9.81;
Next I am allowing user input and plotting using ode45() as follows:
clear all;
close all;
clc;
disp('Spring-Mass-Damper Calculator');
try
mass = input('Enter mass m (kg) between 0.5kg and 20kg ');
if (mass >=0.5 && mass <= 20)
m = mass;
else
mass = input('Enter mass m (kg) between 0.5kg and 20kg');
m = mass;
end
spring_damping = input('Enter spring damping c (Ns/m)');
if (spring_damping > 0)
c = spring_damping;
else
disp("Enter a value larger than 0");
end
spring_stiffness = input('Enter spring stifness k (N/m)');
if (spring_stiffness > 0)
k = spring_stiffness;
else
disp("Enter a value larger than 0");
end
%initial conditions [x(0) and x*(0)] [displacement velocity]
[t,y] = ode45(@(t,y) springmassdamper(t,y,m,c,k), [0 1], [0 0]);
plot(t,y(:,1),'r','LineWidth',1);
hold on;
plot(t,y(:,2),'k','LineWidth',1);
legend('Velocity', 'Displacement');
xlabel('Time (s)');
ylabel('Displacement (m)');
title('Spring-Mass-Damper');
catch ME
warning('Can not use characters, please enter a number instead');
end
Thus, with 0 initial conditions for both velocity and displacement I get the following graph which does not make sense to me:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171365/image.png)
The issue I am encountering is that, when I have any type of input, one of the graphs (displacement) does not settle at zero. I do not understand how this is possible. Is there an issue with the parameters I am plotting or is there something wrong in one of the functions?
Example User input:
Enter mass m (kg) between 0.5kg and 20kg: 10, Enter spring damping c (Ns/m): 50, Enter spring stifness k (N/m): 1000
The red graph does not seem correct to me. It looks like I am exceeding Hooke's Law and deforming the spring.
I also notice that if I increase the velocity initial conditions enough such that
%initial conditions [x(0) and x*(0)] [displacement velocity]
[t,y] = ode45(@(t,y) springmassdamper(t,y,m,c,k), [0 1], [0 15]);
the red graph (displacement) approaches zero. With the exact same user Input I get
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171366/image.png)
When I input initial conditions for displacement to be 10 and velocity at 0, I get the mass settling at 0 displacement which is correct.
%initial conditions [x(0) and x*(0)] [displacement velocity]
[t,y] = ode45(@(t,y) springmassdamper(t,y,m,c,k), [0 1], [10 0]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/171367/image.png)
Any suggestions on what I am doing wrong and/or flaws in my reasoning would be appreciated. I must be missing something crucial here.
0 comentarios
Respuesta aceptada
Star Strider
el 18 de Mzo. de 2018
I believe ‘dydt(1)’ should simply be:
dydt(1) = y(2);
instead of multiplying it by ‘m’.
5 comentarios
Philippe Miron
el 20 de Nov. de 2019
The other error is simply the legend of the plot. y(:,1) contains the position and y(:,2) contains the velocity. So the legend arguments:
legend('Velocity', 'Displacement');
should be:
legend('Displacement', 'Velocity');
Más respuestas (1)
enrico Rothe
el 22 de Dic. de 2021
hey,
can someone explain to me why i get the error if i copy paste the code :
Not enough input arguments.
Error in springmassdamper (line 4)
dydt(1)=y(2);
i m new to matlab and appreciate any help
1 comentario
Star Strider
el 22 de Dic. de 2021
Somewhere on the system running that code, there is a function defined as ‘y’ with more than one argument.
Run this from a script or the Command Window —
which y -all
That should reveal the problem. Alternatively, there could be a ‘dydt’ function, however I doub thatt it would throw the same error in this instance. It could be worthwhile checking it as well. The solution is to give the function another name.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!