0

I am trying to replicate the mathematical operations from the link using Python.

I managed to integrate the function but I am unable to plot it for the specified intervals. I've tried to use arrange to get values for arguments and plot it against the function but it doesn't work.

Does anyone know how to get it work?

Link - Mathcad capture

My snippet code:

from scipy.integrate import quad

def f(x):
    if x >= 0 and x <= 2:
        return x ** 2

    elif x > 2 and x <= 4:
        return 4

    else:
        return 0

 res = quad(f, 0, 5)

 print(res)
3
  • 2
    The result of an integral over an interval is a single number. Do you want to plot that number? Commented Jul 20, 2018 at 22:19
  • I tried to plot the function in the intervals rather than the integral. Also, I am looking for an alternative solution, preferably in a symbolic way just like in the figure. I unable to find the solution where I can specify a symbol x and then for a different function interval have a different expression Commented Jul 22, 2018 at 12:30
  • You tagged this question with matplotlib and scipy and use a scipy method for integration. Those are numeric tools. For symbolic math refer to other packages. However, for the task as described in the question, numeric tools are totally fine. Commented Jul 22, 2018 at 12:50

1 Answer 1

3

I assume you want to plot the function, f(x), rather than the results of integrating it. To do that you will want to create a list of x values (sounds like you did this), evaluate f for each of those values, and then use matplotlib's plot function to display the result.

The documentation for arange says that "When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use linspace for these cases." You probably want to plot with a non-integer step in x, otherwise you will use most of the details of your plot. So I would suggest switching to linspace.

import numpy as np 
from matplotlib.pyplot import plot 

xvals = np.linspace(0,6,100) #100 points from 0 to 6 in ndarray
yvals = list(map(f, xvals)) #evaluate f for each point in xvals 

plot(xvals, yvals)

Most likely where you ran into a problem was directly applying your function f to an ndarray. The way it is written, f expects a single value as an input rather than an array. Map solves this problem by applying f to each value in your ndarray individually.

Edit: To use a sympy symbolic function:

You can also define a piecewise function in sympy. For the things you are trying to accomplish in your question, this won't be any different from using the original method described. However, if you want to do further symbolic manipulations with your function this could be useful.

import sympy

x = sympy.symbols('x') 
f = sympy.Piecewise((0, x>4),(4, x>2) ,(x**2, x>=0)) #defines f as a symbolic function
sympy.plot(f, (x, 0,6)) #Plots f on the interval 0 to 6 

Note in the definition of a piecewise function, the conditions are evaluated in order, so the order you define them in does matter. If, for example you swapper the first two conditions, the x>4 condition would never be reached because x>2 would always be satisfied first. This is why the conditions are defined in the reverse order from your original function.

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

2 Comments

Do you know if it's possible to define the same function with intervals in a symbolic way with sympy?
@Xzm Sure, I've updated my answer to include a symbolic version of the function.

Your Answer

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