f



Problems solving using Solve

I have a problem suing the following code:

Cone[beta_, v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_] := 
Module[
{betaUsed, vtemp, fx, fy, ex, ey, 
waarde,criticalRC,criticalHeight,xVerplaatsing,ePx2Py2},
      betaUsed = beta ;
      vtemp = v;
      fline[x_] := Tan[betaUsed]x - Tan[betaUsed]vtemp;
      fx = x /. Solve[fline[x] == cd[x,rcCD,cx,cy], x][[1]];
      fy = cd[fx,rcCD,cx,cy];
      criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
      criticalHeight = -criticalRC*fx + fy;
      ex = (-ay + rcAB*ax + criticalHeight)/(rcAB + (-criticalRC));
      ey = ab[ex,rcAB,ax,ay];
      xVerplaatsing = ex - vtemp;
      ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
      waarde = xVerplaatsing/ePx2Py2;
      ArcCos[-waarde]
];

afg = 
Simplify[D[Cone[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy],v], 
TimeConstraint -> 1 ];
Solve[afg== 0, v];

eg.
{ax,ay}= {-44.9376, 36.1678} and {cx,cy}= {17.8329, 122.477} and 
{centerOfMassx,centerOfMassy}={-18.6338,72.3356} and rcAB=-0.166667 and 
rcCD=-0.37931

Doing the Solve[afg== 0, v] takes ages (after an hour I quit it). Is there 
any way I can make it work faster? Is any code in Cone such that it 
unnecessarily makes it run faster? So are there any ways I can try to make 
everything run faster? Using integers it runs, but it takes too long when 
using real cones (made up of four points A B C D and a centerOfMass).

Akil 


0
akomur (15)
8/19/2006 4:51:03 AM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

8 Replies
590 Views

Similar Articles

[PageSpeed] 50

Akil,

Don't do so much in one step. When I plug your data into Cone there are
already problems. First make certain that the Cone routine is working and
giving you what you expect. If it isn't you could add Print statments in
Cone to see what is happening.

Also, I would calculate afg and look at it before plugging into the last
Solve statement.

Also, are you expecting afg to be calculated entirely symbolically? If you
define the data including centerOfMassy, then you couldn't differentiate
with respect to centerOfMassy.

If you have a problem you have to go back and make certain that each step is
working before putting it all together.

David Park
djmp@earthlink.net
http://home.earthlink.net/~djmp/

From: akil [mailto:akomur@wanadoo.nl]


I have a problem suing the following code:

Cone[beta_, v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_] :=
Module[
{betaUsed, vtemp, fx, fy, ex, ey,
waarde,criticalRC,criticalHeight,xVerplaatsing,ePx2Py2},
      betaUsed = beta ;
      vtemp = v;
      fline[x_] := Tan[betaUsed]x - Tan[betaUsed]vtemp;
      fx = x /. Solve[fline[x] == cd[x,rcCD,cx,cy], x][[1]];
      fy = cd[fx,rcCD,cx,cy];
      criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
      criticalHeight = -criticalRC*fx + fy;
      ex = (-ay + rcAB*ax + criticalHeight)/(rcAB + (-criticalRC));
      ey = ab[ex,rcAB,ax,ay];
      xVerplaatsing = ex - vtemp;
      ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
      waarde = xVerplaatsing/ePx2Py2;
      ArcCos[-waarde]
];

afg =
Simplify[D[Cone[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy],v]
,
TimeConstraint -> 1 ];
Solve[afg== 0, v];

eg.
{ax,ay}= {-44.9376, 36.1678} and {cx,cy}= {17.8329, 122.477} and
{centerOfMassx,centerOfMassy}={-18.6338,72.3356} and rcAB=-0.166667 and
rcCD=-0.37931

Doing the Solve[afg== 0, v] takes ages (after an hour I quit it). Is there
any way I can make it work faster? Is any code in Cone such that it
unnecessarily makes it run faster? So are there any ways I can try to make
everything run faster? Using integers it runs, but it takes too long when
using real cones (made up of four points A B C D and a centerOfMass).

Akil



