Skip to main content
14-Alexandrite
September 11, 2025
Solved

RK4, manually ?? Odesolve?? RKfixed??

  • September 11, 2025
  • 3 replies
  • 1290 views

Hello

in the attached sheet there is a problem of mechanics of a damped system

Unfortunately I couldn't use odesolve, so I tried RKfixed, but again I got an error message from Mathcad. So I did a manual RK4๐Ÿ˜…

Could someone help me use the Prime RKfixed? it would be more elegant than what I did๐Ÿ˜‚

and also to stop the loop when "v" (speed)  becomes negative,

after that continuing the iterations no longer makes physical sense, at least when x<0. Since at that point the load has passed through the damper
 
 
 

img.png

also, all attempts I made with the units failed

 

Differential equations are not easy for me๐Ÿ˜‚

Best answer by Werner_E

Here is a way to turn the solve block with odesolve into a function of zeta, so the outcome of different values of zeta could be compared side by side.

Werner_E_0-1757729403705.png

Werner_E_1-1757729425214.png

Not sure why the line x(t) <- x(t) in "getfunctions" is necessary, but if its omitted Prime throws an error.

EDIT: Just gave it a try and that's a bug in Prime 10 which is fixed in Prime 11.

 

Prime 10 sheet attached

3 replies

23-Emerald V
September 11, 2025

Hi,

 

It would be helpful if you could post your 'failures' as this might allow us to aid you in getting the results you want.

 

However, looking at your worksheet, I'm guessing that you're having difficulties in defining the differential equation in a form suitable for rkfixed?

 

Your DE can be expressed as shown in the image below.  It seems to get the same values, anyway!

 

2025 09 11 D.png

 

I've attached a Mathcad Prime 11 worksheet with the rkfixed solution.

 

Stuart

25-Diamond I
September 11, 2025

As Stuart already supplied the RKfixed solution, here for completeness sake the solution using a solve block with "odesolve".

As you can see in the plots the results pretty much match the results your program returns.

If you are interested in the position/time where velocity v gets zero, you could use the 'root' function (see at the bottom of the pic).

Werner_E_0-1757629214955.png

 

Prime 10 sheet attached

 

EDIT: Because you also asked how to stop the iteration in your program when velocity gets negative: You could use and if-statement and trigger the break command. This will leave the loop. The value your program should return therefore must be placed outside and below the for-loop. There is no need for a local variable R.

Werner_E_0-1757630619927.png

 

 

25-Diamond I
September 11, 2025

Because you wrote that you had trouble using units.

I applied units

Werner_E_0-1757634678094.png

and both your program as well as the solve block with 'odesolve' work OK.

Werner_E_2-1757635056039.png

 

But because you used variable "m" for mass you have to take great care when you type "m" to use the correct label - either 'Variable' if you mean mass or "Unit' if you mean meter. After you define your mass Prime will default to 'Variable' when you type m and you would have to change it to 'Unit' manually (easily done by pressing Ctrl+U).

 

Prime 10 sheet attached

 

 

XDN14-AlexandriteAuthor
14-Alexandrite
September 12, 2025

Good

The model with odesolve is much more elegant than my manual method. Yes, I need to stop my loop when a certain condition is reached, here v<0. However, I need to keep the matrix of results calculated up to this stopping point. So I duplicated the loop by keeping the index of the stop, then restarting the loop with an index limited to the stopping value. I left the calculations for x and f , because if necessary I can choose a conditional stop other than v , but it's detail

 

This is a crude method, but it gives the matrix of results I need to extract the extrema.

XDN_0-1757669439807.png

However, by varying the parameters zeta or t, my manual model diverges from the odesolve model. The larger zeta becomes, the stronger the decorelation becomes.๐Ÿค”in fact it happens quite early 

so I assume an error in my model and I prefer to trust your model

 

One last thing, why can't mathcad evaluate max(x(t))? At the end I need to get the extrema because I'm looking for parameters minimizing F

 

Thanks

 

19-Tanzanite
September 13, 2025

I decided to use this problem to see if I could develop a 4th order, fixed step, Runge-Kutta routine for use with Prime Express (as Express versions have no ODE solvers or programmable facilities).  The solution Iโ€™ve developed is very inefficient, but the inefficiency isnโ€™t really noticeable for this toy problem.  The result compares well with the analytical solution.  See attached.

DampedHM.png

 

Alan

23-Emerald V
September 13, 2025

I thought I'd posted an Express-friendly (well, less-hostile) version of an ancient Mathsoft Mathcad 7 ODE library.  Can't find it, so I probably didn't.

 

Attached is a very much tidied-up version of the MPE7 worksheet I found skulking in my folders.   It covers the first five Runge-Kutta fixed, single step ODE solvers (rk1 and rk2 are equivalent to simple and improved Euler, respectively).   rkfixed seems like overkill for the problem at hand.

 

2025 09 13 A.png

 

Stuart

19-Tanzanite
September 13, 2025

Wish I'd been aware of this earlier!  Would have saved me a lot of effort!!

 

Hmm!  Just tried to run your file ODELibE11 - SingleStep.mcdx, and it doesn't work in my version of Prime 11 Express.  I get complaints about the attempted use of premium features!

 

Alan