0
$\begingroup$

How would I write a function using If and/or Which command for following Piecewise-defined function then plot the function

f(x)=1, if x<=1
f(x)=x^3, if -1<x<=1
f(x)=-1, if x>1

This what I have so far

f[x_] := If[(x <= -1) && (x < 1 || x <= 1), x^3, 
  Which[x <= -1, -1, x > 1, 1]]
$\endgroup$

closed as off-topic by Artes, rhermans, MarcoB, Bob Hanlon, dr.blochwave Dec 1 '15 at 22:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Artes, rhermans, MarcoB, Bob Hanlon, dr.blochwave
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Your function is confusing. You want $f(x) =1$ if $x\leq1$ and $f(x) = x^3$ if $x >1$? If so: f[x_ /; x <= 1] := 1; f[x_ /; x > 1] := x^3; of f[x_] := If[x <= 1, 1, x^3];. $\endgroup$ – march Dec 1 '15 at 17:09
  • 1
    $\begingroup$ For mathematical functions, you should try Piecewise: reference.wolfram.com/language/ref/Piecewise.html. $\endgroup$ – Chip Hurst Dec 1 '15 at 17:13
1
$\begingroup$

Your function definition is inconsistent. Perhaps you meant,

f1[x_] = Piecewise[{{Sign[x], Abs[x] > 1}}, x^3];

f2[x_] := Clip[x^3];

Plot[{f1[x], f2[x]}, {x, -2, 2},
 PlotStyle -> {
   Directive[Red, AbsoluteDashing[{5, 5}]],
   Directive[Blue, AbsoluteDashing[{7, 7}]]},
 PlotLegends -> "Expressions"]

enter image description here

$\endgroup$

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