0
djmp (1214)
8/20/2006 8:46:31 AM
akil wrote:
> I have a problem suing the following code:
> 
> Cone[beta_, v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_] := 
> Module[
> {betaUsed, vtemp, fx, fy, ex, ey, 
> waarde,criticalRC,criticalHeight,xVerplaatsing,ePx2Py2},
>       betaUsed = beta ;
>       vtemp = v;
>       fline[x_] := Tan[betaUsed]x - Tan[betaUsed]vtemp;
>       fx = x /. Solve[fline[x] == cd[x,rcCD,cx,cy], x][[1]];
>       fy = cd[fx,rcCD,cx,cy];
>       criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
>       criticalHeight = -criticalRC*fx + fy;
>       ex = (-ay + rcAB*ax + criticalHeight)/(rcAB + (-criticalRC));
>       ey = ab[ex,rcAB,ax,ay];
>       xVerplaatsing = ex - vtemp;
>       ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
>       waarde = xVerplaatsing/ePx2Py2;
>       ArcCos[-waarde]
> ];
> 
> afg = 
> Simplify[D[Cone[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy],v], 
> TimeConstraint -> 1 ];
> Solve[afg== 0, v];
> 
> eg.
> {ax,ay}= {-44.9376, 36.1678} and {cx,cy}= {17.8329, 122.477} and 
> {centerOfMassx,centerOfMassy}={-18.6338,72.3356} and rcAB=-0.166667 and 
> rcCD=-0.37931
> 
> Doing the Solve[afg== 0, v] takes ages (after an hour I quit it). Is there 
> any way I can make it work faster? Is any code in Cone such that it 
> unnecessarily makes it run faster? So are there any ways I can try to make 
> everything run faster? Using integers it runs, but it takes too long when 
> using real cones (made up of four points A B C D and a centerOfMass).
> 
> Akil 
> 
> 
Hi Akil,

I tried to run your code on my system but the definition of the function 
'cd' is missing.

Anyway, since your are interested in numerical solutions, why don't you 
use NSolve in place of Solve? Solve is looking for an analytic solution 
-- a closed form expression that can be quite time consuming to find, 
assuming that such a solution exists at all -- whereas NSolve uses 
directly numerical algorithms.

HTH,
Jean-Marc

0
8/20/2006 8:52:35 AM
definition for cd is cd[input_,rcCD_,cx_,cy_] := rcCD*input + cy - rcCD*cx; 
(which just defines a line of the form y=ax+b)

Using NSolve instead of Solve still takes way too long (quit it after 15 
minutes, do not know if it even will solve it). I have about a 100 cones for 
each part I need to test and the way it is now takes too long.

In my usage all values except beta and v are Reals. Beta is usually the 
range [0, Pi/2] and v is also a Real.

For a solvable solution try:

{ax,ay}= {7,4} and {cx,cy}= {21,11} and
{centerOfMassx,centerOfMassy}={13,7} and rcAB=3 and
rcCD=7/4

That should give a solution, and quite fast at that.

Akil

"akil" <akomur@wanadoo.nl> schreef in bericht 
news:ec65bn$1f8$1@smc.vnet.net...
>I have a problem suing the following code:
>
> Cone[beta_, v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_] 
> :=
> Module[
> {betaUsed, vtemp, fx, fy, ex, ey,
> waarde,criticalRC,criticalHeight,xVerplaatsing,ePx2Py2},
>      betaUsed = beta ;
>      vtemp = v;
>      fline[x_] := Tan[betaUsed]x - Tan[betaUsed]vtemp;
>      fx = x /. Solve[fline[x] == cd[x,rcCD,cx,cy], x][[1]];
>      fy = cd[fx,rcCD,cx,cy];
>      criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
>      criticalHeight = -criticalRC*fx + fy;
>      ex = (-ay + rcAB*ax + criticalHeight)/(rcAB + (-criticalRC));
>      ey = ab[ex,rcAB,ax,ay];
>      xVerplaatsing = ex - vtemp;
>      ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
>      waarde = xVerplaatsing/ePx2Py2;
>      ArcCos[-waarde]
> ];
>
> afg =
> Simplify[D[Cone[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy],v],
> TimeConstraint -> 1 ];
> Solve[afg== 0, v];
>
> eg.
> {ax,ay}= {-44.9376, 36.1678} and {cx,cy}= {17.8329, 122.477} and
> {centerOfMassx,centerOfMassy}={-18.6338,72.3356} and rcAB=-0.166667 and
> rcCD=-0.37931
>
> Doing the Solve[afg== 0, v] takes ages (after an hour I quit it). Is there
> any way I can make it work faster? Is any code in Cone such that it
> unnecessarily makes it run faster? So are there any ways I can try to make
> everything run faster? Using integers it runs, but it takes too long when
> using real cones (made up of four points A B C D and a centerOfMass).
>
> Akil
>
> 


