0
$\begingroup$

I basically made a code interpolating a set of data that gives me the polynomial for the interpolation. Here's the sample interpolation I did.

xData={0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.}
y2Data={1.05513, 1.12104, 1.19859, 1.2887, 1.39235, 1.51062, 1.64465, 1.79568, 1.96503, 2.1541, 2.36438, 2.59745, 2.85498, 3.13873,3.45051, 3.79223, 4.16587, 4.57346, 5.01706, 5.4988}
num = Length[xData];
mat = Table[(xData[[i]])^n, {i, 1, num}, {n, 0, num - 1}];    
mat;
solMat = LinearSolve[mat, y2Data];
poly2 = 0;
For[i = 1, i <= num, i++,
   poly2 = poly2 + solMat[[i]]*z^(i - 1)]
poly2
   

enter image description here

and I want to use that polynomial for a different task. Basically, I want it similar to the photo below but without copy-pasting it.

enter image description here

$\endgroup$
3
  • $\begingroup$ Can you please show us the code used for generating that polynomial? Generally, just assign this expression to f. For example, if your code is expr = Sum[z^i, {i, 3}];, put f[z_] := Evaluate[expr]. $\endgroup$
    – Domen
    Commented Oct 26, 2023 at 13:36
  • $\begingroup$ Perhaps f=Function[z, "Out[1042]"] $\endgroup$ Commented Oct 26, 2023 at 13:37
  • $\begingroup$ I get a different output that what you show, and I get some warnings from the LinearSolve step, but if poly2 is the thing you're wanting to turn into a function, then you can just do f[z_] = poly2. Notice the Set rather than SetDelayed. $\endgroup$
    – lericr
    Commented Oct 26, 2023 at 14:24

1 Answer 1

2
$\begingroup$
$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

Use arbitrary precision rather than machine precision.

xData = {0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 
    0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.} // SetPrecision[#, 15] &;
y2Data = {1.05513, 1.12104, 1.19859, 1.2887, 1.39235, 1.51062, 1.64465, 
    1.79568, 1.96503, 2.1541, 2.36438, 2.59745, 2.85498, 3.13873, 3.45051, 
    3.79223, 4.16587, 4.57346, 5.01706, 5.4988} // SetPrecision[#, 15] &;
num = Length[xData];

mat = Transpose[xData^# & /@ Range[0, num - 1]];

solMat = LinearSolve[mat, y2Data];

(poly2[z_] = solMat . z^Range[0, num - 1])//N

(* 0.79949 + 15.0472 z - 426.711 z^2 + 7694.19 z^3 - 91979.9 z^4 + 
 784617. z^5 - 4.98164*10^6 z^6 + 2.42031*10^7 z^7 - 
 9.16522*10^7 z^8 + 2.73726*10^8 z^9 - 6.49034*10^8 z^10 + 
 1.22439*10^9 z^11 - 1.83343*10^9 z^12 + 2.1636*10^9 z^13 - 
 1.98557*10^9 z^14 + 1.38648*10^9 z^15 - 7.1114*10^8 z^16 + 
 2.52421*10^8 z^17 - 5.53586*10^7 z^18 + 5.64823*10^6 z^19 *)

However, you can get this result directly using InterpolatingPolynomial

poly3[z_] = InterpolatingPolynomial[Transpose[{xData, y2Data}], z];

poly2[x] - poly3[x] // Simplify

(* 0.*10^-4 + 0.*10^-4 x + 0.*10^-4 x^2 + 0.*10^-4 x^3 + 0.*10^-4 x^4 + 
 0.*10^-4 x^5 + 0.*10^-4 x^6 + 0.*10^-4 x^7 + 0.*10^-4 x^8 + 0.*10^-4 x^9 + 
 0.*10^-4 x^10 + 0.*10^-4 x^11 + 0.*10^-4 x^12 + 0.*10^-4 x^13 + 
 0.*10^-4 x^14 + 0.*10^-4 x^15 + 0.*10^-4 x^16 + 0.*10^-4 x^17 + 
 0.*10^-4 x^18 + 0.*10^-4 x^19 *)

Plot[poly3[x], {x, Min[xData], Max[xData]},
 Epilog -> {Red, AbsolutePointSize[4],
   Point[Transpose[{xData, y2Data}]]}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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