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

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

ODE with two functions of time

MarkBuckton
4-Participant

ODE with two functions of time

There may be a trick to solving this ODE but I am at a loss. All I can assume is that it is something to do solving 2 functions simultaneously but in my view the only linkage between these functions is time otherwise I believe they should be independent in terms of depth h because the depths in the tank and the weir are only related indirectly by way of inflow. As you can see I am getting an error. The problem looks simple enough i.e. fill a tank with water and then drain the water out at the bottom of the tank at a slower rate than the infill rate. By definition the tank should fill with water to a certain height.The complexity is that the inflow is by way of a weir. The inflow from the weir increases with the depth of flow. I(h). The outflow is a function of the depth of water in the tank Qout(hw). But as you can see the ODE fails but I don't know why?

Can someone show me how this can be done?

Thanking you in advance.

Mark

1 ACCEPTED SOLUTION

Accepted Solutions

it looks like you can use ODE solve blocks as functions

Yes you can turn any solve block in a function. E.g. you may provide the necessary guess value for a given-find() block as parameter, etc. YOu may also rewrite the block to accomodate for a weir height which is not constant over time. in the block replace h by h(t). To test it, define a function hh(x):=150mm and define h(index)w150:=h(index)w(hh). You get the same function h(index)w150 as before.

provided the variable that you want to find is not also a function.

What the odesolveblock is trying to find IS a function (h(index)w) - its not a variable!?

Why is it that you had to create a function with 2 arguments.

Its a new limitation of Prime or I haven't found out how to do it otherewise. I'm not used to Prime as I don't use it for anything serious. In Mathcad 15 you would not have to define that auxiliary function, you could use h(index)w() the same way as h.w() directly.

I would have thought that the ODE solve block function hw(h) contained the solution vectors for both the variable Hw and t. However when I try to calculate this function in isolation I get the following:

What does all that all mean looks like some error code?

What odesolve returns is not just a solution vector - its that vector of discrete function values coupled with an implicit interpolation. The result of odesolve can be used numerically like a normal function. Without turning the block into a function, when we wrote hw:=odesolve(...) you can use the result like a function, typing hw(12 s)=... or plotting it like you had defined it analytically. So when you simply type hw= Prime tells you in its charming way that its a function - thats not an error message. The same happens now when you evaluate h(index)w(150mm)=... So to evaluate that function for time 10s it would be necessary to write h(index)w(150mm)(10s)=... The first parenthesis being the argument for the solve block function which returns a function and the second parenthesis for the argiment of this new function. This syntax doesn't work, though but in MC15 we are allowed to write h(index)(150mm,10s)=.., in Prime we are not allowed to do so and that the reason for that auxiliary function.

The "error" you get in the attached pic is simply eyplained. h at that moment is not undefined but one of your range variables. The range acts like an implicit for-loop feeding one value after the other in the solve block and getting back a function each time. So it presents you the result telöling you that the expression you want to evaluate is a list of functions.

It seems strange that the Solve Block does not return a user accessible solution vectors i.e. ones that can be inspected directly.

If you want use the solution vectors and do the interpolation to turn it into a function yourself, you have to use one of the standalone ODE solvers. That way you have also control over the method used (we could chose the method in MC15 for odesolve, too, but this feature is gone with Prime), odesolve uses Adams/BDF and I am not sure if it switches automatically to another method if necessary. The help mentions in "Algorithms for ODEs" the other methods, but I think this is simply a left overer from turning the MC15 help, where we could chose the method) into the Prime help. The syntax for the standalone solvers may be a bit cumbrous but if you want to see the vectors thats the way to go. Look them up in the help or any other documentation you have at hand. http://www.ptc.com/cs/help/mathcad_hc/prime3_hc/mprime/ode_solvers.html?queryId=144d98fae6b

Is not this the whole purpose of an ODE i.e. we are solving for a function

Are we? Then you have to use odesolve as you already did. This gives you a function but not the data vector behind it. Basically odesolve gives you the same as Adams() or BDF() followed by an lspline interpolation.

View solution in original post

8 REPLIES 8

But as you can see the ODE fails but I don't know why?

Because you use I(h(t)), but h is at that time not a function of time but a range variable. Thats the reason for the error.

You haven't set up an equation showing the depth of flow in dependence of time.

MarkBuckton
4-Participant
(To:Werner_E)

Werner;

Is not the Qout(hw) also not a function of time? As it too uses a range variable to kick off the calculation. I can see physically at least how Qout(hw) is weakly related to time as it takes time to fill the tank to each new value of hw which in turn increases the outflow as Qout(hw) gets larger with depth. However I see no way of linking the inflow with time as it seems independent except to say that an increasing depth of flow over the weir gives a greater inflow. All I am doing is describing how the inflow varies with h (aka a hydraulic rating curve) but I don't see a way to link this with time?

