Re: ODE/DAE help

66 views
Skip to first unread message

John Hedengren

unread,
Apr 25, 2013, 12:33:27 AM4/25/13
to apmo...@googlegroups.com
Ben,

The items o1, o2, i1, and i2 were not defined. When I tried to run the model in the web-interface it produced an error like:

 *** Error in syntax of function string: Invalid element: o2
Position: 7                   
 (1-(1+o2/g)*exp(o1*e/f))*o2-(o1*m1)
       ?

I uncommented your o1 and o2 definitions and then added some definitions for i1 and i2 (please verify that these are correct). You can get syntax highlighting here if you'd like to see color coding for the APM models.

Model Constants a = 10 b = .1 c = .1 d = .001 e = .03 f = .00001 g = .0000001 h = .0000000001 i = 10 j = 300 k= 600 End Constants Parameters i1 = i i2 = i End Parameters Variables l1 = 1 l2 = 1 m1 = 10 m2 = 500 n1 = 600000 n2= 200000 o1 = 10^(-5) ! This would be my typical guesses for numerical solution of the algebraic equation o2 = 10^(-6) ! This would be my typical guesses for numerical solution of the algebraic equation End Variables Equations o1 = h*(i*j*(m1*exp(-o1/(.5*(l1*c*k/d)^(.5)*f/c))-m2*exp(o1*e/f))/(1+(g/o1)*(exp(o1*e/f)-exp(-o1/(.5*(l1*c*k/d)^(.5)*f/c))))-n1+n2) ! non-linear algebraic 1 (1-(1+o2/g)*exp(o1*e/f)) * o2 = o1*m1 ! non-linear algebraic 2 b*l1 * $m1 = -(2*o2+2*o1*b^2*m1) ! ODE 1 b*l2 * $m2 = (2*o2+2*o1*b^2*m2) ! ODE 2 i1*c * $n1 = -30*d*a*i1^2*c ! ODE 3 i2*c * $n2 = -30*d*a*i2^2*c ! ODE 4 $l1 = 2*o1*b ! ODE 5 $l2 = -2*o1*b ! ODE 6 End Equations End Model

Another thing that I did (optional) is rearrange the equations to eliminate some of the divisions. This generally helps avoid divide by zero and makes the solution more robust.

I've attached a MATLAB and Python driver to dynamically simulate the model. You may get better results once you fix my guesses with i1 and i2. The dynamic mode of APM is normally described with respect to time but you can label it as distance instead. You can specify the time (or distance) points in the CSV file along with other distance-varying inputs to your model.

-John Hedengren

On Wednesday, April 24, 2013 6:08:07 PM UTC-6, Ben wrote:
Hi,

I am trying to solve a system of first order ODEs and nonlinear algebraic equations. These ODEs are with respect to position (x) as opposed to time. I am having some trouble getting them to solve using apmonitor. The model is shown below. Can anyone suggest where I might be going wrong? 

I am used to having to choose a 'guess' for the algebraic equations when solving alone in Matlab, is it necessary to instead provide an initial condition as a variable here? 

How might I vary the system with respect to position (x) instead of time (t)?

Any help is greatly appreciated. 

Many thanks,
Ben

Model

Constants
a = 10 
b = .1
c = .1 
d = .001 
e = .03
f = .00001 
g = .0000001
h = .0000000001
i = 10
j = 300
k= 600
End Constants

Variables
l1 = 1
l2 = 1
m1 = 10
m2 = 500
n1 = 600000 
n2= 200000 
! o1 = 10^(-5) This would be my typical guesses for numerical solution of the algebraic equation
! o2 = 10^(-6) This would be my typical guesses for numerical solution of the algebraic equation
End Variables

