7750 Views 26 Replies Latest reply: Aug 28, 2012 3:52 PM by ptc-4391462
36 posts since
Dec 31, 2011
Currently Being Moderated

## 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

Attachments:
• 1,278 posts since
Jun 8, 2010
Currently Being Moderated
Feb 2, 2012 3:39 AM (in response to ptc-4391462)
Re: For loop output to matrix

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

Alan

Attachments:
• 2,402 posts since
Sep 26, 2010
Currently Being Moderated
Feb 2, 2012 10:38 PM (in response to ptc-4391462)
Re: For loop output to matrix

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

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Feb 2, 2012 11:15 PM (in response to ptc-4391462)
Re: For loop output to matrix

The attached might of of help.

Mike

Attachments:
• 1,278 posts since
Jun 8, 2010
Currently Being Moderated
Feb 3, 2012 4:45 AM (in response to ptc-4391462)
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.

Attachments:
• 1,278 posts since
Jun 8, 2010
Currently Being Moderated
May 22, 2012 1:30 AM (in response to ptc-4391462)
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

Attachments:
• 1,278 posts since
Jun 8, 2010
Currently Being Moderated
May 27, 2012 7:09 AM (in response to ptc-4391462)
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

Attachments:
• 1,278 posts since
Jun 8, 2010
Currently Being Moderated
May 28, 2012 12:57 AM (in response to ptc-4391462)
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

• 74 posts since
Apr 11, 2007
Currently Being Moderated
Aug 28, 2012 4:50 AM (in response to ptc-4391462)
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

• 1,278 posts since
Jun 8, 2010
Currently Being Moderated
Aug 28, 2012 5:36 AM (in response to ptc-4391462)
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

Attachments:

#### More Like This

• Retrieving data ...

#### Legend

• Correct Answers - 3 points