0
akomur (15)
8/20/2006 8:53:35 AM
Sorry,

ab[input_,rcAB_,ax_,ay_] := rcAB*input + ay - rcAB*ax;

I hope I did not forget anything else. I need the derivative because it 
gives me the maximum or minimum answer for Cone for each beta, and that is 
needed for further steps in my project.

The code itself works quite well. Tested it extensively on cones, and double 
checked te answers. Also checked if the maximums were where they were 
supposed to be, and that also checked out.

A picture of an example is at:
http://home.wanadoo.nl/akomur/example.png

Akil


"akil" <akomur@wanadoo.nl> schreef in bericht 
news:ec65bn$1f8$1@smc.vnet.net...
>I have a problem suing the following code:
>
> Cone[beta_, v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_] 
> :=
> Module[
> {betaUsed, vtemp, fx, fy, ex, ey,
> waarde,criticalRC,criticalHeight,xVerplaatsing,ePx2Py2},
>      betaUsed = beta ;
>      vtemp = v;
>      fline[x_] := Tan[betaUsed]x - Tan[betaUsed]vtemp;
>      fx = x /. Solve[fline[x] == cd[x,rcCD,cx,cy], x][[1]];
>      fy = cd[fx,rcCD,cx,cy];
>      criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
>      criticalHeight = -criticalRC*fx + fy;
>      ex = (-ay + rcAB*ax + criticalHeight)/(rcAB + (-criticalRC));
>      ey = ab[ex,rcAB,ax,ay];
>      xVerplaatsing = ex - vtemp;
>      ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
>      waarde = xVerplaatsing/ePx2Py2;
>      ArcCos[-waarde]
> ];
>
> afg =
> Simplify[D[Cone[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy],v],
> TimeConstraint -> 1 ];
> Solve[afg== 0, v];
>
> eg.
> {ax,ay}= {-44.9376, 36.1678} and {cx,cy}= {17.8329, 122.477} and
> {centerOfMassx,centerOfMassy}={-18.6338,72.3356} and rcAB=-0.166667 and
> rcCD=-0.37931
>
> Doing the Solve[afg== 0, v] takes ages (after an hour I quit it). Is there
> any way I can make it work faster? Is any code in Cone such that it
> unnecessarily makes it run faster? So are there any ways I can try to make
> everything run faster? Using integers it runs, but it takes too long when
> using real cones (made up of four points A B C D and a centerOfMass).
>
> Akil
>
> 


0
akomur (15)
8/21/2006 7:29:04 AM
akil wrote:
> definition for cd is cd[input_,rcCD_,cx_,cy_] := rcCD*input + cy - rcCD*cx; 
> (which just defines a line of the form y=ax+b)
> 
> Using NSolve instead of Solve still takes way too long (quit it after 15 
> minutes, do not know if it even will solve it). I have about a 100 cones for 
> each part I need to test and the way it is now takes too long.
> 
> In my usage all values except beta and v are Reals. Beta is usually the 
> range [0, Pi/2] and v is also a Real.
> 
> For a solvable solution try:
> 
> {ax,ay}= {7,4} and {cx,cy}= {21,11} and
> {centerOfMassx,centerOfMassy}={13,7} and rcAB=3 and
> rcCD=7/4
> 
> That should give a solution, and quite fast at that.

No: the definition of the function 'ab' is missing.

Jean-Marc

0
8/21/2006 7:41:12 AM
akil schrieb:
> I have a problem suing the following code:
> 
....

> 
> eg.
> {ax,ay}= {-44.9376, 36.1678} and {cx,cy}= {17.8329, 122.477} and 
> {centerOfMassx,centerOfMassy}={-18.6338,72.3356} and rcAB=-0.166667 and 
> rcCD=-0.37931
> 
> Doing the Solve[afg== 0, v] takes ages (after an hour I quit it). Is there 
> any way I can make it work faster? Is any code in Cone such that it 
> unnecessarily makes it run faster? So are there any ways I can try to make 
> everything run faster? Using integers it runs, but it takes too long when 
> using real cones (made up of four points A B C D and a centerOfMass).
> 
> Akil 
> 
> 

