Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment you will write a program that will retrieve a polynomial expression, two initial values, and associated parameters from command line arguments specified

In this assignment you will write a program that will retrieve a polynomial expression, two initial values, and associated parameters from command line arguments specified below, and then exercise the Newton and Secant Methods to attempt to find a root within the required accuracy and within the maximum number of iterations. The program must generate output to the console (terminal) screen as specified below.

You may submit this assignment either individually or as a two-person team. Teams of more than two students are not permitted. If working as a team, both students must submit identical programs. Please see the syllabus for the full set of teaming rules.

Command Line Arguments

  1. Your program must compile and run from the command line.
  2. The program executable must be named newton (all lower case, no spaces or file extension).
  3. The program must retrieve the following command line arguments, which must be specified in the order shown below. Your program may NOT prompt the user to enter any input values, NOR may it simply wait for the user to enter such values without prompting.
Argument 1: p0

an integer or decimal real value, which can be negative; this value represents the initial x-value for Newtons Method and the first initial value for the Secant Method.

Argument 2: p1 an integer or decimal real value, which can be negative; this value represents the second initial x-value for the Secant Method.

Argument 3:

tol

a decimal real value, which must be positive; this value represents the accuracy tolerance value (e.g., .0005).

Argument 4:

max

a positive integer; represents the maximum number of iterations that each method will be allowed to run.

Argument 5: lim

a positive integer; represents the highest order term of the input polynomial (e.g., lim = 3 for a cubic polynomial).

Arguments 6 to (6+lim)

decimal real values for the coefficients of the terms of the polynomial, in descending order of the powers of x, from x^lim to x^0.

Specifying the Polynomial Using Input Arguments

To simplify input of the polynomial, for this assignment the polynomial will be specified by entering its coefficients as decimal real values in descending order of the powers of x, from x^lim to x^0, where the following conditions must be observed:

(a) An implicit coefficient of 1 must be included explicitly; and

(b) All terms of the polynomial must be specified, so that terms that do not appear in the polynomial must be represented by zero (0) coefficients in the parameter set.

For example, suppose we wish to enter the function x3 - 2x2 - 5. For this function, the x1 term is not used. Also, this function has an implicit coefficient of 1 for the x3 term. The parameter set for this function will be: 1 -2 0 -5 .

Please note that the number of parameters needed to specify the polynomial will vary and will always be one more than the degree of the polynomial. For the third order polynomial above, we have four coefficients. For a 4th degree polynomial, 5 coefficients are needed, etc.

Running the Program

To run the program, simply launch the program from a command prompt or terminal window and enter all of the required parameters, in the order specified above, following the name of the program before pressing the "enter" or "return" key on your keyboard.

For a Java program in Windows, Mac OS, or Linux, an example run command (with arguments) would be:

java newton 2 4 .0005 50 3 1 -2 0 -5

For a C/C++ version of the program in Mac OS or Linux, the run command would look like

.ewton 2 4 .0005 50 3 1 -2 0 -5

And the C/C++ version in Windows would be simply:

newton 2 4 .0005 50 3 1 -2 0 -5

Please note that all of the command arguments are separated by spaces and are entered immediately after the program name and before you press the "enter" or "return" key.

Program Output

The program must send all output to the console (terminal) screen. The output must contain the following sections and must be formatted in substantially the same form as the sample output in the next section:

(a) Program title with your name: The program must output "Polynomial Root Finder by [ your name ]"

(b) Echo control parameters: The program must echo the input parameters for p0, p1, tol, max, and lim.

(c) Echo polynomial: The program must echo the polynomial coefficients in a manner that associates each coefficient with the power of x that it precedes. Additionally, negative values should be expressed as such. The x0 term may be displayed with or without the x0.

(d) Report Newton Method Results: The program must show the values of each successive approximation computed in accordance with Newtons Method. Do not format the output values (we wish to view all digits). The program must also report whether or not a solution was found, and if so, after how many iterations. The program must also detect a possible divide by zero situation and exit gracefully with an appropriate message in such situation.

(e) Report Secant Method Results: The program must show the values of each successive approximation computed in accordance with the Secant Method. Do not format the output values (we wish to view all digits). The program must also report whether or not a solution was found, and if so, after how many iterations. The program must also detect a possible divide by zero situation and exit gracefully with an appropriate message in such situation.

Sample Output

The following is the desired output from running the program using the following command line parameters: 2 4 .0005 50 3 1 -2 0 -5. The function for this output is f(x) = x3 -2x2 -5. The graph of this function looks like:

image text in transcribed

from which we can see that the function has a root somewhere between 2 and 3.

The program output for the above parameter set is:

image text in transcribed

Please note how the terms of the polynomial are shown. This is the recommended display format.

Program Testing

Your program will be tested by running a number of test cases. Some of the test cases will share the same function but will vary the other parameters. Different functions will also be tested. Scenarios that should be trapped for divide-by-zero errors by both methods will also be tested.

What to Submit

  • You must submit exactly one source code file of type .c, .cpp, or .java
  • The source file must contain a comment at the top identifying you (or you and your teammate) as the author(s), in the following form:

image text in transcribed

This requirement is in addition to the requirement to output the program title with your name(s).

  • The source code file must be submitted in Webcourses from the assignment page.
  • You must do your own work. UCFs Golden Rule applies.

Your program must successfully compile from the command line with one of these commands:

C program: prompt> gcc -lm o newton [your_file_name].c

C++ program: prompt> g++ -lm o newton [your_file_name].cpp

Java program: prompt> javac newton.java

this is my code

#include #include

int main(){ float p_0, p_1, p0, p1, tol; int max, lim, k,i,j; float p,f,F,q,w; scanf("%f %f %f %d %d", &p_0, &p_1, &tol, &max , &lim); float cf[lim+1]; for (i = 0; i

printf("Polynomial Root Finder by Amro Zidan "); printf("Input Parameters: \tp0 = %f \tp1 = %f \ttol = %f \tmax = %d ", p_0, p_1, tol, max); printf("Polynomial is of order: %d ",lim); printf(" Terms of polynomial: "); int counter = lim; for(i = 0; i

printf(" Newton's method: ");

p0 = p_0;

for(k=1;k

for (j=0; j

for (j=0; j

p=p0-f/F;

printf("\tp%d = %f ", k, p);

if(fabs(p-p0)

if (k==max){ printf("\tSolution not found by Newton method "); } }

printf(" Secent method: "); p0 = p_0; p1 = p_1;

for(k=1;k

if (k==max){ printf("\tSolution not found by Secent method "); } } return 0; }

the problem here is that i used scanf to input arguments from the keyboard but it asks to only input arguments from the command line.

I need to change code to fit this. thank you

4 -2 4 -4 201 University of Central Florida COT4500 Spring 2019 k Autho(s) your name or names>

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

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

1. Begin by going to the ERIC website at http://www.eric.ed.gov/

Answered: 1 week ago