مجموعه کامل رسم توابع و انتگرالگیری از BASIC تا Python
حتماً — مجموعه را به پایتون تبدیل کردم و بهجای `SCREEN` و `PSET/CIRCLE` از `matplotlib` استفاده شده تا نمودارها دقیقتر و قابل اجرا روی سیستمهای امروزی باشند.
```python
import math
import numpy as np
import matplotlib.pyplot as plt
def setup_plot(title, xlim=None, ylim=None):
plt.figure(figsize=(8, 5))
plt.axhline(0, color="black", linewidth=0.8)
plt.axvline(0, color="black", linewidth=0.8)
plt.grid(True, linestyle="--", alpha=0.4)
plt.title(title)
if xlim:
plt.xlim(*xlim)
if ylim:
plt.ylim(*ylim)
# Program 1: Helix in cylindrical coordinates
def program_1(a=3, t_max=2 * math.pi, step=0.02):
t = np.arange(0, t_max, step)
x = a * np.cos(t)
y = a * np.sin(t)
setup_plot("Program 1: Helix Projection", (-a - 1, a + 1), (-a - 1, a + 1))
plt.plot(x, y)
plt.gca().set_aspect("equal", adjustable="box")
plt.show()
# Program 2: Sphere in spherical coordinates (2D projection)
def program_2(a=3, step=0.1):
phi = np.arange(0, math.pi + step, step)
th = np.arange(0, 2 * math.pi + step, step)
xs, ys = [], []
for p in phi:
for t in th:
x = a * math.sin(p) * math.cos(t)
y = a * math.sin(p) * math.sin(t)
xs.append(x)
ys.append(y)
setup_plot("Program 2: Sphere Projection", (-a - 1, a + 1), (-a - 1, a + 1))
plt.scatter(xs, ys, s=2)
plt.gca().set_aspect("equal", adjustable="box")
plt.show()
# Program 3: Rectangle integration
def program_3(a, b, n):
def f(x):
return x * math.sin(x)
h = (b - a) / n
s = 0
for i in range(n):
x = a + i * h
s += f(x)
result = s * h
print("Program 3 result:", result)
# Program 4: Simpson integration
def program_4(a, b, n):
if n % 2 != 0:
raise ValueError("n must be even")
def f(x):
return 1 / (1 + x ** 2)
h = (b - a) / n
s1 = sum(f(a + i * h) for i in range(1, n, 2))
s2 = sum(f(a + i * h) for i in range(2, n, 2))
result = h / 3 * (f(a) + f(b) + 4 * s1 + 2 * s2)
print("Program 4 result:", result)
# Program 5: sinc(x)
def program_5(xmin=-20, xmax=20, step=0.05):
x = np.arange(xmin, xmax, step)
y = np.where(np.abs(x) < 1e-8, 1, np.sin(x) / x)
setup_plot("Program 5: sinc(x)", (xmin, xmax), (-0.5, 1.2))
plt.plot(x, y)
plt.show()
# Program 6: Polynomial graph
def program_6(coeffs, xmin=-10, xmax=10, step=0.05):
x = np.arange(xmin, xmax, step)
y = np.zeros_like(x, dtype=float)
for j, a in enumerate(coeffs):
y += a * x ** j
setup_plot("Program 6: Polynomial", (xmin, xmax), None)
plt.plot(x, y)
plt.show()
# Program 7: f(x)=x, g(x)=sin(x), h(x)=x*sin(x)
def program_7(xmin=-10, xmax=10, step=0.05):
x = np.arange(xmin, xmax, step)
y1 = x
y2 = np.sin(x)
y3 = y1 * y2
setup_plot("Program 7: x, sin(x), x*sin(x)", (xmin, xmax), None)
plt.plot(x, y1, label="f(x)=x")
plt.plot(x, y2, label="g(x)=sin(x)")
plt.plot(x, y3, label="h(x)=x sin(x)")
plt.legend()
plt.show()
# Program 8: a sin(x) + b cos(x)
def program_8(a, b, xmin=-20, xmax=20, step=0.05):
x = np.arange(xmin, xmax, step)
y = a * np.sin(x) + b * np.cos(x)
setup_plot("Program 8: a sin(x) + b cos(x)", (xmin, xmax), None)
plt.plot(x, y)
plt.show()
# Program 9: tan(ax+b)/(cx+d)
def program_9(a, b, c, d, xmin=-10, xmax=10, step=0.01):
x = np.arange(xmin, xmax, step)
y = []
for xv in x:
den = c * xv + d
ang = a * xv + b
if abs(den) < 1e-6 or abs(math.cos(ang)) < 1e-6:
y.append(np.nan)
else:
y.append(math.tan(ang) / den)
setup_plot("Program 9: tan(ax+b)/(cx+d)", (xmin, xmax), (-10, 10))
plt.plot(x, y)
plt.show()
# Program 10: ln((ax+b)/(cx+d))
def program_10(a, b, c, d, xmin=-15, xmax=15, step=0.01):
x = np.arange(xmin, xmax, step)
y = []
for xv in x:
den = c * xv + d
if abs(den) < 1e-6:
y.append(np.nan)
continue
z = (a * xv + b) / den
if z <= 0:
y.append(np.nan)
else:
y.append(math.log(z))
setup_plot("Program 10: ln((ax+b)/(cx+d))", (xmin, xmax), None)
plt.plot(x, y)
plt.show()
# Program 11: Polynomial graph from entered coefficients
def program_11(coeffs, xmin=-10, xmax=10, step=0.05):
x = np.arange(xmin, xmax, step)
y = np.zeros_like(x, dtype=float)
for j, a in enumerate(coeffs):
y += a * x ** j
setup_plot("Program 11: Polynomial Graph", (xmin, xmax), None)
plt.plot(x, y)
plt.show()
# Program 13: atan(ax^3+bx^2+cx+d)
def program_13(a, b, c, d, xmin=-10, xmax=10, step=0.05):
x = np.arange(xmin, xmax, step)
y = np.arctan(a * x ** 3 + b * x ** 2 + c * x + d)
setup_plot("Program 13: atan(ax^3+bx^2+cx+d)", (xmin, xmax), None)
plt.plot(x, y)
plt.show()
# Program 14: tan((ax+b)/(cx+d))
def program_14(a, b, c, d, xmin=-10, xmax=10, step=0.01):
x = np.arange(xmin, xmax, step)
y = []
for xv in x:
den = c * xv + d
if abs(den) < 1e-6:
y.append(np.nan)
continue
u = (a * xv + b) / den
if abs(math.cos(u)) < 1e-6:
y.append(np.nan)
else:
y.append(math.tan(u))
setup_plot("Program 14: tan((ax+b)/(cx+d))", (xmin, xmax), (-10, 10))
plt.plot(x, y)
plt.show()
# Program 15: atan((ax-b)/(cx+d))
def program_15(a, b, c, d, xmin=-10, xmax=10, step=0.05):
x = np.arange(xmin, xmax, step)
y = []
for xv in x:
den = c * xv + d
if abs(den) < 1e-6:
y.append(np.nan)
else:
y.append(math.atan((a * xv - b) / den))
setup_plot("Program 15: atan((ax-b)/(cx+d))", (xmin, xmax), None)
plt.plot(x, y)
plt.show()
if __name__ == "__main__":
# Examples:
program_1(a=3)
program_2(a=4)
program_3(0, math.pi, 1000)
program_4(0, 1, 100)
program_5()
program_6([1, -3, 2]) # 1 - 3x + 2x^2
program_7()
program_8(2, 1)
program_9(1, 0, 1, 1)
program_10(1, 2, 1, 3)
program_11([4, -1, 0, 1]) # 4 - x + x^3
program_13(1, 0, -2, 1)
program_14(2, 1, 1, 2)
program_15(1, 1, 1, 2)
```
چند نکته در تبدیل:
- بهجای رسم پیکسلی BASIC، از `matplotlib` برای نمودار پیوسته استفاده کردم.
- در برنامههای دارای `tan` و `log`، نقاط نامعتبر با `NaN` حذف شدهاند تا خطا ندهند.
- برنامههای 6 و 11 هر دو چندجملهای هستند، ولی جدا نگهشان داشتم تا با شمارههای اصلی منطبق بمانند.
- `Program 12` فعلاً حذف شده چون از روی فایلها قابل انتساب قطعی نبود.