Question

Write C code for the following: Please type the code in the solution. Also, please put...

Write C code for the following:

Please type the code in the solution.

Also, please put the screenshot of the codes of the program being run successfully. Also, put the screenshot of the output from the computer screen.

I am getting many errors while trying to run the program. Screenshots of the program will help me.

Find all the roots using the Newton-Raphson method: 2 + sin(x) - 3x - x2 = 0

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

/*Source code for New-Raphson to find the roots*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
/*Function whose root is to be determined*/
#define    f(x)    3*x - cos(x) -1

/* Defining derivative of g(x).*/

#define   g(x)   3 + sin(x)
int main()
{
   float x0, x1, f0, f1, g0, e;
   int step = 1, N;
     /* Inputs */
   printf("\nEnter initial guess:\n");
   scanf("%f", &x0);
   printf("Enter tolerable error:\n");
   scanf("%f", &e);
   printf("Enter maximum iteration:\n");
   scanf("%d", &N);
   /* Implementing Newton Raphson Method */
   printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
   do
   {
          g0 = g(x0);
          f0 = f(x0);
          if(g0 == 0.0)
          {
               printf("Mathematical Error.");
               exit(0);
          }
          x1=x0-f0/g0;
          printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1);
          x0=x1;
          step=step+1;
          if(step>N)
          {
               printf("Not Convergent.");
               exit(0);
          }
          f1=f(x1);
   }while(fabs(f1)>e);
   printf("\nRoot is: %f", x1);
   return 0;
}

Screen shot of output:

Add a comment
Know the answer?
Add Answer to:
Write C code for the following: Please type the code in the solution. Also, please put...
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
  • Using python, write code for problem. Please also put screenshot of your code. The following program...

    Using python, write code for problem. Please also put screenshot of your code. The following program will perform properly if the user enters 0 in response to the request for input. However, the program will crash if the user responds with "eight". Rewrite the program using a try/except statement so that it will handle both types of responses. See Fig. 6.1. while True: n = int (input ("Enter a nonzero integer: ")) if n! = 0: reciprocal = 1/n print...

  • THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU....

    THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU. Program 1 Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape interface that has methods for getting information such as the volume and the surface area of a three-dimensional shape. Then make classes and subclasses that implement various shapes such as cube, cylinders and spheres. Place common behavior in superclasses whenever possible, and use abstract classes as appropriate. Add methods to the...

  • Please create a C program with the following C code declares variable mtx of matrix type,...

    Please create a C program with the following C code declares variable mtx of matrix type, and dynamically allocates memory for mtx. It will then get keyboard input for the N-by-N matrix. Subsequently, the program de-allocates (free) the memory of the matrix. A. Your task is to write the codes to dynamically allocate and de-allocate the memory for the matrix. B. Subsequently, you have to modify the above code so that now the program will first ask the user to...

  • guys can you please help me to to write a pseudo code for this program and...

    guys can you please help me to to write a pseudo code for this program and write the code of the program in visual studio in.cpp. compile the program and run and take screenshot of the output and upload it. please help is really appreciated. UTF-8"CPP Instruction SU2019 LA X 119SU-COSC-1436- C Get Homework Help With Che X Facebook -1.amazonaws.com/blackboard.learn.xythos.prod/584b1d8497c84/98796290? response-content-dis 100% School of Engineering and Technology COSC1436-LAB1 Note: in the instruction of the lab change "yourLastName" to your last...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

  • Write a C program that computes Pi with the approximation algorithm that I introduced in class....

    Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of adjacent elements (remember...

  • PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP...

    PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP AND FORMAT: #include <iostream> using namespace std; int main() { //variables here    do { // program here             }while (true);    } Objectives To learn to code, compile and run a program containing SELECTION structures Assignment Write an interactive C++.program to have a user input the length of three sides of a triangle. Allow the user to enter the sides...

  • Not in C++, only C code please In class, we have studied the bisection method for...

    Not in C++, only C code please In class, we have studied the bisection method for finding a root of an equation. Another method for finding a root, Newton's method, usually converges to a solution even faster than the bisection method, if it converges at all. Newton's method starts with an initial guess for a root, xo, and then generates successive approximate roots X1, X2, .... Xj, Xj+1, .... using the iterative formula: f(x;) X;+1 = x; - f'(x;) Where...

  • Please write this code in C++ Object-Oriented Programming, specify which files are .h, and .cpp, and...

    Please write this code in C++ Object-Oriented Programming, specify which files are .h, and .cpp, and please add comments for the whole code. Include a class diagrams, and explain the approach you used for the project and how you implemented that, briefly in a few sentences. Please note the following: -Names chosen for classes, functions, and variables should effectively convey the purpose and meaning of the named entity. - Code duplication should be avoided by factoring out common code into...

  • What to do Write a C program that computes Pi with the approximation algorithm that I...

    What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...

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