2
$\begingroup$

I have no complains until Plot. g[x] seems good but Plot displays nothing. I need help. By advance Thanks

x[1] := 1; y[1] := 2;
x[2] := 2; y[2] := 3;
x[3] := 3; y[3] := 4;
x[4] := 4; y[4] := 1;
a = {x[1], y[1]};
b = {x[2], y[2]};
c = {x[3], y[3]};
d = {x[4], y[4]};
g[x_] := Piecewise[{
   {Interpolation[{a, b}, InterpolationOrder -> 1], {a[[1]] <= x < b[[1]]}}, 
   {Interpolation[{b, c}, InterpolationOrder -> 1], {b[[1]] <= x < c[[1]]}}, 
   {Interpolation[{c, d}, InterpolationOrder -> 1], {c[[1]] <= x < d[[1]]}}}]
g[x]
Plot[g[x], {x, 1, 4}]
$\endgroup$
3
  • 1
    $\begingroup$ Does g[x_] := Piecewise[{{Interpolation[{a, b}, InterpolationOrder -> 1][ x], a[[1]] <= x < b[[1]]}, {Interpolation[{b, c}, InterpolationOrder -> 1][x], b[[1]] <= x < c[[1]]}, {Interpolation[{c, d}, InterpolationOrder -> 1][x], c[[1]] <= x < d[[1]]}}] work? $\endgroup$
    – kglr
    Jun 11, 2018 at 10:51
  • $\begingroup$ Look at g[1] - as you can see, this is not correct $\endgroup$
    – Lukas Lang
    Jun 11, 2018 at 10:52
  • 2
    $\begingroup$ Why wouldn't you use g = Interpolation[{a,b,c,d}, InterpolationOrder->1] instead? $\endgroup$
    – Carl Woll
    Jun 11, 2018 at 13:52

1 Answer 1

4
$\begingroup$
g[x_] := Piecewise[{{Interpolation[{a, b}, InterpolationOrder->1]@x, a[[1]]<= x <b[[1]]},
  {Interpolation[{b, c}, InterpolationOrder -> 1]@x, b[[1]] <= x < c[[1]]},
  {Interpolation[{c, d}, InterpolationOrder -> 1]@x, c[[1]] <= x < d[[1]]}}]
g[x]

enter image description here

Plot[Evaluate@g[x], {x, 1, 4}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks for the @X $\endgroup$ Jun 11, 2018 at 12:29

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.