1
$\begingroup$

In the following piece of code I am trying to convert a Piecewise function to a simple interpolation function f[t_]

T=10000    
 f[t_] := FunctionInterpolation[
          Piecewise[{{0, 0 <= t <= T/4}, {1 - Sin[2*\[Pi]*t/T], 
             T/4 <= t <= 3*T/4}, {2, 3*T/4 <= t <= T}}], {t, 0, T}, 
          InterpolationOrder -> 10]

However

  1. I was not able to plot it: Plot[f[t], {t, 0, T}] resulted in

    FunctionInterpolation::range: Argument {0.817143,0,T} is not in the form of a range specification, {x, xmin, xmax}.

  2. I was not able to configure WorkingPrecision: when adding ...WorkingPrecision -> 20], WorkingPrecision comes in red.

Can anyone help?

$\endgroup$
2
  • $\begingroup$ What is T ? Without its numerical value Plot cannot plot. $\endgroup$
    – Lotus
    Commented Feb 5, 2020 at 10:11
  • $\begingroup$ Actually I forgot T but your answer is great: it is more practical to have T as a parameter. $\endgroup$
    – user67126
    Commented Feb 5, 2020 at 10:48

1 Answer 1

1
$\begingroup$

A little large for a comment: Change the function definition as:

f[t_, T_] := 
 FunctionInterpolation[
  Piecewise[{{0, 0 <= t <= T/4}, {1 - Sin[2*\[Pi]*t/T], 
     T/4 <= t <= 3*T/4}, {2, 3*T/4 <= t <= T}}], {t, 0, T}, 
  InterpolationOrder -> 10]

Now this yields an Interpolation function.

f[t, 10]

And can be plotted.

Plot[f[t, 10][x], {x, 0., 10.}]

enter image description here

$\endgroup$
5
  • $\begingroup$ And what about Working Precision? $\endgroup$
    – user67126
    Commented Feb 5, 2020 at 10:39
  • $\begingroup$ Where were you adding the WorkingPrecision option ? $\endgroup$
    – Lotus
    Commented Feb 5, 2020 at 16:32
  • $\begingroup$ Well, I tried in the end of the FunctionInterpolation block, I mean FunctionInterpolation[...bla,bla,bla,....,WorkingPrecision->100]. That is how it works for NDSolve or Plot however it does now work for FunctionInterpolation. $\endgroup$
    – user67126
    Commented Feb 6, 2020 at 10:20
  • $\begingroup$ That is because WorkingPrecision is not a valid option for FunctionInterpolation. See Options[FunctionInterpolation] $\endgroup$
    – Lotus
    Commented Feb 6, 2020 at 10:33
  • $\begingroup$ Ok, then this should be discussed in a seperate question. Thanks for your help! $\endgroup$
    – user67126
    Commented Feb 6, 2020 at 11:57

Your Answer

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