Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help me with the function display_polynominal: Write a C program, polynomial.h, containing the following functions: void display_polynomial(float p[], int n, float x), which prints the

Help me with the function display_polynominal:

Write a C program, polynomial.h, containing the following functions:

  1. void display_polynomial(float p[], int n, float x), which prints the polynomial in format like p[0]*x^{n-1} + p[1]*x^{n-2} +...+ p[n-2]*x^1 + p[n-1]*x^0 with 2 digits after the point, e.g., 1.00*10.00^3+2.00*10.00^2+3.00*10.00^1+4.00*10.00^0
  2. float horner(float p[], int n, float x), which computes and returns the value of the following polynomial P(x) of order n and coefficients p[0], , p[n-1].P(x)=p[0]xn1+p[1]xn2+...+p[n2]x1+p[n1]x0P(x)=p[0]xn1+p[1]xn2+...+p[n2]x1+p[n1]x0It is required to use Horners algorithm (supplementary link).
  3. float bisection(float p[], int n, float a, float b), which finds an approximate real root c in interval [a, b] of polynomial P(x), using the bisection method (supplementary link). Use the fault tolerant 1e-6 (or 0.000001) as a stop condition, i.e., if x0 is the actual root, stop the iteration if |c-x0|<1e-6 or |P(c)| < 1e-6.

Use the provided the main function program polynomial_main.c to test the above functions. The output should be like the following. Note that it is also right if you get -0.00 for P(-1.65).

Public test

gcc polynomial.h polynomial_main.c -o polynomial polynomial P(0.00)=1.00*0.00^3+2.00*0.00^2+3.00*0.00^1+4.00*0.00^0=4.00 P(1.00)=1.00*1.00^3+2.00*1.00^2+3.00*1.00^1+4.00*1.00^0=10.00 P(10.00)=1.00*10.00^3+2.00*10.00^2+3.00*10.00^1+4.00*10.00^0=1234.00 P(-2.00)=-2.00 P(2.00)=26.00 root=-1.65 P(-1.65)=0.00

polynomial.h:

#include #include #include #define EPSILON 1e-6

void display_polynominal(float p[], int n, float x){ // Insert code here }

float horner(float p[], int n, float x){ float ans = p[0]; int i; for (i = 1; i < n; i++){ ans *= x; ans += p[i]; } return ans; }

float bisection(float p[], int n, float a, float b){ float c = a; while (abs(b-a) >= EPSILON || solve(b) >= EPSILON){ c = (a + b)/2; if (solve(c) == 0){ break; } else if (solve(c) < 0){ a = c; } else { b = c; } } return c; }

polynomial_main.c:

#include #include #include #include "polynomial.h"

int main() { int n = 4; float p[] = { 1, 2, 3, 4 };

int m = 3; float x[] = { 0, 1, 10 };

// test display and horner functions int i; for (i = 0; i < m; i++) { printf("P(%.2f)=", x[i]); display_polynomial(p, n, x[i]); printf("="); printf("%.2f ", horner(p, n, x[i])); }

// test bisection function float a = -2, b = 2; float pa = horner(p, n, a); printf("P(%.2f)=%.2f ", a, pa); float pb = horner(p, n, b); printf("P(%.2f)=%.2f ", b, pb);

if (pa * pb <= 0) { float root = bisection(p, n, a, b); printf("root=%.2f ", root); printf("P(%.2f)=%.2f ", root, horner(p, n, root)); } else { printf("have the same sign on both sides "); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

Explain the factors influencing wage and salary administration.

Answered: 1 week ago

Question

Examine various types of executive compensation plans.

Answered: 1 week ago

Question

1. What is the meaning and definition of banks ?

Answered: 1 week ago

Question

2. What is the meaning and definition of Banking?

Answered: 1 week ago