Jump to content

Matlab help.. or general modeling of matrix with functions in it


mooeypoo

Recommended Posts

Okay, Ironically, I don't even know if this is the right forum for it. Maybe Engineering would be better.. or computer science.. but it's mainly mathematical, so I was hoping the people who frequent this forum will be able to help me out.

 

 

 

 

I want to model a system of objects, each with mass m, attached to each other by springs of constant k.

But I have 20 such objects that move in time.

 

My equation involves both an integer-counter x(s) where s is from 1 to 20 (counting the objects), but also x is a function of t (which is continuous).

 

All I want to try and do is solve the relatively simple partial differential equation:

 

d^2x/dt^2 - k/m x'' =0

 

Where x'' relates to differentiation in relation to s, so it's not continuous. I represent it by x'' = x(s+1) + x(s-1) - 2*x(s)

 

And I want to solve for x, symbolically.

 

I know that if I had everything in relation to a single variable (that is, x' and x'' differentials in respect to t) then it's a simple ODE. But.. it's.. not exactly that.

 

I'm stuck, I don't even know how to start with it, really. I assume I'll be needing a "for" loop, but how do I solve each of those equations for the second derivative of x per t, and insert it into a double-index array (matrix, really).

 

It's basically creating a matrix that has functions in it rather than just numbers, and then trying to model that. I'm sure it's possible, I just have no clue how to start it.

 

Even giving me links for further reading would help. Anything.. help!

 

Thanks a lot!

 

~mooey

Link to comment
Share on other sites

Okay, Ironically, I don't even know if this is the right forum for it. Maybe Engineering would be better.. or computer science.. but it's mainly mathematical, so I was hoping the people who frequent this forum will be able to help me out.

 

 

 

 

I want to model a system of objects, each with mass m, attached to each other by springs of constant k.

But I have 20 such objects that move in time.

 

My equation involves both an integer-counter x(s) where s is from 1 to 20 (counting the objects), but also x is a function of t (which is continuous).

 

All I want to try and do is solve the relatively simple partial differential equation:

 

d^2x/dt^2 - k/m x'' =0

 

Where x'' relates to differentiation in relation to s, so it's not continuous. I represent it by x'' = x(s+1) + x(s-1) - 2*x(s)

 

And I want to solve for x, symbolically.

 

I know that if I had everything in relation to a single variable (that is, x' and x'' differentials in respect to t) then it's a simple ODE. But.. it's.. not exactly that.

 

I'm stuck, I don't even know how to start with it, really. I assume I'll be needing a "for" loop, but how do I solve each of those equations for the second derivative of x per t, and insert it into a double-index array (matrix, really).

 

It's basically creating a matrix that has functions in it rather than just numbers, and then trying to model that. I'm sure it's possible, I just have no clue how to start it.

 

Even giving me links for further reading would help. Anything.. help!

 

Thanks a lot!

 

~mooey

 

 

Ok so I'm not very knowledge about this, but I don't think you can solve a system of continuous equations without serious computational power. Instead use something like the forward Euler method, or the many built in ODE solvers in matlab: http://www.mathworks.com/help/techdoc/math/f1-662913.html

 

If this isn't what you meant perhaps a diagram of the process you're trying to model would be helpful?

Link to comment
Share on other sites

Anything I say here will be said with the assumption of no prior knowledge as I have no idea what your level of programming is. I am also simply giving my opinion on how this could be approached based on my own experience.

 

1) Matlab has a framework for object oriented programming and this is probably a good time to start using it

 

2) You have several different objects here

- objects of mass m

- spring objects

- physics objects

 

some might say that the spring object and the physics object could be one but I usually keep them separate. It is best to instantiate one spring for every two masses that are connected together.

 

3) Each object requires certain properties with exception to the physics object. The physics object will simply define how two masses will interact with one spring.

 

The spring object, if it is not a fixed object, will require the definition of several properties. The first property to define will be k which is your spring rate constant. If it is not fixed it will also require a matrix representing its position, its velocity can be represented by a vector and a scalar. You will probably also want to define a property that is a measure of its current state of compression. This last bit is particularly important as when you increment per frame status, which is what I was getting from your statements on the discontinuity and differential with respect to the select states of three objects, you will probably want to factor in this information. This is also a good place to define which two masses are being joined by which spring ie. connection_a = mass_01 connection_b = mass_02

 

The mass object will, as the spring object, define a location matrix and a vector and scalar defining velocity. This is all this object will probably define.

 

4) The two objects, the spring and the mass, will require the definition of some get and set functions. Essentially what these will allow you to do is access the current state via the physics object, make the relevant calculations and then set the new appropriate values.

 

The physics object will define several methods that a main routine will be able to call and it will actively execute your algorithm on the current spring object in queue(the one you pass in.) I say call the spring object as this also contains the information in regards to which masses are being acted on as defined above in 3.

 

The proper combination or approach mathematically will depend on your system and I really didn't understand how you were defining your system in your OP.

 

5) Define a routine that loops over your array of springs and that calls the physics object to apply the relevant computation on the values.

 

As always I apologize ahead of time if I misunderstood anything or if I am not making sense or if I simply haven't answered your question. I hope someone can help you in any case, this sounds like a really fun project :)

Link to comment
Share on other sites