Although what I believe may be incorrect I was of the belief that the ODE solve block was the structure that created the function of time for both functions inflow and outflow. Anyway it should produced function(s) that describe how the height of the water varies in the tank with time and as a consequence one can then calculate the outflow function which may at times be leveling off or in some cases decreasing if the outflow proved to be greater than inflow.

So where to now? because my feeling is that the inflow although it varies with the height of water above the weir seems not to be coupled with the volumetric storage capacity of the tank. Or maybe once again describing this situation mathematically eludes me.

Regards, Mark

The problem in your solveblock is that you use a function h(t) which you did not define anywhere. This is why the solve block is throwing an error. If you use a function you either have to define it prior or solve for it in an ode solve block (like you do with h.w).

As you write you thought that odesolve would take care of h(t) I ma ask - how?? If you want odesolve to solve for h(t), too, you have to set up the block to solve for both functions. That way you would have tp provide one additional equation and an initial value.

I guess you must make clear to yourself whats given and what you are searching for.

OK second try.

In your sheet you have setup three equations,

  • Inflow I dependent on water height above weir h, I(h)
  • Outflow Q dependent on water height hw in tank, Q.out(hw)
  • differential equation showing the change of hw dependent on I, Q.out and an area A over time with an initial value of hw(0)=0

As you say is h not dependent on time, also there is no feedback setup how the inflow I would affect h. So we have to assume that h is either a constant value or its a function of time h(t) which is otherwise independent from the system and so would have been to be provided. As this is not the case we can assume h as being a constant. Given your ranges you are obviously interested in the results for h varying from 0 to 200mm.

Ranges! - they are good for plots and if you want to create a table of values from functions you already have. Please don't expect odesolve to work with a range for h!

This all set its clear that the water height hw in the tank is dependent on two parameters, the (constant) height h and the time t elapsed since the first water drop got in the tank. So hw is a hw(h,t)! Its a 2-dimensional surface. You may draw hw over time for different constant values of h (that is you draw slices of that surface) or plot a 3D surface plot or a contour plot. See attached.

So the main question still applies: What is given, what do you know or want to assume of your model and what are you interested in at the end. You may also be interested in a plot of the height hw over the height h for a constant time ("how high is the water in the tank after 15 minutes for different weir heights h").

One approach usually doesn't work at all: "Given nothing, searching for all".

weir1.png

weir2.png

weir3.png

Let me again underline that I haven't checked the correctness of your model/your equations in any way. I used your model as is.

MarkBuckton
4-Participant
(To:Werner_E)

Werner;

Thank you very much. Your approach is ingenious so it looks like you can use ODE solve blocks as functions provided the variable that you want to find is not also a function. This is so powerful and a good thing to know and remember. The other trick you have used eludes me, however. Why is it that you had to create a function with 2 arguments. I would have thought that the ODE solve block function hw(h) contained the solution vectors for both the variable Hw and t. However when I try to calculate this function in isolation I get the following:

What does all that all mean looks like some error code? If it is an error why then can a programming block strip out the solution vectors and why did you need to do this in the first place? It seems strange that the Solve Block does not return a user accessible solution vectors i.e. ones that can be inspected directly. Is not this the whole purpose of an ODE i.e. we are solving for a function or am I missing some subtle point.

Kind regards, Mark

it looks like you can use ODE solve blocks as functions

Yes you can turn any solve block in a function. E.g. you may provide the necessary guess value for a given-find() block as parameter, etc. YOu may also rewrite the block to accomodate for a weir height which is not constant over time. in the block replace h by h(t). To test it, define a function hh(x):=150mm and define h(index)w150:=h(index)w(hh). You get the same function h(index)w150 as before.

provided the variable that you want to find is not also a function.

What the odesolveblock is trying to find IS a function (h(index)w) - its not a variable!?

Why is it that you had to create a function with 2 arguments.

Its a new limitation of Prime or I haven't found out how to do it otherewise. I'm not used to Prime as I don't use it for anything serious. In Mathcad 15 you would not have to define that auxiliary function, you could use h(index)w() the same way as h.w() directly.

I would have thought that the ODE solve block function hw(h) contained the solution vectors for both the variable Hw and t. However when I try to calculate this function in isolation I get the following:

What does all that all mean looks like some error code?

