COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to do numerical computations?

• Email
• Follow

```Hello,

I have written some mathematica code which contains some complicated functions.The functions involve integrations and iterations in the intermediate steps. When I run the code it takes a hell lot of time. I do not know what mathematica is doing inside, actually I have another code in fortran that takes only a few minutes to do all the calculations but there are other problems like it dosent work for some parameter range.

Is there a method in which I can ask mathematica to give me only the numerical values just like any executable program does.

Thanks.
TaB

```
 0

See related articles to this posting

```"bagarti" <bagarti@gmail.com> wrote in message
news:hov834\$94q\$1@smc.vnet.net...
> Hello,
>
> I have written some mathematica code which contains some complicated
> functions.The functions involve integrations and iterations in the
> intermediate steps. When I run the code it takes a hell lot of time. I do
> not know what mathematica is doing inside, actually I have another code in
> fortran that takes only a few minutes to do all the calculations but there
> are other problems like it dosent work for some parameter range.
>
> Is there a method in which I can ask mathematica to give me only the
> numerical values just like any executable program does.
>
> Thanks.
> TaB
>

Symbolic computation is very useful for the analysis and the investigation
of a problem analytically. (funny thing to say, because how else would one
do this other by symbolic computation?)

But when it comes to solving the problem on a large scale, and for speed, I
think you want to switch to numerical computation.  This could be as easily
as warping N[] around an expression, or having the initial parameters be
floating point values instead of rational (i.e. for the values of the
variables used, write 1.0/2 or 0.5 instead of 1/2 and write 1.0 instead of
1).

There is a cascading effect here, if one variable is floating point, then
the calculation of the expression which this variable is in will be done
numerically and will be normally faster.

Also, try to use functions such as NIntegrate and NSolve and NDSolve instead
of the symbolic versions.

I wrote a small finite elements program last year in Mathematica, and it was
very slow when the number of elements and the stiffness matrix became large,
this is because at first I kept everything in symbolic form, so all the
terms were in symbolic form until the end when I substitute numerical values
for the variables.

When I started to make some of the changes above, and to assign a numerical
values for the variables early on, the program ran much much faster than
before.

--Nasser

```
 0

```On Mar 31, 11:27 pm, bagarti <baga...@gmail.com> wrote:
> Hello,
>
> I have written some mathematica code which contains some complicated functions.The functions involve integrations and iterations in the intermediate steps. When I run the code it takes a hell lot of time. I do not know what mathematica is doing inside, actually I have another code in fortran that takes only a few minutes to do all the calculations but there are other problems like it dosent work for some parameter range.
>
> Is there a method in which I can ask mathematica to give me only the numerical values just like any executable program does.
>
> Thanks.
> TaB

Many analytical functions with in Mathematica have numerical counterparts for
example Integrate[] has NIntegrate[]. You can use those to get
numerical results. If your code is very slow this could have many
reasons and without giving an example of what you are doing it will be
hard to help you. Keep in mind that Mathematica is interpreted and not compiled
Fortran code.

I hope this helps.

Manfred

```
 0

```I'm taking a guess that you have a fairly complicated Block[] or
Module[] with an integration that involves some parameters.
Mathematica is hitting that integration and trying to evaluate it
symbolically, which can be time consuming.  I think what you want to
do is prevent that integration from evaluating until it gets numerical
values for the parameters.  One way to do this is to create a function
out of the integration that requires numerical input.  A trivial
example is

int[a_?NumericQ] := NIntegrate[ Sin[x] , {x, 0, a} ]

This way, when you say

b = int[c]

where c has no value, Mathematica will hold it as int[c] and not
attempt a symbolic evaluation.  Later, you can say c = 3., and the
numerical integration can be evaluated quite quickly.

Daniel

```
 0

```Hi,as you do not give an example I can not be sure. But it sounds like
the ever recurring problem of a function being evaluated with symbolic
arguments. To ensure that the computations are done numerically, you may
e.g. define:
f[x_?NumericQ]:=...
Daniel

On 31.03.2010 12:27, bagarti wrote:
> Hello,
>
> I have written some mathematica code which contains some complicated functions.The functions involve integrations and iterations in the intermediate steps. When I run the code it takes a hell lot of time. I do not know what mathematica is doing inside, actually I have another code in fortran that takes only a few minutes to do all the calculations but there are other problems like it dosent work for some parameter range.
>
> Is there a method in which I can ask mathematica to give me only the numerical values just like any executable program does.
>
> Thanks.
> TaB
>

--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh@metrohm.com>
Internet:<http://www.metrohm.com>

```
 0