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

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Mixing ODE and find

athurin
4-Participant

Mixing ODE and find

Hi,

I was wondering if there was any way to use find given a set of differential equation. Here is a (simple) example : charge of an RC system.

input voltage, and capacitor value are known. I want to find the resistor value such that, after a given time Tx, the capacitor voltage is Vx.

Obviously, this example is simple enough to solve manually, but could this be applied to much more complex systems where manual solving is not an option.

1 ACCEPTED SOLUTION

Accepted Solutions

The old command line solvers can be incorporated into programs, then used in solve blocks.

View solution in original post

9 REPLIES 9

We must use Odesolve.

I will try to solve it!

athurin
4-Participant
(To:ValeryOchkov)

Doesn't odesolve need a value for R ?

If it was possible to solve the ODE symbolically, then use find the the value of R, that would do the trick for this particular example, but symbolic solve sin't always an option, especially in areas where there are lots of ifs in the equations.

The old command line solvers can be incorporated into programs, then used in solve blocks.

athurin
4-Participant
(To:Fred_Kohlhepp)

That's a great solution ! Thank you very much !

Fred, I just had a look a your sheet again to see whats the problem because it should work OK the way you had set it up. I think I found the cause for the wrong value for R. Its the match() function and a simple precision problem which can be coped with in several ways:

1) Decrease TOL to something like 10^-10

2) Replace the calculation line for dx to take the mean of all indices wich match would return

1.png

3) Omit the match function completely and use some sort of interpolation (linear should be good enough here)

2.png

BTW, any specific reason you had set up your ODE using the charge function instead of the voltage function directly? A clever idea to use the constant R as second "function" to solve for. Incidentally we can do without that trick if we resort to a small program which creates the vector D for rxfixed on the fly. See attached file.

Personally I still prefer the odesolve approach but think its a pity we don't have any control anymore in Prime as of the algorithm used.

Here is a different approach using two solve blocks, one with odesolve, the second with find.

I get a different value for the resistor (785.57 ohm, compared to Fred's 710.94 ohm) , though, haven't checked why.

EDIT: Just used the easy to find exact solution of the ODE and found the correct value for the restistor again with 785.57 ohm! Not sure whats the problem in Fred's sheet.

1.png

EDIT2: Found the problem in Fred's approach. It was a simple precison problem because of the rather small time values used. Match() would find a couple of matches within the given tolerance and the first one of this list is not the best one. See my other reply to Fred's post for details and solution.

athurin
4-Participant
(To:Werner_E)

Interesting, your approach is much closer to what I "instinctively" wanted to do, except that your syntax works !

Both solutions are really interesting, I will have to spend some time understanding in details how all that works, but this does exactly what I was looking for.

Many thanks to both of you !

Adrien Thurin wrote:

Interesting, your approach is much closer to what I "instinctively" wanted to do, except that your syntax works !

Yes, you approach was quit intuitive but unfortunately thats no the way Prime works.

The points (apart from using the boolean = instead of the assignment := for equations) to consider are:

  • If you use an unknown in a solve block you have to solve for it even if you just use as a shortcut and are not interested in. So its OK if you use I(t) as you had done, but then you would have to provide a initial condition I(0)=.. and must let Odesolve solve for I(t). You have to treat is as a system of ODEs. Thats the reason I have omitted I(t) - if I(t) is needed after you have found the correct value of the resistor it can very easy be defined using the now (numerically) "known" V.out(t).
  • The second point is that you tried to use just one solve block which would be nice if we could do so, But because V.out(t) is an unknown you must search for it and because its a function we cannot use find() like for finding R but odesolve. This fact forces us to use two different solve blocks. Fred showed a different approach and solved for V.out using one of the standalone command line solvers.

One thing concerning odesolve vs. command line solver. Odesolve in a solve block might be more attractive because it uses a more intuitive syntax. The command line solvers might have advantages if used inside of programs. In Prime we have one additional reason for using the command line solver because here we have control over the algorithm used (we have a handful of different solvers to chose from). In Mathcad we could chose different algorithms for odesolve, too, but in Prime we have the "i do it for you" appraoch (which admittedly works pretty well most of the time).

The results of both approaches are slightly different. The command line solvers return a matrix of scalars. First column are t-values, second column function values of first solved for function, etc. If you need a true function with values inbetween, you have to use some kind of interpolation as I did in my third way to avoid the precision problem in Fred's sheet. The result of odesolve is pretty much the same, but here you don't a fubnction name as result, not the discrete values themselves. Internally this is represented by the very same marix values coupled with an automatic interpolation but the user has no access to those matrix values and does not know which kind of interpolation is applied. Again that mechanism works pretty well but sometimes users have the urge for a bit more control over the situation and use the command line solvers for that reason.

athurin
4-Participant
(To:Werner_E)

Thanks for the explanation. I will keep that in mind in the future !

Top Tags