Question

9. If we define f(r)= r-c, then solving f(x) = 0 is equivalent to finding e/n for n > 0. Given an approximate value of r, th

we are currently using C++ through reply.it thanks for your help.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE:: The following code takes input from the user and calculates the root of the aforesaid mentioned equation.

NOTE:: Lines starting with // are actually comments, will be ignored by the compiler

//including the iostream for basic input and output operations
#include <iostream>
//including math library for the mathematical calculation like taking absolute
#include <cmath>
//using default std namespace
#define MAX_COUNT 20
//defined the maximum number of iteration. Please change it to your need
#define EPSILON 0.001
//defined the lowest precision value. Please change it to suit your need
using namespace std;

//function to compute the new value of x given the previous ones

double compute_new_x(int n, double x_old, double c){
    //implementing the formula to calculate the new value of x
    double x_new=(((n-1)*x_old)/n)+(c/(n*(pow(x_old,n-1))));
    //returning the calculated value
    return x_new;
}

//working the main function around

int main() {
   //taking the input for the value of n
   int n;
   double x_ini,c;
   //taking input from user as n
   cout<<"Please enter the value of n"<<endl;
   cin>>n;
   //Asking the value of x
   cout<<"Please enter the initial value of x"<<endl;
   cin>>x_ini;
   //Asking the value of c
   cout<<"Please enter the value of c"<<endl;
   cin>>c;
   //checking whether all the inputs are positive if not then asking again
   while(n<0||x_ini<0||c<0){
        cout<<"You entered something negativive:: Not Allowed";
        cout<<"Please enter the value of n"<<endl;
        cin>>n;
        cout<<"Please enter the initial value of x"<<endl;
        cin>>x_ini;
        cout<<"Please enter the value of c"<<endl;
        cin>>c;
   }
   //computing the value of new root for the first time
   double x_new=compute_new_x(n,x_ini,c);
   //Printing the value of root at first iteration
   cout<<"Value at iteration 1 "<<x_new<<endl;
   //Starting the root convergence
   int count=0;
   //Looping till the max count or reach the precision
   while((abs(x_ini-x_new)>EPSILON)&&count<MAX_COUNT){
        x_ini=x_new;
        x_new=compute_new_x(n,x_ini,c);
        cout<<"Value at iteration "<<count+2<<" "<<x_new<<endl;
        count++;
   }
   //printing so that you can cross check the result
   double result=(1/(double)n);
   //typecasted n to double to put it in pow function
   cout<<"Value of c^(1/n) "<<pow(c,result)<<endl;
   return 0;
}

The above code is perfectly running. I tested it at the codechef online IDE.

If you face any issues please revert back, I will be happy to assist you further.

Hope this helps!!!

Thanks and regards

Add a comment
Know the answer?
Add Answer to:
we are currently using C++ through reply.it thanks for your help. 9. If we define f(r)=...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C PROGRAM NOT C++ Make sure your program reports out the following: - The polynomial that...

    C PROGRAM NOT C++ Make sure your program reports out the following: - The polynomial that the user has entered - The initial guess that the user has entered - Whether or not the Newton-Raphson algorithm converged to a solution - If successful, the root of the polynomial that the algorithm found - The number of iterations that the Newton-Raphson algorithm required to converge to a solution Also, add any numerical protections to the algorithm that you find necessary HINT:...

  • help with C++ please Substituting these formulas, the Newton-Raphson update rule for computing the n' root...

    help with C++ please Substituting these formulas, the Newton-Raphson update rule for computing the n' root of any value K is: 2+1 = - na"-T Write a program that includes a user-defined function named root that will calculate and return the nth root of any value using the Newton-Raphson method described above: double rootN(double x, int n); The function takes two arguments: a (type double) value > 0 and an integer root > 1. This function should work exactly as...

  • Newton's Method in MATLAB During this module, we are going to use Newton's method to compute...

    Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x° + 3x² – 2x – 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its...

  • Please write in Language c using only the files stdio.h and math.h Suppose you wish to...

    Please write in Language c using only the files stdio.h and math.h Suppose you wish to find the root r of a function f(x), that is, the value r where f(r)=0. One method is to make an initial guess, x0, compute the line tangent to f at x0, and find where the tangent line intercepts the x-axis, x1. Then, use x1 as the second guess and repeat this procedure n times until f(xn) approximately equals 0 and report xn as...

  • Data clustering and the k means algorithm. However, I'm not able to list all of the...

    Data clustering and the k means algorithm. However, I'm not able to list all of the data sets but they include: ecoli.txt, glass.txt, ionoshpere.txt, iris_bezdek.txt, landsat.txt, letter_recognition.txt, segmentation.txt vehicle.txt, wine.txt and yeast.txt. Input: Your program should be non-interactive (that is, the program should not interact with the user by asking him/her explicit questions) and take the following command-line arguments: <F<K><I><T> <R>, where F: name of the data file K: number of clusters (positive integer greater than one) I: maximum number...

  • Submission Items: (1) A Word document in which you: a. Describe in brief the problem you...

    Submission Items: (1) A Word document in which you: a. Describe in brief the problem you are solving and the numerical method used. b. Snapshot of the output, when you run the program for the conditions mentioned in the questions. c. Discuss your results. (2) MATLAB code (m) file() Question 1: [10 points) Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan(x) = x- + +... The function should accept 3 parameters:...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • using C++ language!! please help me out with this homework In this exercise, you will have...

    using C++ language!! please help me out with this homework In this exercise, you will have to create from scratch and utilize a class called Student1430. Student 1430 has 4 private member attributes: lastName (string) firstName (string) exams (an integer array of fixed size of 4) average (double) Student1430 also has the following member functions: 1. A default constructor, which sets firstName and lastName to empty strings, and all exams and average to 0. 2. A constructor with 3 parameters:...

  • Please can someone help me to finish this program? It is a C programming not a...

    Please can someone help me to finish this program? It is a C programming not a C++, when programming the program in linux I keep getting expected expression before || token, also please check if everything else is fine, maybe there is something else worng in my program and may ask me later to fix it, I am concern about the sin and cosine. The program is supposed to do all this: 1.Display a welcome message. 2.Prompt for the height...

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT