Question

Please write a Taylor series function. C++

Please write a Taylor series function. C++

Homework Answers

Answer #1

Functions

The tool for solving these problems is to introduce functions, code which, like cos(x), takes one argument (a real number), performs an operation (such as a Taylor series approximation), and then returns a value (another real number).

When we define the function cos(x), we state that x is a formal parameter: that is, it is a place holder. We use that place holder in our definition of cos(x):

If we actually try to calculate, for example, cos(1), the value 1 is the actual parameter. We would substitute x = 1 into the formula for cos(x) and then return the result. In this case, we might get an approximation which looks like 0.5403023058681397174. This is the return value of the function cos(1).

In C++, we can similarly define a function, however, just as we defined variables to have a given type, we must define the types of the formal parameters and the return type, together with a name and code which must be evaluated.

Example 1

We have already seen such a function: main. Program 1 recalls the first program we saw: Hello World!

Program 1. Printing the value of a variable and its address.

#include <iostream>

using namespace std;

int main() {
        cout << "Hello World!" << endl;

        return 0;
}

We will now examine each of the features:

int

The return type of the function: this function returns an int.

main

The name of the function (how it will be called).

()

The list of formal parameters. In this example, there are no formal parameters, so the set of parentheses is left empty.

{ ... }

This is the body of the function, the instructions which take the parameters and convert them into appropriate return values.

return 0;

The return keyword indicates that the following object should be returned from the function. The type of the object being returned must match the return type, in this case, int.

When you compile a file with a int main() function, the generated executable begins by executing that function. The returned value (in most cases 0) is returned to the operating system.

Example 2

Let us now write a function which calculates the absolute value of an int. We will call this function int abs( int x ) and it is shown in Program 2. The function void main() asks the user for a int and then prints the absolute value of the given int.

Program 2. Requesting a value and printing its absolute value.

#include <iostream>

using namespace std;

int abs( int n ) {
        if ( n >= 0.0 ) {
                return n;
        } else {
                return -n;
        }
}

int main() {
        int input;
        cout << "Enter a number: ";
        cin >> input;

        cout << "|" << input << "| = " << abs( input ) << endl;

        return 0;
}

The compiler uses the type of the parameter n to ensure that the actual parameter input is of the same type (or, at least, can be cast into the stated type). The compiler also uses the return type (int) to ensure that the result is printed correctly (as an int).

Example 3

In this third example, we will see a poor implementation of the cosine function which uses a Taylor series approximation. This is demonstrated in Program 3.

Program 3. Requesting a value and printing its cosine.

#include <iostream>

using namespace std;

double cos( double x ) {
        // use the Taylor series approximation 1 - x^2/2! + x^4/4! - ...
        double result = 1.0;
        double term = 1.0;
        double n = 0.0;

        while ( term != 0.0 ) {
                n += 2.0;
                term *= -x*x/(n - 1)/n;

                result += term;
        }

        return result;
}

int main() {
        double x;
        cout << "Enter a number: ";
        cin >> x;

        cout << "cos( " << x << " ) = " << cos( x ) << endl;

        return 0;
}

Again, the return type double is used to ensure that whatever value is returned is printed as a double.

Example 4

Okay, the last two examples have been math-oriented. Now we will see a function which is related to programming.

Suppose you are writing a large piece of software and need to initialize all the entries of arrays of int to a default value: sometimes 0, sometimes 1, and sometimes -1. You could place a for-loop in every location, however, we will see that you will be writing a lot of code to do this.

Let's consider the Code Fragments 1 and 2.

Code Fragment 3. Initializing an array to -1.

        int array_size = 1000;
        int * array = new int[array_size];

        for ( int i = 0; i < array_size; ++i ) {
                array[i] = -1;
        }

Code Fragment 4. Initializing an array to 0.

        int num_accounts = 325;
        int * account_balance = new int[num_accounts];

        for ( int i = 0; i < num_accounts; ++i ) {
                account_balance[i] = 0;
        }

While examining the for loop, we notice that some things are the same: there is a variable i and entries of an array are being assigned.

Code Fragment 5. Extracting the common features of Code Fragments 3 and 4.

        for ( int i = 0; i < array_limit; ++i ) {
                array_name[i] = initial_value;
        }

We can now create a function as follows:

  1. Pick a name: initialize_array
  2. Determine the return type: in this case, there is nothing to return, so we use void to indicate that there is no return type, and
  3. We have three formal parameters. We list them in order of relevance together with their types: int * array_name, int array_limit, int initial_value.

We now take these pieces and define the function shown in Code Fragment 6.

Code Fragment 6. A function which initializes an array.

#include <iostream>

using namespace std;

void initialize_array( int * array_name, int array_limit, int initial_value ) {
        for ( int i = 0; i < array_limit; ++i ) {
                array_name[i] = initial_value;
        }

        // nothing to return 
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ please! Write a recursive function to compute the sum of the Taylor series. use double...
C++ please! Write a recursive function to compute the sum of the Taylor series. use double taylor(int a, int n)
For this problem, consider the function f(x) = ln(1 + x). (a) Write the Taylor series...
For this problem, consider the function f(x) = ln(1 + x). (a) Write the Taylor series expansion for f(x) based at b = 0. Give your final answer in Σ notation using one sigma sign. (You may use 4 basic Taylor series in TN4 to find the Taylor series for f(x).) (b) Find f(2020) (0). Please answer both questions, cause it will be hard to post them separately.
Use the definition of Taylor series to find the Taylor series (centered at c) for the...
Use the definition of Taylor series to find the Taylor series (centered at c) for the function. f (x) = e3x,   c = 0
Find the Taylor series for the function using the definition of Taylor series. f(x) = cos2x...
Find the Taylor series for the function using the definition of Taylor series. f(x) = cos2x , a = pi
The function f(x)=lnx has a Taylor series at a=4 . Find the first 4 nonzero terms...
The function f(x)=lnx has a Taylor series at a=4 . Find the first 4 nonzero terms in the series, that is write down the Taylor polynomial with 4 nonzero terms.
Write the Taylor series for the function f(x) = x 3− 10x 2 +6, using x...
Write the Taylor series for the function f(x) = x 3− 10x 2 +6, using x = 3 as the point of expansion; that is, write a formula for f(3 + h). Verify your result by bringing x = 3 + h directly into f (x).
A) Find the first 4 nonzero terms of the Taylor series for the given function centered...
A) Find the first 4 nonzero terms of the Taylor series for the given function centered at a = pi/2 B) Write the power series using summation notation f(x) = sinx
Use this list of Basic Taylor Series to find the Taylor Series for f(x) = 1...
Use this list of Basic Taylor Series to find the Taylor Series for f(x) = 1 6x−9 based at 0. Give your answer using summation notation, write out the first three non-zero terms, and give the interval on which the series converges. (If you need to enter ∞, use the ∞ button in CalcPad or type "infinity" in all lower-case.) The Taylor series for f(x)= 1 6x−9 is: ∞ k=0 The Taylor series converges to f(x) for all x in...
Consider the function ?(?) = sin(?). Find the Taylor series formula when centered at ? =...
Consider the function ?(?) = sin(?). Find the Taylor series formula when centered at ? = ?/3
Find the second degree polynomial of Taylor series for f(x)= 1/(lnx)^3 centered at c=2. Write step...
Find the second degree polynomial of Taylor series for f(x)= 1/(lnx)^3 centered at c=2. Write step by step.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT