Как нарисовать математические функции в python из текстового файла? - PullRequest
0 голосов
/ 18 января 2020

Текстовый файл:

sin (x)

cos (x)

0.1 * x ** 2

-6,28 6,2

(ограничены двумя числами)

enter image description here

1 Ответ

0 голосов
/ 18 января 2020
import matplotlib.pyplot as plt
from math import *

def main():
    file=open('functions.txt','r')
    lst=file.read().split('\n')

    functions=[]
    limits=[]
    for item in lst:
        ''' loop that run on list '''
        if 'x' in item: # check if there is x in string so i can know it a math function
            functions.append(item)
        else: 
            # put the last line from the txt in limits
            limits.append(item)
    lim_ab = limits[0].split()
    a,b = float(lim_ab[0]),float(lim_ab[1])

    x_vals=[float(x/100) for x in range(int(a*100),int(b*100))]
    # values of x  run betwen a and , and dix by 100 so i can take ass much point
    # i cant put between float cause there is no limts points

    for item in range(len(functions)):
        ''' run in lenth of func lst and for math fuction have diffrent y but same x '''
        y_vals=[eval(functions[item]) for x in x_vals]

        plt.axis([a,b,-1.2,4.2])
        plt.plot(x_vals, y_vals, color='red')
        plt.xlabel("x values")
        plt.ylabel("y values")
    plt.show()
main()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...