Problems with Matlab code for simulating spring motion

Click For Summary
SUMMARY

The discussion focuses on a Matlab function designed to simulate the motion of a series of connected Hooke's law oscillators, specifically using the function mtxsolver(M,s,l). The system consists of oscillators with fixed stiffness of 1 and variable stiffness s, with a total size defined as 2*l. Participants express confusion regarding the matrix dimensions for initial conditions, particularly why the initial conditions matrix is smaller than the stiffness matrix and how ode45 utilizes these dimensions in solving the system of equations.

PREREQUISITES
  • Understanding of Hooke's law and oscillatory motion
  • Familiarity with Matlab programming and syntax
  • Knowledge of numerical methods, specifically ode45 for solving differential equations
  • Basic linear algebra concepts, particularly matrix operations
NEXT STEPS
  • Explore Matlab's ode45 function and its application in solving ordinary differential equations
  • Study matrix manipulation in Matlab, focusing on creating and indexing matrices
  • Learn about the principles of coupled oscillators and their mathematical modeling
  • Investigate the implications of initial conditions in numerical simulations of dynamic systems
USEFUL FOR

This discussion is beneficial for Matlab programmers, physicists modeling mechanical systems, and engineers working on simulations of oscillatory motion in mechanical structures.

bcolson
Messages
3
Reaction score
0
The issue I am having is setting up the actual program for the problem. I am having trouble interpreting the code and, because of that, having issues getting it to work properly. The whole thing was written with Matlab and the goal was to write a function that could simulate the motion of a series of connected Hooke's law oscillators. Half of the system has stiffness 1 the other half has variable stiffness s.
Total size of the system should be 2*l.function [T,y] = mtxsolver(M,s,l)
%M == mass, s == stiffness, l == length

Size = 2*l;
size = Size-1;

%set up vectors
k = zeros((Size),1);
R = 1;

%set up k values in loops.
while R < (floor(Size/2))
k(R) = 1;
R = R + 1;
end

while R < (Size)
k(R) = s;
R = R + 1;
end

k(Size+1) = 0;

for c = 1:Size
for r = 1:Size
if r == c
%to diagonal
A(r,c) = (k(r)+k(r+1))/M;
elseif r-c == 1
%to left diagonal
A(r,c) = -k(r)/M;
elseif r-c == -1
%to right diagonal
A(r,c) = -k(r+1)/M;
else
%to all else
A(r,c) = 0;
end
end
end%set up initial conditions
initCond = zeros(2,floor((Size+1)/2));
initCond(1,1) = 1;

%ode function
fun = @(t,x)[x(2); - A*x(1)];
[T,y] = ode45(fun,[0,30],initCond);
endI can't wrap my head around the matrix size of the initial conditions. Why exactly does it have to be so much smaller than the size of the matrix describing the coupled stiffness? Is it something where ode45 is only using half of the matrix A for position calculations?
 
Physics news on Phys.org
bcolson said:
The issue I am having is setting up the actual program for the problem. I am having trouble interpreting the code and, because of that, having issues getting it to work properly. The whole thing was written with Matlab and the goal was to write a function that could simulate the motion of a series of connected Hooke's law oscillators.Half of the system has stiffness 1 the other half has variable stiffness s.
Total size of the system should be 2*l.

Matlab:
function [T,y] = mtxsolver(M,s,l)
%M == mass, s == stiffness, l == length

Size = 2*l;
size = Size-1;

%set up vectors
k = zeros((Size),1);
R = 1;

%set up k values in loops.
while R < (floor(Size/2))
    k(R) = 1;
    R = R + 1;
end

while R < (Size)
    k(R) = s;
    R = R + 1;
end

k(Size+1) = 0;

for c = 1:Size
  for r = 1:Size
      if r == c
           %to diagonal
           A(r,c) = (k(r)+k(r+1))/M;
       elseif r-c == 1
           %to left diagonal
           A(r,c) = -k(r)/M;
       elseif r-c == -1
           %to right diagonal
           A(r,c) = -k(r+1)/M;
       else
           %to all else
           A(r,c) = 0;
       end
   end
end%set up initial conditions
initCond = zeros(2,floor((Size+1)/2));
initCond(1,1) = 1;

%ode function
fun = @(t,x)[x(2); - A*x(1)];
[T,y] = ode45(fun,[0,30],initCond);
end

I can't wrap my head around the matrix size of the initial conditions. Why exactly does it have to be so much smaller than the size of the matrix describing the coupled stiffness? Is it something where ode45 is only using half of the matrix A for position calculations?
The ##[##code=matlab##]## and ##[##/code##]## make it look a lot better.

I know nothing about matlab, but you are start doubling the size. Why ? Could you adhere to the template and provide some more info on what is what ? For me size has the dimension of meters, but here it is the length of an array.
Note that you solve 'size' second order differential equations. Where are the velocities and their initial conditions ?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K