Mathematica Mathematica NMinimize Program Running Slow - What's Wrong?

  • Thread starter Thread starter shafieza_garl
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion focuses on optimizing a Mathematica program that uses NMinimize for numerical minimization. The original code suffers from performance issues, primarily due to the definition of the function H, which involves complex nested integrals that significantly slow down computation. Suggestions include changing assignments to delayed assignments for proper variable substitution and replacing Integrate with NIntegrate for numerical efficiency. The rewritten code aims to address these issues, but still encounters challenges with numerical integration speed. It is noted that simplifying expressions, such as avoiding unnecessary square roots, has minimal impact on performance. The primary bottleneck remains the H function, which, despite being well-defined, continues to be a time-consuming element in the minimization process.
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: 431
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.
 
Back
Top