Re: Differential equations error with MathLink/JLink
- To: mathgroup at smc.vnet.net
- Subject: [mg27903] Re: Differential equations error with MathLink/JLink
- From: Ralph Gauges <ralph.gauges at eml.villa-bosch.de>
- Date: Fri, 23 Mar 2001 04:31:25 -0500 (EST)
- Organization: Tiscali / Nacamar
- References: <98vh54$8sr@smc.vnet.net> <99chhr$8bi@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Todd Gayley wrote: > On 17 Mar 2001 06:17:56 -0500, Ralph Gauges <ralph.gauges at eml.villa-bosch.de> wrote: > > >Hi, > > > >I am rather new to mathematica and I have some problems solving > >differential equations using any mathlink product. > >First I tried the python module for mathematica and when that didn't > >work, I tried JLink with the same result. > >The problem is as follows. Whenever I try to specify a differential > >equation like > > > >x'[t]==2*x[t]+3 > > > >and want this to be evaluated, I get the error message : > > > >MathLinkException occurred: MLGet out of sequence > > > >I don't think I made a mistake since I just took the SampleProgram.java > >from the JLink packet and changed one line: > > > > //ml.evaluate("2+2"); > > ml.evaluate("x'[t]==2*x[t]+3"); > > > >Other stuff works just fine. > >Has anyone else had these problems before and found a solution? > >Thank you very much for the help > > > >Ralph > > Ralph, > > The result you will get from ml.evaluate("x'[t]==2*x[t]+3") is exactly what you will get > if you type this into a Mathematica notebook--the original expression returned unmodified. > Perhaps you want to use ml.evaluate("DSolve[x'[t]==2*x[t]+3, x[t], t]"). > > Getting an "MLGet out of sequence" error in a J/Link (or MathLink) program generally means > that you are trying to read the result in an inappropriate way. You just changed one line > in SampleProgram.java, but you also need to change the way the result is read. That spot > in the sample program calls getInteger() to read the result, but obviously an integer is > not what comes back from your calculation. > > If you just want to display the result on the screen, you are better off using > evaluateToOutputForm() instead of evaluate(), as evaluateToOutputForm() will return the > result as a formatted string. If you want the result as an expression, so that you can > operate on it in Java (not likely in this case), then you should use evaluate() and either > pick apart the result with a series of "get" calls (using getNext() to tell you how to > read each piece), or you can use getExpr() to read the whole result as an Expr and use the > Expr methods to decipher its structure (this is the recommended technique). > > Remember, though, that it is usually easier to ask Mathematica to return exactly what you > need rather than pick apart a complicated result on the Java side. In other words, > something like this: > > ml.evaluate("x[t] /. Flatten[DSolve[x'[t]==2*x[t]+3, x[t], t]]"); > > although this result will still be a symbolic expression: > Plus[Rational[-3,2],Times[Power[E,Times[2,t]],C[1]]]. > > --Todd Gayley > Wolfram Research Thanks, this getExpr seems to work a lot better than to fiddle around with all the get methods. Now I will have to see how I get this over to python again since I haven't planned on switching to java. I guess trying to reproduce this error in java was not the best idea, but since the python module is not supported I thought I could try. In the python module the get Functions are mapped to python functions and I traced the program flow to the error in the C files. There was a getNext statement which stated that the next object was a function, but the following getFunction call caused the error described. Maybe I can rewrite the module to use the getExpr instead of getting the individual parts. Thanks a lot for the help Ralph