26 Replies Latest reply: Aug 28, 2012 3:52 PM by remslie

# For loop output to matrix

I am a beginner with programming in Mathcad. I am having trouble understanding how to use FOR Loops to dump results to a matrix from which I can extract values to plot in a graph. I wish to solve a non-linear equation and evaluate this function for a range of values and then graph the results in the form of a Bifurcation Diagram. The non-linear equation becomes chaotic at certain input force values.

I have constructed a Runge Kutta 4th order function to integrate the equation and develop the displacement and velocity time waveforms - no problem. As a first step I want to evaluate the equation for a range of forces and store the velocity (Y2) versus time (Y0) waveforms in a matrix. As a second stage I want to sample the velocity waveform after it has stabilised (after say 100 cycles) at periopdic intervals to see if the velocity amplitude remains stable or whether a chaotic behaviour occurs.

The attached Mathcad file has the integration front end and my attempt at a nested FOR loop to index the force amplitude and store the velocity (Y2) values in a matrix. The matrix output only seems to store the last iteration. I would appreciate if someone could get me over this first hurdle to evaluate all velocity waveforms for the range of forces 'A".

Extracting data to a new matrix after every N1 data points would be the next step and then plotting this data as aBifurcation Diagram.

Any assiatnce would be appreciated.

Cheers

Ross

• ###### Re: For loop output to matrix

You had the assignment to Xi,j in the wrong place.

Alan

• ###### Re: For loop output to matrix

Alan,

Thankyou for your fast response. That was embarassingly easy to fix. I guess I am still somewhat confused with the syntax for nesting these FOR loops.If you are aware of an online reference that provides more on the topic than just the tutorial I would be keen to check it out. Thanks again

• ###### Re: For loop output to matrix

Some example can be found in the attachment (it's include material from "Mathcad Help" [hotkey F1]).

• ###### Re: For loop output to matrix

The attached might of of help.

Mike

• ###### Re: For loop output to matrix

Good example, Mike. From which e-book this are?

• ###### Re: For loop output to matrix

There from 'Inside Mathcad - Programming'.

Mike

• ###### Re: For loop output to matrix

In case you might be interested, I've attached an alternative solution method using Mathcad's built-in ODE solver (at the end of the attached file). There seems little point in reinventing the wheel by programming your own integration routines (except for educational reasons).

Alan

Message was edited by: Alan Tidied up the graph.

• ###### Re: For loop output to matrix

Many thanks for your multiple responses. The tutes and ebook extracts provide valuable demos. I appreciate your generousity in spending time educating the "beginners".The ODE solver is a very elegant means of solving this problem. Its amazing how much calculation can be generated with so few lines of program code.

Cheers Ross

• ###### Re: For loop output to matrix

You're welcome.

• ###### Re: For loop output to matrix

Yes, the ODE solver is a very elegant way of solving the problem, but I must admit that I have found it the most difficult to get my head around.

Mike

• ###### Re: For loop output to matrix

Alan,

I have been tinkering with a program that includes your ODEsolve suggestion -it is very effeicient!

I am trying to include a non-linear backlash (flatspot) into the displacement variable. As I understand the ODE solve block only allows first derivative experssions on LHS. Is there a way I can include a function thant modifies x(t) that includes the backlash - it is included in the attached sheet as g(t) just above the solve block.

Any ideas would be greatly appreciated.

Cheers

ross

• ###### Re: For loop output to matrix

Ross Emslie wrote:

Alan,

I have been tinkering with a program that includes your ODEsolve suggestion -it is very effeicient!

I am trying to include a non-linear backlash (flatspot) into the displacement variable. As I understand the ODE solve block only allows first derivative experssions on LHS. Is there a way I can include a function thant modifies x(t) that includes the backlash - it is included in the attached sheet as g(t) just above the solve block.

Any ideas would be greatly appreciated.

Cheers

ross

Something like this?

Alan

• ###### Re: For loop output to matrix

Alan,

Thanks yet again.I didn't believe it could be that simple!This forum certainly allows you to push new boundaries.

I will keep tinkering.

Cheers

ross

• ###### Re: For loop output to matrix

Alan,

If I could burden you just once more!

I have taken your odesolve method that outputs the vector xv(A) and the final step in solving my problem is to introduce a second degree of freedom with another 2nd order equation.

I have increased the vector from 2x1 to 4x1 to output the extra displacement and velocity variables (see attached)but there is now an error I don't know how to eliminate. From other programs I have seen there does not seem to be a restriction as to how many variables can be solved for in the odesolve block - so can you advise what the problem might be in my solveblock.

Thanks

Ross

• ###### Re: For loop output to matrix

Ross Emslie wrote:

Alan,

If I could burden you just once more!

I have taken your odesolve method that outputs the vector xv(A) and the final step in solving my problem is to introduce a second degree of freedom with another 2nd order equation.

I have increased the vector from 2x1 to 4x1 to output the extra displacement and velocity variables (see attached)but there is now an error I don't know how to eliminate. ...

The equations work in principle, but the solutions grow extremely rapidly, so the system crashes before your end-time. The attached shows they work over a shorter period. However, I wonder if the ODE's are formulated correctly as the solutions basically explode!

Alan

• ###### Re: For loop output to matrix

Alan,

Thanks. At least I had the right idea. De-bugging programs is a nightmare for the learner! I will track through the equation derivations and check all my constants. I'll let you know what I find.

Cheers

ross

• ###### Re: For loop output to matrix

Ross Emslie wrote:

... I will track through the equation derivations and check all my constants. ...

Also check carefully the signs of all the terms in dv/dt and du/dt.

Alan

• ###### Re: For loop output to matrix

Alan,

I have found a couple of constants that were incorrect. The program now gives reasonable looking time waveforms so I now can undertake some sensitivity studies to explore the problem further.

Many thanks again for your assistance. Let me know if you are ever in Sydney and I'll buy you a beer!

Cheers

Ross

• ###### Re: For loop output to matrix

Alan,

I continue to tinker with this problem and after debugging the last version of the program I have been getting sensible answers. I have now made the problem incrementally more compliacted by adding an addditional 2 degrees of freedom to my system of ODE's.

Whilst keeping essentially the same structure as your solve block I am now getting an error for N1 stating it needs to be a vector - I cannot fathom why. Any suggestions would be apprectaied.

Cheers

ross

• ###### Re: For loop output to matrix

Ross Emslie wrote:

Alan,

I continue to tinker with this problem and after debugging the last version of the program I have been getting sensible answers. I have now made the problem incrementally more compliacted by adding an addditional 2 degrees of freedom to my system of ODE's.

Whilst keeping essentially the same structure as your solve block I am now getting an error for N1 stating it needs to be a vector - I cannot fathom why. Any suggestions would be apprectaied.

Cheers

ross

You enter the loop with N as a vector, but change it within the loop into a matrix (Ni,j), hence the complaint about it (no longer) being a vector.

Stuart

• ###### Re: For loop output to matrix

Stuart and Alan,

Many thanks guys for highlighting what, in hindsight, is now an obvious and very poor choice for a matrix name. I can now move forward to explore the chaos (double meaning!) in my non-linear problem.

Cheers

Ross

• ###### Re: For loop output to matrix

Stuart is correct. You are trying to construct a matirx N, forgetting that you already have a vector N. Since the matrix is being constructed within a loop involving N, Mathcad is getting confused! Rename the matrix N to, say, NN and all is well - see attached.

Alan