cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Translate the entire conversation x

RK4, manually ?? Odesolve?? RKfixed??

XDN
14-Alexandrite
14-Alexandrite

RK4, manually ?? Odesolve?? RKfixed??

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😂

17 REPLIES 17
StuartBruff
23-Emerald IV
(To:XDN)

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

Werner_E
25-Diamond I
(To:XDN)

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

 

 

Werner_E
25-Diamond I
(To:Werner_E)

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

 

 

XDN
14-Alexandrite
14-Alexandrite
(To:Werner_E)

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

 

XDN
14-Alexandrite
14-Alexandrite
(To:XDN)

Actually, this doesn't seem to be a correlation problem, taking your graph and evaluating "F0"

XDN_1-1757670302318.png

just a display problem?

Werner_E
25-Diamond I
(To:XDN)

Not sure why you use that crude way to get the matrix just up to the point where v gets negative.

What I suggest in my answer above should return the very same result but without having to run the algorithm twice:

Werner_E_0-1757677754128.png

Not that now you have a program consisting of two statements (see the vertical double bar at the far left) - the for-loop and the augment function after it.

 

Can't comment on the discrepancy between your program and the odesolve solution without seeing it. Had not looked into your implementation of RK but assumed it was correct as the results pretty much matched (apart from the start in F(t)).

 

max(x(t)) does not work because the "max" function does not work on functions but only on vectors/matrices.

Using functions you could look at the position where the derivative v(t) is zero and I already showed how to do that using the "root" function..

 

I am not sure which "display problem" you are referring to. If you are taking about the difference right at the start after t=0 you may try to set N to a higher value like 10^4. Because F(t) can't be evaluated at t=0s for the reasons already described you will not see the vertical line at t=0 which is seen when plotting the vectors returned by your function or by using RKfixed as shown by Stuart.

Here is what I get (using your original values) when I set N=10^5 and also increase the steps used in odesolve from the default value 10^3 up to 10^5:

Werner_E_1-1757679132567.png

 

 

XDN
14-Alexandrite
14-Alexandrite
(To:Werner_E)

Yes, of course, I'm stupid. I was fooled by the simple bar in my initial program, so Break stopped the loop and consequently returned the last value of F. Now it's OK. Okay, for max(F(t)) not applicable, is there a built-in function to determine the extremum Fmax? (Ok for root of the curve for x(t) et v(t))

Werner_E
25-Diamond I
(To:XDN)

As far as I know there is no built-in function to determine the maximum or minimum of a given function.

As already written you sometimes can use the zero of the derivative to find a local extremum. Or you could sample the function with a tiny step value, thus creating a vector you could use min or max on.

Find attached a small utility function with rudimentary error handling which returns the minimum and maximum of a given function f within a given interval (x1 to x2). The interval is divided in N intervals and the appropriate function values are collected in a vector. Then min and max is used.

Werner_E_1-1757681916766.png

 

EDIT:

Added a second version of minmax which is a bit more efficient. It does not create large matrices and does not make use of the min and max functions. It seems to run a bit faster and also provides better precison. Although precision can be increased for both version by setting TOL at at lower value.

Werner_E_0-1757684666009.png

 

Prime 10 sheet attached

 

StuartBruff
23-Emerald IV
(To:XDN)

For your information, it is possible to pass parameters, such as zeta, to rkfixed by adding them to the DE as a value with a differential of zero.  For example,

 

2025 09 12 C.png

 

Stuart

Werner_E
25-Diamond I
(To:StuartBruff)

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

AlanStevens
19-Tanzanite
(To:XDN)

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

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

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

Sorry about that.  I've got little idea what code might be helpful to somebody.  I'd forgotten all about the worksheet until "ODE" and "Express" triggered a vague sense of déjà vu, and I did a search in my archives.

 

Stuart

Werner_E
25-Diamond I
(To:AlanStevens)


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!

Its because the calculation of RKF uses rkfixed, which is a premium feature.

Replace RKF in the plot at the abscissa by RK1 or RK4 and delete RKF at the ordinate.

Werner_E_0-1757804621881.png

 

StuartBruff
23-Emerald IV
(To:Werner_E)

Ah, well spotted. I’d forgotten to remove the rkfixed, which I’d used to verify I hadn’t messed up anything (too much) in my MP11 migration and tidy-up.

Stuart
AlanStevens
19-Tanzanite
(To:Werner_E)


@Werner_E wrote:

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!

Its because the calculation of RKF uses rkfixed, which is a premium feature.

Replace RKF in the plot at the abscissa by RK1 or RK4 and delete RKF at the ordinate.

 

 


Yes! That works.

Announcements

Top Tags