For example, sinx and cosx are approximated by the following two polynomials

       在数学上对一些复杂的函数,为了便于研究,往往用一些简单的函数来近似表达。常用多项式来近 似表示函数,只需对自变量进行有限次数的加、减、乘、除运算便能求出函数值来。例如关于 sinx 与 cosx 是用如下两个多项式来近似表达的

Insert picture description here
The method of error control in actual calculation is as long as the absolute value of the remaining term is less than a predetermined value ε, which can be set to 10-5 or 10-6.
Please write a program to calculate any point of the two functions of sinx and cosx in the interval [0, 90 °] according to the title description and relevant mathematical knowledge.

Programming guidance

    从程序设计角度来说,本题目主要训练编程者设计函数与运用函数的能力。这里给出 sinx 的计算程 序的编写方法,cosx 可以参考 sinx 的计算方法进行设计。 根据题目的描述可以看出,sinx 是用一个多项式来近似表示的,而多项式中的各项均有规律,其通 项可以用如下公式来表示。 

Insert picture description here
Suppose that the position of the general term is represented by i, the absolute value of the general term is represented by item, the symbol of the general term is represented by s and its initial value is 1, and the sum of the general terms is represented by sum. The following 4 steps can be repeated until the value of the general item is less than a given number ε to solve the problem.
(1) Solve the general term and put the value of the general term in the item;
(2) Multiply the value of the general term item by s into sum, that is, sum = sum + s * item;
(3) The symbol of the general term s becomes the opposite number, that is, s = -s;
(4) The number of items in the general term plus 1, i = i + 1; In the above four steps, the most important thing is to find the value of the general term, the general term is A fraction, whose numerator is a power function xn whose exponent is related to the number of positions of the general term, and whose denominator is a factorial n related to the number of positions of the general term! . Therefore, the numerator and denominator can be designed as a function to complete.
3. Code example
The exponential function in the program is double power (double x, int n), the function for factorial is int fact (int n), and the calculation of the sine function is written in double my_sin (double x). Insert picture description here
Note: This program calls the custom sine function my_sin () in the main function while calling the standard sine function of the C language system. Its purpose is to compare the correctness of the custom sine function with the custom sine function.
Insert picture description here

Published 10 original articles · Liked12 · Visitors 1862

Guess you like

Origin blog.csdn.net/qq_44236958/article/details/88933687