While Loop Applications using the Newton-Rhapson Method for Polynomials

Matthew Zelisko
University of Houston, Engineering
Author Profile


This activity demonstrates the possible uses for while loops in the context of a common numerical method, the Newton-Rhapson method for root finding (sometimes referred to as Newton's method). The problem given requires using while loops for: data validation, error minimization, and controls.

Learning Goals

Learning Objectives:
- How to apply and implement while loops in various contexts
- Find the roots of a generic polynomial using Newton-Rhapson Method

MATLAB is used to to solve the classic Newton-Rhapson numerical method for finding the roots of an equation. This problem focuses on polynomial equations so that the user can input any set of coefficients to get an answer.

The students will need to develop an algorithm to solve the problem and address all of the tasks. This will involve critical thinking about how to convert a numerical method that could easily be done by hand into a MATLAB code. The error minimization illustrates how while loops can be used in an iterative process to arrive at an acceptable answer using a numerical method.

Context for Use

This problem was originally developed as part of an assignment for a University Freshmen level computing and problem solving course. It was designed to be a part of an individual homework assignment, but could easily be converted to a longer in-class activity. Depending on the abilities of the students, and their prior knowledge of coding and numerical methods this problem could take between 60 and 120 minutes.

To complete the assignment, students will need to know how to use while loops as well as basic inputs, outputs, and conditional (if) statements. A few other simple functions are necessary, but the students should be able to learn these on their own if not covered. Examples of these extra functions include: polyval, polyder, and mod.

This activity/problem is generally given when the students have had only a few weeks of working with MATLAB so the syntax and code itself is not too complicated. It is best suited for right after learning while loops or as a review of while loops as it covers many possible applications. The background information is presented that a student with only a basic understanding of calculus (i.e. what is a derivative) should be able to complete the assignment.

Description and Teaching Materials

All descriptive materials are attached with sample outputs of what would be the expected outcome.
Problem Description (Acrobat (PDF) 569kB Aug16 18)

Teaching Notes and Tips

No solution is attached so that this problem could be used as a homework assignment if necessary.


This problem is assessed by running the student's code with various different test cases and comparing the results to a known working code. Each task will need to be tested multiple times to make sure it is working correctly.

For example:
Task 1: Data validation
- Test case 1: coefficients given for an odd order polynomial; expected result is no error/warning
- Test case 2: coefficients given for an even order polynomial; expected result is code should ask for inputs to be re-entered
- Test case 3: coefficients given for an even order polynomial 5 times in a row; expected result is a warning that the code is ignoring the last coefficient entered and continuing

These same sample test cases could also be used to make sure the other tasks in the problem are working as expected.

References and Resources

Any reference to using the Newton-Rhapson method should be sufficient. There is a reference given in the attached problem description.