Borrar filtros
Borrar filtros

I am getting error not enough input arguments.

2 visualizaciones (últimos 30 días)
Puja
Puja el 22 de En. de 2024
Comentada: Sam Chak el 14 de Jun. de 2024
function fval=massSpringFun(t,y)
%Function for mass-spring system
%Define constants
m=1;
c=0.001;
k=100;
h=0.01;
x1=0.01;
x2=0;
%This set of ODEs will be solved using ode45
x1=y(1);
v1=y(2);
x2=y(3);
v2=y(4);
%Definr dy/dt
fval(1,1)=v;
fval(2,1)=(k/m)*(x1-x2)-(c/h)*v2;
%To run the mass spring system
y0=[1;0];
tspan=[0 10];
[tsol,ysol]=ode45(@(t,y)massSpringFun(t,y),tspan,y0);
plot(tsol,ysol(:,1));

Respuestas (1)

Abhimenyu
Abhimenyu el 14 de Jun. de 2024
Hi Puja,
The issue you are facing is because of the function definition and the initial conditions provided. The 'y0' variable is defined as [1; 0], but the system has four variables (displacements and velocities for two masses). You need to provide initial conditions for all four variables.
Please follow the below-mentioned example MATLAB code to correctly simulate the mass-spring system:
% Define the mass-spring system function
function fval = massSpringFun(t, y)
% Extract variables
x1 = y(1);
v1 = y(2);
x2 = y(3);
v2 = y(4);
% Define constants
m = 1;
c = 0.001;
k = 100;
h = 0.01;
% Define dy/dt
fval(1, 1) = v1;
fval(2, 1) = (k/m) * (x1 - x2) - (c/h) * v2;
fval(3, 1) = v2;
fval(4, 1) = 0; % Assuming no external force on the second mass
end
Providing initial conditions for all the four variables:
% Initial conditions for all four variables
y0 = [1; 0; 0; 0];
% Time span
tspan = [0, 10];
% Solve the ODE system
[tsol, ysol] = ode45(@massSpringFun, tspan, y0);
% Plot the displacement of the first mass
plot(tsol, ysol(:, 1));
xlabel('Time');
ylabel('Displacement (x1)');
title('Mass-Spring System');
grid on;
I hope this helps to solve your query!
  1 comentario
Sam Chak
Sam Chak el 14 de Jun. de 2024
For a typical mass-spring system, the system should be realistically stable when all energy dissipation mechanisms are accounted for. I have selected some numerical parameter values in order to bring the displacement response back down to a physically realizable range.
function fval = massSpringFun(t, y)
% Extract variables
x1 = y(1);
v1 = y(2);
x2 = y(3);
v2 = y(4);
% Define constants
m = 1;
c = 0.001;
k = 100;
h = 0.01;
% Define dy/dt
fval(1, 1) = v1;
fval(2, 1) = (k/m) * (x1 - x2) - (c/h) * v2;
fval(3, 1) = v2;
fval(4, 1) = 243.39*x1 + 24.36*v1 - 242*x2 - 22.13*v2;
end
tspan = [0, 10];
y0 = [1; 0; 0; 0];
[t, y] = ode45(@massSpringFun, tspan, y0);
plot(t, y(:, 1)), grid on
xlabel('Time'), ylabel('x_{1} Displacement (m)'), title('Mass-Spring System');

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