0
$\begingroup$

i using mathmatica to study lagrange Interpolating,however i couldn't make the output as I want I wrote both who is the output and how i needed

 Clear[f, x];
f[x_] = 1 + Cos[3 x]*Exp[-x/2]; a = 0; b = 2;
Plot[f[x], {x, a, b}, AxesOrigin -> {0, 0}, PlotRange -> All];    
myone[f_, n_, exactdata_] := 
 Module[{data, data2, i, xi, p1, p2, Lerr},
  data = Table [{xi = a + (b - a)/n i , N[ f[xi], 60]}, {i, 0, n}];

  data2 = Table [{xi = a + (b - a)/n i ,  
     N[Round[10^7  f [xi]]/10^7]}, {i, 0, n} ];
  data = SetAccuracy [data, 300]; 
  data2 = SetAccuracy[data2, 300];

  poly[t_] = InterpolatingPolynomial[data, t];
  polyy[t_] = InterpolatingPolynomial[data2, t];
  eps = 0.5*10^-8;
  maxofy = 2;
      "ERROR\n THEORICAL\n"
       N[(2^(n + 1)*eps*maxofy)/(E*n*Log[n])]
       "PRACTICAL "

       N[FindMaximum[{Abs[poly[t] - polyy[t]], t >= 0 && t <= 2}, t]
]]



myone[f, 10, False]  

the output is like that

    {1.65731*10^-14 "ERROR
       THEORICAL
      " "PRACTICAL ", {3.27205*10^-7 "ERROR
        THEORICAL
       " "PRACTICAL " (t -> 1.80556)}}

I need the output to be

ERROR     
1.65731*10^-14  THEORICAL    
3.27205*10^-7   PRACTICAL
$\endgroup$
1
$\begingroup$

Use

Column[{"ERROR", Row[{N[(2^(n + 1)*eps*maxofy)/(E*n*Log[n])], " THEORETICAL" }], 
  Row[{ N[FindMaximum[{Abs[poly[t]-polyy[t]], t >= 0 && t <= 2}, t][[1]]], " PRACTICAL"}]}]

or

Column[{StringForm["ERROR\n `` THEORETICAL\n" , N[(2^(n + 1)*eps*maxofy)/(E*n*Log[n])] ], 
  StringForm["\n `` PRACTICAL", N[FindMaximum[{Abs[poly[t] - polyy[t]], 
     t >= 0 && t <= 2}, t][[1]]]]}]

instead of

"ERROR\n THEORICAL\n"
N[(2^(n + 1) eps maxofy)/(EnLog[n])]
"PRACTICAL "
N[FindMaximum[{Abs[poly[t] - polyy[t]], t >= 0 && t <= 2}, t]]

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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