Equations
o1 = h*(i*j*(m1*exp(-o1/(.5*(l1*c*k/d)^(.5)*f/c))-m2*exp(o1*e/f))/(1+(g/o1)*(exp(o1*e/f)-exp(-o1/(.5*(l1*c*k/d)^(.5)*f/c))))-n1+n2) ! non-linear algebraic 1
o2 = o1*m1/(1-(1+o2/g)*exp(o1*e/f)) ! non-linear algebraic 2
$m1 = -(2*o2+2*o1*b^2*m1)/(b*l1)  ! ODE 1
$m2 = (2*o2+2*o1*b^2*m2)/(b*l2)  ! ODE 2
$n1 = -(30*d/(i1*c))*a*i1^2*c ! ODE 3
$n2 = -(30*d/(i2*c))*a*i2^2*c ! ODE 4
$l1 = 2*o1*b ! ODE 5
$l2 = -2*o1*b ! ODE 6
End Equations


End Model
membrane.zip

Ben

unread,
Apr 25, 2013, 8:48:31 PM4/25/13
to apmo...@googlegroups.com

Hi John,

Thanks very much for your detailed reply. Unfortunately the i1 and i2 were typos, as they should have been l1 and l2. I've changed i1 and i2 to l1 and l2, and eliminated i1 and i2 from the model parameters. I've now tried running the file through Matlab, and through the online APMonitor interface, and I get a successful result in Matlab (and through the online interface when I choose "Sequential, Simulate"). However, when I try to add values for changing x in the csv file, an error is returned, "Error: Did not converge to a solution, Attempt to reference field of non-structure array." The .csv is has the same name as the .apm file. I've attached the .csv file here in case you have any insight. 

Thanks again,

Ben


Model
 Constants
  a = 10 
  b = .1
  c = .1 
  d = .001 
  e = .03
  f = .00001 
  g = .0000001
  h = .00000001
  i = 10
  j = 300
  k= 600

 End Constants

 Variables
  l1 = 1
  l2 = 1
  m1 = 500
  m2 = 10
  n1 = 600000 
  n2= 200000 
  o1 = 10^(-5)  ! This would be my typical guesses for numerical solution of the algebraic equation
  o2 = 10^(-6)  ! This would be my typical guesses for numerical solution of the algebraic equation
 End Variables

 Equations
  o1 = h*(i*j*(m1*exp(-o1/(.5*(l1*c*k/d)^(.5)*f/c))-m2*exp(o1*e/f))/(1+(g/o1)*(exp(o1*e/f)-exp(-o1/(.5*(l1*c*k/d)^(.5)*f/c))))-n1+n2) ! non-linear algebraic 1
  (1-(1+o1/g)*exp(o1*e/f)) * o2 = o1*m1 ! non-linear algebraic 2
  b*l1 * $m1 = -(2*o2+2*o1*b^2*m1)  ! ODE 1
  b*l2 * $m2 = (2*o2+2*o1*b^2*m2)  ! ODE 2
  $n1 = -30*d*a*l1 ! ODE 3
  $n2 = -30*d*a*l2 ! ODE 4
  $l1 = 2*o1*b ! ODE 5
  $l2 = -2*o1*b ! ODE 6
 End Equations
End Model

membrane_test.csv

John Hedengren

unread,
Apr 26, 2013, 10:41:02 AM4/26/13
to apmo...@googlegroups.com

Ben,

 

You need to leave the “time” header, even though you are integrating with respect to distance.

 

 

You may also need to include additional distance points because APM will solve only at the requested time/distance values that you specify. Once you have the sequential simulation working, you can also change to imode=5 for dynamic reconciliation with data for parameter estimation or imode=6 for dynamic optimization. For smaller problems, imode=4 (simultaneous simulation) can sometimes be faster than your current mode which is imode=7.

 

You can label the results as distance in your plots or other results…

 

 

-John Hedengren

--
--
APMonitor user's group e-mail list.
- To post a message, send email to apmo...@googlegroups.com
- To unsubscribe, send email to apmonitor+...@googlegroups.com
- Visit this group at http://groups.google.com/group/apmonitor
 
---
You received this message because you are subscribed to the Google Groups "apmonitor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apmonitor+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

membrane.zip
Reply all
Reply to author
Forward
0 new messages