Mathematica NMinimize Program Running Slow - What's Wrong?

  • Mathematica
  • Thread starter shafieza_garl
  • Start date
  • Tags
    Mathematica
In summary, the problem of the coding is that it would be nicer if you just copied and pasted the code. The code is written in a way that is more difficult to understand.
  • #1
shafieza_garl
20
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: 387
Physics news on Phys.org
  • #2
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.
 
  • #3
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}]
 
  • #4
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
 
  • #5
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.
 

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

1. Why is my NMinimize program running so slow?

There could be several reasons for this. It could be due to the complexity of the objective function, the number of variables being optimized, or the desired precision level. It could also be due to a lack of memory or processing power on your computer.

2. How can I speed up my NMinimize program?

There are a few things you can try to speed up your program. First, you can try using the "Method" option in the NMinimize function to specify a more efficient optimization algorithm. You can also try simplifying your objective function or reducing the number of variables being optimized. Additionally, upgrading your computer's hardware or using a cloud computing service can also help improve performance.

3. Can I parallelize my NMinimize program?

Yes, you can parallelize your NMinimize program to utilize multiple processor cores and speed up the computation. This can be done using the "Parallelization" option in the NMinimize function and specifying the number of parallel kernels to use. However, not all optimization algorithms support parallelization, so you may need to experiment with different methods to find the best one for your specific problem.

4. Is there a way to monitor the progress of my NMinimize program?

Yes, you can use the "EvaluationMonitor" option in the NMinimize function to track the progress of your optimization. This will allow you to see the current values of the variables being optimized and the corresponding objective function value at each iteration. You can also use the "StepMonitor" option to view the optimization steps being taken.

5. What can I do if my NMinimize program keeps failing or giving incorrect results?

If your program keeps failing, it could be due to numerical instabilities in the objective function or constraints. In this case, you may need to adjust the precision level or try using a different optimization algorithm. If the results are incorrect, you should carefully check your objective function and constraints to ensure they are correctly defined. You can also try using the "EvaluationMonitor" and "StepMonitor" options to identify any potential issues with your optimization process.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
254
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
160
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
283
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top