1
$\begingroup$

I'm trying to write a function that return a randomly created rational polynomial function that has a discontinuities points.

I already have a function that generates random rational polynomial (MakeRationalPolynomial), but, as they are randomly generated, I want to keep creating those until a have one that has at least one discontinuity point.

I tried the code in a normal notebook without calling a function and it worked, but if I incapsulate that code in a function body it does not work as expected.

What can be the cause of this behaviour?

MakePolynomial[n_Integer,x_Symbol] := Module[{z,c}, z = RandomChoice[{-1,1}] RandomInteger[{1,10}];
    c = Table[RandomInteger[{-10,10}], {n}];
    FromDigits[Reverse[AppendTo[c,z]],x]]

MakeRationalPolynomial[n_,n2_,x_] := 
    Module[{num,den,f},
        num=MakePolynomial[n,x];
        den=MakePolynomial[n2,x];
        Evaluate[num]/Evaluate[den]
        ]
    

CreateDomain[f_,x_] := Module[{pdisc,pdiscList}, 
                            pdisc = Solve[FunctionSingularities[f,x]];
                            pdiscList = Table[Last[Last[pdisc[[i]]]],
                         {i,Length[pdisc]}]
                        ]

CreateDiscontinuitiesPoly[] :=
  n = 1;
  While[n > 0,
     f = MakeRationalPolynomial[2, 2, x];
     domain = CreateDomain[f, x];
     If[Length[domain] > 0 , n = 0; Print[f], n = 1]
   ]

Button["Generate Poly", CreateDiscontinuitiesPoly[]]
```
$\endgroup$
0

1 Answer 1

6
$\begingroup$

You may wish to disambiguate your description as it seems you're creating rational functions made up of polynomials. Below I modularized CreateDiscontinuityPoly and in the Button I add the Print@ syntax to print the rational expression.

    MakePolynomial[n_Integer, x_Symbol] := Module[{z, c},
   z = RandomChoice[{-1, 1}] RandomInteger[{1, 10}];
   c = Table[RandomInteger[{-10, 10}], {n}];
   FromDigits[Reverse[AppendTo[c, z]], x]
   ];

MakeRationalPolynomial[n_, n2_, x_Symbol] := Module[{num, den, f},
   num = MakePolynomial[n, x];
   den = MakePolynomial[n2, x];
   Evaluate[num]/Evaluate[den]
   ];

CreateDomain[f_, x_] := 
  Module[{pdisc, pdiscList}, 
   pdisc = Solve[FunctionSingularities[f, x]];
   pdiscList = Table[Last[Last[pdisc[[i]]]], {i, Length[pdisc]}]
   ];

CreateDiscontinuitiesPoly[] := Module[{n, f, domain},
   n = 1;
   While[n > 0,
    f = MakeRationalPolynomial[2, 2, x];
    domain = CreateDomain[f, x];
    If[Length[domain] > 0,
     n = 0;
     ,
     n = 1;
     ];
    ];
   f
   ];
Button["Generate Poly", Print@CreateDiscontinuitiesPoly[]]
$\endgroup$
1
  • $\begingroup$ thank you very much $\endgroup$
    – cgcg
    Commented May 11, 2022 at 12:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.