Re: Differential equations error with MathLink/JLink
- To: mathgroup at smc.vnet.net
- Subject: [mg27861] Re: Differential equations error with MathLink/JLink
- From: tgayley at wolfram.com (Todd Gayley)
- Date: Thu, 22 Mar 2001 04:30:03 -0500 (EST)
- Organization: Wolfram Research, Inc.
- References: <98vh54$8sr@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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