Câu hỏi
Bài 2
Đề bài: Nhập góc x (độ). Tính và kiểm tra các công thức nhận doi:
sin(2x) = 2*sin(x)*cos(x)
cos(2x) = cos^2(x) - sin^2(x) = 1 - 2*sin^2(x) = 2*cos^2(x) - 1
tan(2x) = 2*tan(x) / (1 - tan^2(x)) nếu tan(x) ton tai và tan^2(x) != 1
Ví dụ: x = 30
Kết quả:
sin(60) = 0.866
2*sin(30)*cos(30) = 0.866 => Đúng
cos(60) = 0.5
cos^2(30) - sin^2(30) = 0.5 => Đúng
tan(60) = 1.732
2*tan(30)/(1-tan^2(30)) = 1.732 => Đúng
Câu trả lời
import math
x = float(input("Nhap goc x (do): "))
x_rad = math.radians(x)
s = math.sin(x_rad)
c = math.cos(x_rad)
sin2x_ly_thuyet = math.sin(math.radians(2 * x))
sin2x_ct = 2 * s * c
print(f"sin({2*x}) truc tiep = {round(sin2x_ly_thuyet, 4)}")
print(f"2*sin*cos (cong thuc) = {round(sin2x_ct, 4)}")
print(f"=> {'Dung' if round(sin2x_ly_thuyet,4)==round(sin2x_ct,4) else 'Sai'}")
cos2x_ly_thuyet = math.cos(math.radians(2 * x))
cos2x_ct1 = c**2 - s**2
cos2x_ct2 = 1 - 2 * s**2
cos2x_ct3 = 2 * c**2 - 1
print(f"\ncos({2*x}) truc tiep = {round(cos2x_ly_thuyet, 4)}")
print(f"cos^2 - sin^2 = {round(cos2x_ct1, 4)}")
print(f"1 - 2sin^2 = {round(cos2x_ct2, 4)}")
print(f"2cos^2 - 1 = {round(cos2x_ct3, 4)}")
if abs(c) > 1e-9 and abs(1 - (s/c)**2) > 1e-9:
tan2x_lt = math.tan(math.radians(2 * x))
tan2x_ct = 2 * (s/c) / (1 - (s/c)**2)
print(f"\ntan({2*x}) truc tiep = {round(tan2x_lt, 4)}")
print(f"2tan/(1-tan^2) = {round(tan2x_ct, 4)}")