Mathematica NMinimize Program Running Slow - What's Wrong?

  • Context: Mathematica 
  • Thread starter Thread starter shafieza_garl
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around a Mathematica program using NMinimize that is running slowly. Participants are exploring potential coding issues, particularly focusing on the definitions of functions and the structure of integrals within the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports that their NMinimize program is taking a long time to solve and seeks help with the coding problem.
  • Another participant suggests sharing the code using specific formatting to facilitate analysis.
  • A participant provides the original code and highlights the use of several parameters and functions defined through integrals.
  • One participant recommends changing the assignment operator from = to := for delayed assignments in the definitions of functions R, F, and H to ensure proper substitution of variables.
  • This same participant rewrites the code using NIntegrate instead of Integrate, suggesting that it may improve performance for numerical solutions.
  • Another participant identifies that the calculation of H[x0, x1] + H[x1, x2] + H[x2, x3] is significantly contributing to the slow performance of the minimization process.
  • This participant notes that changing variable names and simplifying expressions did not yield significant improvements in execution time.
  • Concerns are raised about the structure of the nested integrals and the potential for optimizing them, though uncertainty remains about the correctness of these modifications.

Areas of Agreement / Disagreement

Participants express differing views on the causes of the slow performance, with some focusing on the definition of the function H and others on the overall structure of the code. There is no consensus on a definitive solution or approach to resolve the performance issues.

Contextual Notes

Participants note that the numerical integration may have inherent challenges, and the specific behavior of the function H is not oscillating or discontinuous, which raises questions about its computational efficiency.

shafieza_garl
Messages
20
Reaction score
0
i try run my program for NMinimize.it took a long time to solve.What is the problem of my coding?
 

Attachments

  • Untitled-3.jpg
    Untitled-3.jpg
    35.2 KB · Views: 448
Physics news on Phys.org
It would be nicer if you just copy/pasted the code, using
Code:
 and [/ code] tags like
[code]
this

Then I can plug it into my own Mathematica and its easier to analyse.
 
ok..this is the code...
Code:
 a = 1/4; b = 4044 + 4.0/9; c = 66 + 2.0/3; d = 
 15.0/1000; x0 = 0.0; x3 = 5.0; n = 3; K = 9.0; c2 = 0.1; \[Alpha] = \
0.25; p2 = 35.0; c1 = 25.0; i2 = 0.04; i1 = 0.05; \[Theta] = 0.03;
R[x_, y_] = 
  Integrate[
   a*(E^(-d*t)*(E^(d*t)*(b - c*t) + E^(d*y)*(-b + c*y)))^2, {t, x, 
    y}]; 
F[x_, y_] = 
  Integrate[
   a*(E^(d*t)*(E^(d*t)*(b - c*t) + E^(d*y)*(-b + c*y)))^2, {t, 
    y*(1 - \[Alpha]) + x*\[Alpha], y}];
H[x_, y_] = 
  0.5*Integrate[
    Integrate[
     E^(d*t)*(6 + 
        t)*((E^(d*t)*(b - c*t) + E^(d*y)*(-b + c*y))^2)^0.5, {t, x, 
      t}], {t, x, y + x*\[Alpha] - y*\[Alpha]}];
NMinimize[{n*K + c2*(R[x0, x1] + R[x1, x2] + R[x2, x3]) + 
   c1*i1*(F[x0, x1] + F[x1, x2] + F[x2, x3]) - 
   p2*i2*(H[x0, x1] + H[x1, x2] + H[x2, x3]), 
  x0 <= x1 && x1 <= x2 && x2 <= x3}, {x0, x1, x2, x3}]
 
First of all, you should replace the assignment by a delayed assignment (replace = by := in R, F and H), otherwise x and y don't get substituted.

The main problem, however, is in your definition of H, I think.
Personally, I am not fond of an expression like
Code:
Integrate[f[t], {t, x, t}]

Since you seem only interested in numerical solutions, I rewrote your code to the following:

Code:
R[x_?NumericQ, y_?NumericQ] := 
 NIntegrate[
  a*(E^(-d*t)*(E^(d*t)*(b - c*t) + E^(d*y)*(-b + c*y)))^2, {t, x, y} //
    Evaluate]
F[x_?NumericQ, y_?NumericQ] := 
 NIntegrate[
  a*(E^(d*t)*(E^(d*t)*(b - c*t) + E^(d*y)*(-b + c*y)))^2, {t, 
    y*(1 - \[Alpha]) + x*\[Alpha], y} // Evaluate]
H[x_?NumericQ, y_?NumericQ] := 
  0.5*NIntegrate[
    E^(d*t)*(6 + 
       t)*((E^(d*t)*(b - c*t) + E^(d*y)*(-b + c*y))^2)^0.5, {\[Tau], 
     x, y + x*\[Alpha] - y*\[Alpha]}, {t, x, \[Tau]}];
FindMinimum[{n*K + c2*(R[x0, x1] + R[x1, x2] + R[x2, x3]) + 
   c1*i1*(F[x0, x1] + F[x1, x2] + F[x2, x3]) - 
   p2*i2*(H[x0, x1] + H[x1, x2] + H[x2, x3]), 
  x0 <= x1 <= x2 <= x3}, {{x1, 1}, {x2, 3}}]

It is not really working now, but that seems to be due some problems with the numerical integration... I don't have time to solve those now, but maybe this will help you speed things up
 
The H[x0, x1] + H[x1, x2] + H[x2, x3] is what is taking the time. Without that the minimization completes in a fraction of a second.

Changing the t to tau doesn't seem to help.

And he has things like ((expr)^2)^0.5, sort of like what they teach in FORTRAN when they are trying to simulate absolute value, but changing the 0.5 to 1/2 or using Sqrt or Abs, none of these make any significant change in the time. I've tried to understand what he is doing well enough to turn this into a double integral, rather than a nested pair of integrals, but I cannot be certain I've done that correctly, especially with his reuse of names, and this still doesn't seem to make any difference.

Getting rid of the decimal points he keeps putting back in each time he posts this question does speed things up by perhaps 1/3, but that is not addressing the overwhelming time sink that his H[x,y] calculation is.

H[x,y] isn't oscillating, discontinuous or going to infinity and appears to almost be a plane over the region he is interested in.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K