Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica problem

  1. Aug 23, 2010 #1
    i try run my program for NMinimize.it took a long time to solve.What is the problem of my coding?
     

    Attached Files:

  2. jcsd
  3. Aug 24, 2010 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    It would be nicer if you just copy/pasted the code, using
    Code (Text):
     and [/ code] tags like
    [code]
    this
     
    Then I can plug it into my own Mathematica and its easier to analyse.
     
  4. Aug 25, 2010 #3
    ok..this is the code...
    Code (Text):
     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}]
     
     
  5. Aug 25, 2010 #4

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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 (Text):
    Integrate[f[t], {t, x, t}]
    Since you seem only interested in numerical solutions, I rewrote your code to the following:

    Code (Text):

    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
     
  6. Aug 26, 2010 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Mathematica problem
  1. Mathematica problem (Replies: 0)

  2. Mathematica problems (Replies: 2)

  3. Mathematica Problem (Replies: 3)

  4. Mathematica problem (Replies: 2)

Loading...