Hi Akil,

you should delay the evaluation of afg:

cd[input_, rcCD_, cx_, cy_] := rcCD*input + cy - rcCD*cx;
(* just a guess: *)  ab = cd;
cone[beta_, v_, rcAB_, rcCD_, ax_, ay_, cx_, cy_, centerOfMassx_, 
centerOfMassy_] :=
   Module[{fx, fy, ex, ey, waarde, criticalRC, criticalHeight, 
xVerplaatsing, ePx2Py2},
     fx = x /. Solve[Tan[beta]*(x - v) == cd[x, rcCD, cx, cy], x][[1]];
     fy = cd[fx, rcCD, cx, cy];
     criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
     criticalHeight = (-criticalRC)*fx + fy;
     ex = Together[(-ay + rcAB*ax + criticalHeight)/(rcAB - criticalRC)];
     ey = Together[ab[ex, rcAB, ax, ay]];
     xVerplaatsing = ex - v;
     ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
     waarde = xVerplaatsing/ePx2Py2;
     ArcCos[-waarde]
   ];
afg[beta_, v_, rcAB_, rcCD_, ax_, ay_, cx_, cy_, centerOfMassx_, 
centerOfMassy_] :=
    Numerator[Together[D[cone[beta, v, rcAB, rcCD, ax, ay, cx, cy, 
centerOfMassx, centerOfMassy], v]]];

Timing[example = afg[beta, v, 3, 7/4, 7, 4, 21, 11, 13, 7]]
{0.031*Second,
  -2*(-300713 + 531069*Tan[beta] + 19551*v*Tan[beta] + 57513*Tan[beta]^2
  - 88578*v*Tan[beta]^2 + 2793*v^2*Tan[beta]^2 - 306269*Tan[beta]^3 +
  76059*v*Tan[beta]^3 - 3759*v^2*Tan[beta]^3 + 49*v^3*Tan[beta]^3)
}

First[Timing[solVector = v /. Solve[example == 0, {v}]; ]]
0.016*Second

Plot[Evaluate[Re[solVector]], {beta, Pi/12, Pi/2}, PlotStyle -> {Red, 
Green, Blue}, AxesOrigin -> {Pi/4, 0}];
(* Graphics omitted *)

v0 = First[solVector]
(1/7)*(179 - 133*Cot[beta])

Groetjes,
   Peter

0
petsie (836)
8/21/2006 7:47:16 AM
If exact numbers give a quick solution, how about using
exact (rational) approximations to the floating-point
parameters?

{ax,ay} = Rationalize[{-44.9376, 36.1678}];
{cx,cy} = Rationalize[{17.8329, 122.477}];
{centerOfMassx,centerOfMassy} = Rationalize[{-18.6338,72.3356}];
rcAB = Rationalize[-0.166667];
rcCD = Rationalize[-0.37931];

In[25]:=
{ax,ay,cx,cy,centerOfMassx,centerOfMassy,rcAB,rcCD}

Out[25]=
   28086   180839  178329  122477    93169   180839    166667      37931
{-(-----), ------, ------, ------, -(-----), ------, -(-------), -(------)}
    625     5000   10000    1000     5000     2500     1000000     100000


N[] can convert the results back to floating-point.

Bruce Miller
Technical Support
Wolfram Research, Inc.
support@wolfram.com
http://support.wolfram.com/