What odesolve returns is not just a solution vector - its that vector of discrete function values coupled with an implicit interpolation. The result of odesolve can be used numerically like a normal function. Without turning the block into a function, when we wrote hw:=odesolve(...) you can use the result like a function, typing hw(12 s)=... or plotting it like you had defined it analytically. So when you simply type hw= Prime tells you in its charming way that its a function - thats not an error message. The same happens now when you evaluate h(index)w(150mm)=... So to evaluate that function for time 10s it would be necessary to write h(index)w(150mm)(10s)=... The first parenthesis being the argument for the solve block function which returns a function and the second parenthesis for the argiment of this new function. This syntax doesn't work, though but in MC15 we are allowed to write h(index)(150mm,10s)=.., in Prime we are not allowed to do so and that the reason for that auxiliary function.

The "error" you get in the attached pic is simply eyplained. h at that moment is not undefined but one of your range variables. The range acts like an implicit for-loop feeding one value after the other in the solve block and getting back a function each time. So it presents you the result telöling you that the expression you want to evaluate is a list of functions.

It seems strange that the Solve Block does not return a user accessible solution vectors i.e. ones that can be inspected directly.

If you want use the solution vectors and do the interpolation to turn it into a function yourself, you have to use one of the standalone ODE solvers. That way you have also control over the method used (we could chose the method in MC15 for odesolve, too, but this feature is gone with Prime), odesolve uses Adams/BDF and I am not sure if it switches automatically to another method if necessary. The help mentions in "Algorithms for ODEs" the other methods, but I think this is simply a left overer from turning the MC15 help, where we could chose the method) into the Prime help. The syntax for the standalone solvers may be a bit cumbrous but if you want to see the vectors thats the way to go. Look them up in the help or any other documentation you have at hand. http://www.ptc.com/cs/help/mathcad_hc/prime3_hc/mprime/ode_solvers.html?queryId=144d98fae6b

Is not this the whole purpose of an ODE i.e. we are solving for a function

Are we? Then you have to use odesolve as you already did. This gives you a function but not the data vector behind it. Basically odesolve gives you the same as Adams() or BDF() followed by an lspline interpolation.

MarkBuckton
4-Participant
(To:Werner_E)

Werner;

Thank you for your thorough answer. I still have a lot to learn regarding Mathcad. Is there a book that explains mathcad in more depth. All the ones that I have seen don't get into "nity grity" explainations such as yours. In my view therefore they are not much better than the MC's help file. The trial and error process is good way to learn but too slow particularly for someone that has not done a lot of math for many years.

Regards, Mark

So my way to learn Mathcad was mainly trial and error as I have used Mathcad (not much) when it was version 2.0 running under DOS. After 2.5 (still DOS) I haven't used the program heavily until MC2000 and then 2001.

I always liked the help system which comprehensively covered most topics, the quicksheets with easy to follow examples and the various e-books integrated in the program. Part of it was transformed into Prime's help but I find that help rather cumbersome and if there are any helpful examples, they are to many mouseclicks away IMHO.

Being an offline reader I was particularily often using the printed documentation and complete reference which was well structured and laid out and complete up to version 11. After V11 the printed doc (which always was also supplied as pdf, too) was radically trimmed and then it was only delivered in electronic pdf version. And now with Prime we don't have any documentation whatsoever, so Prime is a step backward even in that respect.

So if you are lucky (and I am sure you will be) you may find a pdf of an older version of Mathcad (preferably V11) somewhere on the net to download and study; most content will still apply to Prime, while some features may not (yet?) be available in Prime.

I have also read/flipped through a couple of books about Mathcad, but as you wrote, most are rather basic, beginning with a lengthy and detailed explanation of the various menu items and how to enter and edit expressions. Especially most German books I know of are horrible boring in that way.

One book I really liked as it takes a hands on approach, was up to the point and also covered more advanced topics better than other books I knew, was "Pritchard - Mathcad, A Tool for Engineering Problem Solving". http://www.mhhe.com/engcs/general/best/pritchard.mhtml

http://www.amazon.com/Mathcad-Engineering-Problem-Solving-accompany/dp/0077509404

Not a tips and tricks kind of book but it may apply for"nitty-gritty". I have the 2nd edition which was written when Mathcad 14 was the current version and it seems there never was a 3rd ed. The book came with CD and license for the student edition of MC14 which may be responsible for the relatively high price for a 200 pages paperback. I wasn't in need for the CD but the book was worth the price for me nevertheless.

Many tricks and methods not covered in manuals or books could be learned by scanning the old Mathsoft collab forum, which was so poorly moved over to this community forum here. There are some topics which always come up repeatedly, the front runners being "difference between range variable and vector" and "turning a solve block into a function". 😉

Top Tags