I think if you write down your diff. eq. in the form

[math] \frac{d}{dt} \left( \begin{array}{c} \vec x \\ \vec v \end{array} \right)(t) = \left( \begin{array}{c} f(\vec x, \vec v, t) \\ g(\vec x, \vec v, t) \end{array}\right) [/math], where [math]\vec x, \vec v[/math] are the positions and velocities of all the objects,

then numerical algorithms for solving the diff. eq. should jump up immediately. Not exactly sure if this was your problem, though.

Link to comment
Share on other sites

I have either mathematica or matlab. I'm stuck regardless :P If you have any ideas for a mathematica function, that works too! :)

 

 

BTW, thanks for all the help so far, I'm going over them all and will reply soon individually if needed. This is driving me crazy.

Link to comment
Share on other sites

I think if you write down your diff. eq. in the form

[math] \frac{d}{dt} \left( \begin{array}{c} \vec x \\ \vec v \end{array} \right)(t) = \left( \begin{array}{c} f(\vec x, \vec v, t) \\ g(\vec x, \vec v, t) \end{array}\right) [/math], where [math]\vec x, \vec v[/math] are the positions and velocities of all the objects,

then numerical algorithms for solving the diff. eq. should jump up immediately. Not exactly sure if this was your problem, though.

Eventually I will need a numerical solution anyways to produce a graph. The thing is, I'm not entirely sure how to do the above equation (with an array/matrix) in matlab. I'm reading over it, but if anyone has a clue of how to start, I'd appreciate it.

 

 

 

 

Okay, my attempts to do this failed miserably, so I decided to test the equation with a precise x(s).

 

m = 1;

gmma = 1;
x(1) = 1;
x(2) = 2;
dsolve('x(3)=(m/k)*(D2x(1)) - x(1) + 2*x(2)')

 

and I get:

 

 

??? Subscript indices must either be real positive integers or logicals.

Error in ==> dsolve at 173

indx(isalphanumunder(eq_str(indx-1))) = [];

??? Subscript indices must either be real positive integers or logicals.

Error in ==> dsolve at 173

indx(isalphanumunder(eq_str(indx-1))) = [];

 

Which, I guess, makes sense, since I represented x(1) and x(2) as numerical... but.. any ideas on how to do this? anyone? I have x(s), x(s+1) and x(s-1) and D2x (which x-double-dot - the second derivative of x per t).

 

Help.

Link to comment
Share on other sites

In Matlab, you have to initialize your arrays first, something like:

 

x = zeroes(number_of_steps, 1)

(creates an empty number_of_steps x 1 matrix)

 

Then define x(1) = 1 & x(2) = 2

 

Then solve over a descrete set of time steps. You might need a conditional to compute the solution at each step, but I'm not sure.

Link to comment
Share on other sites

  • 2 weeks later...

Okay, I switched back to Mathematica and formulated (with Capn's help, quite a lot of trial and error and some tears) this code:

 

 

m = 1;
k = 1;
gma = 1;
w = Sqrt[k/m];
Remove[x, equations, inits, inits2, soln];
(* x''[s][t]==w^2 * (x[s-1][t]-x[s][t]+2*x[s][t]); *)

equations = Table[x''[s][t] == (w^2) * (x[s - 1][t] + x[s + 1][t] - 2*x[s][t]), {s, 2, 50}]; 
inits =  Table[x[s][0] == s/50, {s, 2, 50}];
inits2 =  Table[x'[s][0] == s/50, {s, 2, 50}];
Join[equations, inits, inits2]
soln = NDSolve[equations, x, {t, 0, 5}]

 

 

But I am getting syntax errors. The values are added in great, but the errors are annoying.

 

If I write it like the above, I get this error:

NDSolve::dvleaf: The function x[1] appears as the head of the expression x[1][t]. >>

 

Whose explanation is:

The initial condition is not given in a valid form.:
NDSolve[{f'[x] + f[x] == 0, f[0][0] == 1}, f, {x, 0, 1}]
[b]NDSolve::dvleaf: The function f[0] appears as the head of the expression f[0][0]. >>[/b]

This shows a valid initial condition for this differential equation:
NDSolve[{f'[x] + f[x] == 0, f[0] == 1}, f, {x, 0, 1}]

 

That, however, makes no sense, 'cause I have f[0] of a bunch of functions, f[1][0], f[2][0], etc. I can't abbreviate to just f[0]!

 

So if I change it to x[t,s] the code breaks on the fact that the derivative and double derivative is unclear to the system (I get an error that tells me that the systems doesn't know if it should derivate in respect to t or s).

 

HEEEEEEELP!

 

If the entire output helps (as in, if anyone wants to go over the way the inits look like), I can post it here. It's just a bit long, I didn't want to flood the post.

 

~mooey

Link to comment
Share on other sites

silly question .... have you tried soln = NDSolve[{ equations, inits, inits2 }, x, {t, 0, 5}] ?? I just think because Table equations is a one dimensional array Join might be simply appending inits and inits2 and so there really is no association. I mean how exactly is NDSolve supposed to know to look up the value of x[0] and x'[0] on the table if the concatenated table consists of what it thinks is x 'cause t is not mentioned in the table creation of init. Maybe add {t,0,0} ..... just thoughts :/

Edited by Xittenn
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.