Problems with Matlab code for simulating spring motion

In summary, the code is difficult to read and understand, and it is difficult to get it to work properly. The problem is that the author is trying to write a function that simulates the motion of a series of connected Hooke's law oscillators. Half of the system has stiffness 1 while the other has variable stiffness. The size of the system is 2*l. The function solves a second order differential equation for the size of the system. The velocities and initial conditions for the velocities are not provided.
  • #1
bcolson
3
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
  • #2
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 ?
 

FAQ: Problems with Matlab code for simulating spring motion

Why is my spring motion simulation not working?

There could be several reasons for this. Some common issues include errors in the code, incorrect input parameters, or missing necessary functions or toolboxes. It is important to carefully review your code and check for any mistakes or missing components. It may also be helpful to consult online resources or ask for assistance from more experienced Matlab users.

How can I improve the accuracy of my spring motion simulation?

One way to improve the accuracy of your simulation is by using smaller time steps. This will allow for a more precise calculation of the spring's motion. Additionally, double-checking your input parameters and equations for the simulation can also help to ensure accuracy.

My spring motion simulation is producing unexpected results. What should I do?

If your simulation is producing unexpected results, it may be helpful to review your code and equations to ensure they are correct. You may also want to check your input parameters to make sure they are appropriate for the simulation. If you are still unsure, it may be beneficial to seek advice from other Matlab users or consult online resources.

Can I simulate multiple springs in Matlab?

Yes, you can simulate multiple springs in Matlab. You will need to modify your code to account for the additional springs and their respective parameters. It may also be helpful to use arrays or loops to simplify the process of simulating multiple springs.

How can I visualize the results of my spring motion simulation?

There are various ways to visualize the results of your simulation in Matlab. One option is to plot the position, velocity, or acceleration of the spring over time using the plot function. You can also use animation functions, such as the "movie" function, to create a visual representation of the spring's motion. Additionally, you may want to explore the use of 3D plots to visualize more complex spring motion simulations.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
10
Views
2K
Back
Top