# Mass spring system equation help

62 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Star Strider
on 8 Aug 2012

Edited: Star Strider
on 8 Aug 2012

I suggest you write an objective function containing your differential equation that would integrate the differential equation, then do curve-fitting of x(t) with nlinfit or lsqcurvefit.

I have provided an objective function here that may work for you. You do not have to pass the parameters specifically to DifEq. It has access to them in the function space. This code assumes all variables and parameters are column vectors.

function X = SMD(B, t, m) % ‘SMD’ for ‘Spring-Mass-Damper’

% B = parameter and initial conditions column vector (unless you want to

% supply the initial conditions in this function rather than passing

% them as parameters).

X0 = B(3:4);

% This gives you the option of passing the last two entries of the

% parameter vector as the initial values of your ODE. In this case, the

% curve-fitting function will also fit the initial conditions as well as

% Kd and Ks. If you don't want the initial conditions to be parameters,

% B becomes [2 x 1] and you define X0 as whatever you like in the

% function.

[T,X] = ode45(@DifEq, t, X0);

function xdot = DifEq(t, x)

% B(1) is the coefficient of viscous friction (‘damper’), Kd;

% B(2) is the spring constant, Ks;

xdot = zeros(2,1);

xdot(1) = x(2);

xdot(2) = -x(1)*B(2)/m -x(2)*B(1)/m;

end

X = xdot(:,2); % Assumes ‘xdot’ is a [N x 2] matrix where N = length(t)

end

I've had success with this general approach in several situations. You may have to experiment with it a bit to fit your particular situation. See Passing Extra Parameters for details in passing the mass m to SMD, or you can define m in the function if it never changes. If you do, then remove it from the SMD argument list.

### More Answers (3)

Sumit Tandon
on 8 Aug 2012

I feel that if you can calculate the values of x' and x'', then you could take a couple of sets of x, x' and x'' and get a system of linear equation of the form Ax - B = 0.

Any other ideas?

Greg Heath
on 9 Aug 2012

There appears to be 2 straightforward approaches:

1. For c1=c/2m, k1=k/m and sufficiently small dt(i) = t(i)-t(i-1)

a. Obtain an inhomogeneous system of linear equations for C = [c1 ; k1] by converting the differential equation to a difference equation in x(i).

b. Obtain the solution to A*C=B via C = A\B.

2. Use NLINFIT or LSQCURVEFIT to estimate c1, k1, A and B from the form of the exact solution

x(i) = exp(-c1*t(i)) * ( A * cos( sqrt(c1^2-k1) * t(i))

+ B * sin( sqrt(c1^2-k1) * (t(i) )

However, since the equation is linear in A and B, a two step estimation of [c1 ; k1 ] and [ A B] might be useful.

Hope this helps.

Greg

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!