1

I need your help with coding a graph result - plotting a function in an interval.

The question which I got is: "Plot the following composite function. You probably want to use 'if' statements and a loop to 'build' it. Plot the function in the interval from [-3, 5].

enter code here
f(x) = {|x|     x<0}
       {-1      0 <= x < 1}
       {+1      1 <= x < 2}
       {ln(x)   2 <= x}

Can anyone write for me please, a code in which the result shows me a GRAPH, in which the above function is shown, without consistancy in the graph's line.

Thank you very much in advance!

3 Answers 3

1

Using if statement would be a more involved way. You can directly make use of NumPy indexing and masking to get the task done. Below is how I would do it.

Explanation: First you create a mesh of x-data points in the interval (3, 5). Then you initialize an empty y-array of same length. Next, you use the conditions on x to get the indices of x-array. This is done by using mask. mask1 = ((x>=0) & (x<1)) defines a condition and then you use y[mask1] = -1 which means, [mask1] would return the array indices where the condition holds True and then you use those indices to assign the y-value. You do this for all 4 conditions. I just used two masks for the middle two conditions. You can also use 4 variables (masks) to do the same thing. It's a matter of personal taste.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-3, 5, 100)
y = np.zeros(len(x))

mask1 = ((x>=0) & (x<1))
mask2 = ((x>=1) & (x<2))

y[x<0] = np.abs(x[x<0])
y[mask1] = -1
y[mask2] = 1
y[x>=2] = np.log(x[x>=2])

plt.plot(x, y)
plt.xlabel('$x$')
plt.ylabel(r'$f(x)$')
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your help, It is really useful :) In addition, I would like to know how can I eliminate the lines connecting each step of the interval? I need to show only the graphic results on the graph, in each step, without the "continuity" of the lines that connect between them.
I can't think of a straightforward way at the moment but you can plot 4 curves separately instead of plotting them once as plt.plot(x[x<0], y[x<0], 'b'), plt.plot(x[mask1], y[mask1], 'b'), plt.plot(x[mask2], y[mask2], 'b'), plt.plot(x[x>=2], np.log(x[x>=2]), 'b')
1

Usually, simple composite functions can easily be written like any other function by multiplying by the respective condition(s). The only place one needs to be careful is with the logarithm, which is not defined over the complete inverval. This problem is circumvented by taking the absolute value here, because it's anyways only relevant in the range > 2.

import numpy as np
import matplotlib.pyplot as plt

f = lambda x: np.abs(x)*(x<0) - ((0<=x) & (x < 1)) + ((1<=x) & (x < 2)) + np.log(np.abs(x))*(2<=x)

x = np.linspace(-3,5,200)

plt.plot(x,f(x))
plt.show()

enter image description here


According to a comment below the answer, one can also evaluate the function in each of the intervals separately,

intervals = [(-3, -1e-6), (0,1-1e-6), (1, 2-1e-6), (2,5)]
for (s,e) in intervals:
    x = np.linspace(s,e,100)
    plt.plot(x,f(x), color="C0")

enter image description here

2 Comments

Thank you very much for your help, It is really useful :) In addition, I would like to know how can I eliminate the lines connecting each step of the interval? I need to show only the graphic results on the graph, in each step, without the "continuity" of the lines that connect between them.
It's a bit questionable why you would first define a single function and then evaluate it independently for each interval. But I updated the answer.
0

Thank you very much for your help, It is really useful :)

In addition, I would like to know how can I eliminate the lines that connecting each step of the interval to the next one?

I need to show only 4 seperate graphic results on the graph, in each step, without the "continuity" of the lines that connect between them.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.