import numpy as np import matplotlib.pyplot as plt def f(x): return (0.25*x - 2)*np.sin(3*x) n = 10000 # number of x-values x = np.linspace(-5, 5, n) x_sol_array = np.array([]) for i in range(0, n-1): if f(x[i])*f(x[i+1]) <= 0: x_sol = (x[i] + x[i+1])/2 print(f'Losning: {x_sol:.2f}') x_sol_array = np.append(x_sol_array, x_sol) filename = 'bf_losning.csv' np.savetxt(filename, x_sol_array.T, fmt='%.2e') plt.close('all') plt.plot(x, f(x)) plt.grid() plt.plot(x_sol_array, f(x_sol_array), 'r*') plt.xlabel('x') plt.ylabel('f(x)') # plt.savefig('losn_likn_bf.pdf') plt.show()