Exportera funktion till Excel


"""
Programmet exporterar en funktion till Excel
"""

import numpy as np
import openpyxl as op
from math import *

def f(x):
    a = 2
    b = 1
    c = 1
    d = 1
    return a * sin(b * x + c) + d

def func_to_excel(x_min: float, x_max: float, step: float, filename: str):
    x_range = abs(x_max - x_min)/step
    rows = int(x_range) + 1
    columns = 2
    matrix = np.zeros([rows, columns])
    for i in range(rows):
        x_coord = x_min + step * i
        matrix[i, 0] = x_coord
        matrix[i, 1] = f(x_coord)
    wb_out = op.Workbook()
    sheet = wb_out.active
    for i in range(1, rows + 1):
        for j in range(1, columns + 1):
            sheet.cell(row=i, column=j).value = matrix[i-1][j-1]
    wb_out.save('./' + str(filename) + '.xlsx')
    wb_out.close()


func_to_excel(-2*pi, 2*pi, 0.1, 'sinusfunktion')