Question

In: Computer Science

USING C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0...

USING C++

1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n where n <= 10 The program inputs n, ai, and x from the keyboard and then prints out the corresponding value of p to the screen. The program continues to input new values of n, ai, x and evaluates p until a negative value for n is input, at which point the program stops.

2)Write a program that calculates exp(x) using the Taylor series approximation exp_approx = 1 + x + x^2 / 2! + x^3 / 3! + ... where x and error are input from the keyboard and a sufficient number of series terms are used such that abs( exp_approx - exp(x) ) < error. Hint: Use *= in a loop to calculate x^i and i! and print them out to the screen to verify that your loop is correct before calculating the series approximation.

Solutions

Expert Solution

1. C++ code:

#include<iostream>
#include<cmath>
using namespace std;

int main(){

while(true){
int n;
float x;

cout<<"Enter the value of n and x\n";
cin>>n;

if(n<0){
cout<<"End of the program\n";
break;
}
cin>>x;

float ans=0;

for(int i=0;i<=n;i++){
float a;
cout<<"Enter the value of a"<<i<<"\n";
cin>>a;
//ans = ans + ai * x^i
ans = ans + a*pow(x,i);
}
cout<<"value of p is "<<ans<<"\n";
}
return 0;
}

Output:

2. C++ code:

#include<iostream>
#include<cmath>
using namespace std;

int main(){
float x, error;
cout<<"Please enter x and error\n";
cin>>x>>error;
//True value of exp(x)
float exp_value = exp(x);

//Current x^val
float cur_x = x;
//Current val!
float cur_fact = 1;
int val = 2;
//Store exp(x) value calculated through taylor series
float exp_taylor = 1;

while(abs(exp_taylor - exp_value) >= error){
cout<<"x^"<<val-1<<" = "<<cur_x<<" ; ";
cout<<val-1<<"!"<<" = "<<cur_fact<<" ; ";
cout<<"x^"<<val-1<<"/"<<val-1<<"!"<<" = "<<cur_x/cur_fact<<"\n";
exp_taylor += cur_x/cur_fact;
cur_x *= x;
cur_fact *= val;
val++;
}
cout<<"Approximated value of exp(x) = "<<exp_taylor<<"\n";
return 0;
}

Output:


Related Solutions

Using C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0...
Using C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n where n <= 10 The program inputs n, ai, and x from the keyboard and then prints out the corresponding value of p to the screen. The program continues to input new values of n, ai, x and evaluates p until a negative value for n is input, at which point the program stops.
find the n-th order Taylor polynomial and the remainder for f(x)=ln(1+x) at 0
find the n-th order Taylor polynomial and the remainder for f(x)=ln(1+x) at 0
Let P(x) be a polynomial of degree n and A = [an , an-1,.... ] Write...
Let P(x) be a polynomial of degree n and A = [an , an-1,.... ] Write a function integral(A, X1, X2) that takes 3 inputs A, X0 and X1 A as stated above X1 and X2 be any real number, where X1 is the lower limit of the integral and X2 is the upper limit of the integral. Please write this code in Python.
Let P(x) be a polynomial of degree n and A = [an , an-1,.... ] Write...
Let P(x) be a polynomial of degree n and A = [an , an-1,.... ] Write a function integral(A, X1, X2) that takes 3 inputs A, X0 and X1 A as stated above X1 and X2 be any real number, where X1 is the lower limit of the integral and X2 is the upper limit of the integral. Please write this code in Python. DONT use any inbuilt function, instead use looping in Python to solve the question. You should...
p(x) = a0 + a1x + a2x2 + · · · + akxk is a polynomial...
p(x) = a0 + a1x + a2x2 + · · · + akxk is a polynomial of degree k. What is the Taylor series of p(x), and its radius and interval of convergence.
Given n ∈N and p prime number and consider the polynomial f (x) = xn (xn-2)+1-p...
Given n ∈N and p prime number and consider the polynomial f (x) = xn (xn-2)+1-p 1)Prove that f (x) is irreducible in Q [x] 2) If n = 1 and p = 3, find Q [x] / f (x)) 3) Show that indeed Q [x] / (f (x)) is a field in the previous paragraph PLEASE answer all subsections
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
Write a C program that evaluates the factorials from 1 to 5. Print results in a...
Write a C program that evaluates the factorials from 1 to 5. Print results in a tabular format.
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT