使用循环实现函数,该循环使用泰勒级数来近似余弦函数的值。 [英] Implement a function using loops that uses the taylor series to approximate the value of the cosine function.

查看:119
本文介绍了使用循环实现函数,该循环使用泰勒级数来近似余弦函数的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cosx =Σn=0∞(-1)n(2n)!x2n = 1-x22!+ x44! - ⋯

随着n的增加,我们的求和项变得越来越多精确逼近cos(x),无穷多个项精确等于cos(x)。 (注意:x是弧度。)



显然我们不能在现实世界中添加无数个术语,所以相反,如果我们将其实现为在C ++中,我们必须指定我们想要使用的求和项的数量。 (换句话说,总和的上限是无限的。)

因此,你的函数将采用两个参数:值x(以弧度表示)作为近似时使用的术语数解。因此,如果我的测试代码调用approxCosine(1.5,3),你应该返回余弦近似的前3项之和:



approxCosine(1.5,3) )=Σn= 02(-1)n(2n)!x2n = 1-x22!+ x44!



我的尝试:



i不知道从哪里开始

解决方案

说真的吗?你的主要努力是粘贴声明。

引用:

我不知道从哪里开始



答案就在这个问题上。 首先要做的是阅读声明

Quote:

因此, 您的函数将采用两个参数:值 x(以弧度表示)术语数在近似解决方案时使用。因此,如果我的测试代码调用 approxCosine(1.5,3),你应该返回余弦近似的前3项之和:



我们这样做不要做你的家庭作业。

HomeWork不会在乞求别人做你的工作时测试你的技能,它会让你思考并帮助你的老师检查你对你的课程的理解已经采取了,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个确切的问题,我们可能会提供帮助。



作为程序员,你的工作是创建算法来解决具体问题,你可以'依靠别人为你永远做到这一点,所以有一段时间你将不得不学习如何做。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。



发展的概念就像这个词所暗示的那样:系统地使用科学和满足特定目标或要求的技术知识。 BusinessDictionary.com [ ^ ]

这与有快速谷歌并放弃,如果我不是一回事找不到完全正确的代码。


引用:

我不知道从哪里来开始



余弦泰勒展开的一般术语是(-1) n x 2n /(2n!)。

第一步可能是编写C ++表达式。前提条件是编写阶乘函数,这是没有提供的。



所以,假设你已经编写了自己的阶乘函数(比如 fact ),然后你可以写

 term [n] = pow(x, 2  0  * n)/ fact( 2  * n); 
if ((n% 2 )== 1 )term [n] = -term [n]; // 对于n odd,术语为负


cosx=∑n=0∞(−1)n(2n)!x2n=1−x22!+x44!−⋯
as n increases, our summation term becomes a more and more accurate approximation of cos(x), and an infinite number of terms would precisely equal cos(x). (Note: x is in radians.)

Obviously we can't add up an infinite number of terms in the real world, so instead if we implement this as a function in C++, we must specify how many terms of the summation we want to use. (In other words, the upper bound of the sum stops being infinite.)
Thus, your function will take two parameters: the value x (in radians) as the number of terms to use when approximating the solution. Thus, if my test code calls approxCosine(1.5, 3), you should return the sum of the first 3 terms of the cosine approximation:

approxCosine(1.5,3)=∑n=02(−1)n(2n)!x2n=1−x22!+x44!

What I have tried:

i do not know from where to start

解决方案

Seriously ? your main effort was to paste the statement.

Quote:

i do not know from where to start


The answer is in the question. The first thing to do is to read the statement.

Quote:

Thus, your function will take two parameters: the value x (in radians) and the number of terms to use when approximating the solution. Thus, if my test code calls approxCosine(1.5, 3), you should return the sum of the first 3 terms of the cosine approximation:


We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".


Quote:

i do not know from where to start


The general term of the cosine taylor expansion is (-1)nx2n/(2n!).
The first step could be writing such as a C++ expression. The prerequisite is writing the factorial function, that is not provided.

So, supposing you've written your own factorial function (say fact), then you might write

term[n] = pow(x, 2.0 * n) / fact(2 * n);
if ((n % 2) == 1) term[n] = -term[n]; // for n odd, the term is negative


这篇关于使用循环实现函数,该循环使用泰勒级数来近似余弦函数的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