>
> definition for cd is cd[input_,rcCD_,cx_,cy_] := rcCD*input + cy -
> rcCD*cx;
> (which just defines a line of the form y=ax+b)
>
> Using NSolve instead of Solve still takes way too long (quit it after 15
> minutes, do not know if it even will solve it). I have about a 100 cones
> for
> each part I need to test and the way it is now takes too long.
>
> In my usage all values except beta and v are Reals. Beta is usually the
> range [0, Pi/2] and v is also a Real.
>
> For a solvable solution try:
>
> {ax,ay}= {7,4} and {cx,cy}= {21,11} and
> {centerOfMassx,centerOfMassy}={13,7} and rcAB=3 and
> rcCD=7/4
>
> That should give a solution, and quite fast at that.
>
> Akil
>
> "akil" <akomur@wanadoo.nl> schreef in bericht
> news:ec65bn$1f8$1@smc.vnet.net...
>>I have a problem suing the following code:
>>
>> Cone[beta_,
>> v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_]
>> :=
>> Module[
>> {betaUsed, vtemp, fx, fy, ex, ey,
>> waarde,criticalRC,criticalHeight,xVerplaatsing,ePx2Py2},
>>      betaUsed = beta ;
>>      vtemp = v;
>>      fline[x_] := Tan[betaUsed]x - Tan[betaUsed]vtemp;
>>      fx = x /. Solve[fline[x] == cd[x,rcCD,cx,cy], x][[1]];
>>      fy = cd[fx,rcCD,cx,cy];
>>      criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
>>      criticalHeight = -criticalRC*fx + fy;
>>      ex = (-ay + rcAB*ax + criticalHeight)/(rcAB + (-criticalRC));
>>      ey = ab[ex,rcAB,ax,ay];
>>      xVerplaatsing = ex - vtemp;
>>      ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
>>      waarde = xVerplaatsing/ePx2Py2;
>>      ArcCos[-waarde]
>> ];
>>
>> afg =
>> Simplify[D[Cone[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy],v],
>> TimeConstraint -> 1 ];
>> Solve[afg== 0, v];
>>
>> eg.
>> {ax,ay}= {-44.9376, 36.1678} and {cx,cy}= {17.8329, 122.477} and
>> {centerOfMassx,centerOfMassy}={-18.6338,72.3356} and rcAB=-0.166667 and
>> rcCD=-0.37931
>>
>> Doing the Solve[afg== 0, v] takes ages (after an hour I quit it). Is
>> there
>> any way I can make it work faster? Is any code in Cone such that it
>> unnecessarily makes it run faster? So are there any ways I can try to
>> make
>> everything run faster? Using integers it runs, but it takes too long
>> when
>> using real cones (made up of four points A B C D and a centerOfMass).
>>
>> Akil
>>
>>
>
>


0
brucem1 (33)
8/21/2006 7:58:33 AM
I do

ab[input_,rcAB_,ax_,ay_] := rcAB*input + ay - rcAB*ax;
cd[input_,rcCD_,cx_,cy_] := rcCD*input + cy - rcCD*cx;

Cone[beta_, v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_] :=
Module[
{betaUsed, vtemp, fx, fy, ex, ey,
waarde,criticalRC,criticalHeight,xVerplaatsing,ePx2Py2},
      betaUsed = beta ;
      vtemp = v;
      fline[x_] := Tan[betaUsed]x - Tan[betaUsed]vtemp;
      fx = x /. Solve[fline[x] == cd[x,rcCD,cx,cy], x][[1]];
      fy = cd[fx,rcCD,cx,cy];
      criticalRC = (centerOfMassy - fy)/(centerOfMassx - fx);
      criticalHeight = -criticalRC*fx + fy;
      ex = (-ay + rcAB*ax + criticalHeight)/(rcAB + (-criticalRC));
      ey = ab[ex,rcAB,ax,ay];
      xVerplaatsing = ex - vtemp;
      ePx2Py2 = Sqrt[xVerplaatsing^2 + ey^2];
      waarde = xVerplaatsing/ePx2Py2;
      ArcCos[-waarde]
];

afg[beta_,v_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_]:=
Numerator[Together[D[Cone[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy],v]]];

TopCurve[b_,rcAB_,rcCD_,ax_,ay_,cx_,cy_,centerOfMassx_,centerOfMassy_]:=Module[
{afgeleiden,solutions,curve},
  afgeleiden=afg[beta,v,rcAB,rcCD,ax,ay,cx,cy,centerOfMassx,centerOfMassy];
 solutions = Solve[afgeleiden == 0, v];
 curve = solutions[[2]];
 Return[ v /. curve /. beta -> b]
];

now, which seems to work. Tested all solutions, because Solve[afgeleiden ==
0, v]; gets up to 9 solutions in most cases. I used to take the second, and
the second still seems to be the real max/min curve.

Now I can continue with the next step, looking for intersections with anothe
complex functions as TopCurve.

Thanks guys.



0
akomur (15)
8/25/2006 9:37:22 AM
Reply: