Ask Your Question
1

Please help me to draw f(x) = sin(x) if x<=0 and f(x)=cos(x) if x>1

asked 2013-06-12 14:33:24 +0100

noufalasharaf gravatar image

thanks for your answers

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-06-12 15:50:39 +0100

calc314 gravatar image

There are a few options. One is to use the piecewise command:

f=Piecewise([[(-10,1),sin(x)],[(1,10),cos(x)]],x)
plot(lambda x: f(x),(-2*pi,2*pi))

Another is to use a python function definition:

def f(x):
    if x<=1:
        return(sin(x))
    else:
        return(cos(x))
plot(f,(-2*pi,2*pi))

Note that with the python function definition, the plot command must be called just with f and not f(x). Calling with f(x) causes python to evaluate $f$ before doing the plot and will result in an incorrect plot.

edit flag offensive delete link more

Comments

More compactly, you could also use a lambda expression: `lambda x: sin(x) if x `<`= 1 else cos(x)`

Eviatar Bach gravatar imageEviatar Bach ( 2013-06-13 22:26:03 +0100 )edit
2

answered 2013-06-12 20:01:21 +0100

Another option is to use two separate plots: plot(sin(x), (x, -10, 0)) + plot(cos(x), (x, 1, 10)). By the way, I don't know if the question contains a typo or if there is not supposed to be a plot when x is between 0 and 1.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-06-12 14:33:24 +0100

Seen: 2,552 times

Last updated: Jun 12 '13