# Example Bisection Method Problem in MATLAB Grader

## Summary

Here's an example of a text book problem in **Numerical Methods** that was converted to a **MATLAB Grader** assignment to assess students in a more automated and interactive way. Other problems can be modeled and updated based on this **Bisection Method** example.

**your**modifications and improvements to this activity through the Community Contribution Tool »

## Learning Goals

**For teachers/instructors:**

- Convert an example problem to a MATLAB Grader assignment by:

a. stating the Problem Description and Instructions,

b. writing and configuring the Reference Solution and the Learner Template codes, and

c. setting up the Assessment codes.

**For students:**

- Learn to correctly modify a MATLAB script with an anonymous function (function_handle class) and a custom function (function in a script),
- properly use a while loop (with a stop condition), and
- approximate the root (solution to 1 equation with 1 unknown) using the bisection method.

## Context for Use

The Hofstra University Numerical Methods course is listed in engineering (ENGG 101), computer science (CSC 102) and math (MATH 147) where we use MATLAB for approximating solutions to different types of problems including solving for a single and system of equations (finding the roots), minimum and maximum (optimization), curve-fitting (via regression and interpolation), numerical differentiation and integration, and solving ordinary differential equations (with initial and boundary values). We usually get about 20 students per class. I first teach them the MATLAB fundamentals (concept only used in class) prior to using vectors and matrices and iterations. Assignment problems (such as the example provided here) are accessed and solved in MATLAB Grader for honing the students' MATLAB skills and for implementing various numerical methods.

## Description and Teaching Materials

Send me an email (roche.c.deguzman@hofstra.edu) for access to the actual **MATLAB Grader** assignment problem example.

The students are presented with a physics problem with a given equation: F = (1/(4*pi*e0))*((q*Q*x)/(x^2+a^2)^(3/2)).

All parameters (F, pi, e0, q, Q, and a) are known except for one unknown (x). The units are in SI and conversion is not needed.

The goal of the assignment problem is to use the numerical technique called the **bisection method** to approximate the unknown value at a specified stopping condition.

The equation can be rearranged so that the left side is zero: 0 = (1/(4*pi*e0))*((q*Q*x)/(x^2+a^2)^(3/2))-F.

If the right side is simplified to a very small value, such as 0.0001 or -0.0001, then the value of the unknown x is numerically obtained, xr = x root = solution.

The right side can be assigned as an anonymous function (function_handle class) with x input:

**f = @(x) (1/(4*pi*e0))*((q*Q*x)/(x^2+a^2)^(3/2))-F**.

The solution: xr will be achieved when abs(yr)<=0.0001, where yr = f(xr).

To implement the bisection method, an initial bracket [xL, xU] containing two values (lower and upper x) need to be specified provided that xr is within: xL<=xr<=xU.

The value of the root (midpoint of the bracket) is then computed per iteration (until stop):

**xr = (xL+xU)/2**,

then the bracket is updated based on the condition below:

- if f(xL)*f(xr)<0, then xU = xr (the analytical x is within the bracket [xL, xr], hence xU = xr), or
- if f(xr)*f(xU)<0, then xL = xr (the analytical x is within the bracket [xr, xU], hence xL = xr).

**Problem Description and Instructions**section of MATLAB Grader. Note that the instructions should be limited to what you need for the students to know initially and the expected variable outputs. For example, the stopping condition and initial bracket are provided, and I expected them to solve for xr, yr, and i.

**Live Script code in MATLAB**(make the script as clear and simple as possible) AssignBisect.mlx (MATLAB Live Script 4kB Oct18 21), it can now be copied and pasted in the

**Reference Solution**of the

**Code**section, selecting the

**Problem Type**as

**Script**. A user-made or custom function (fx) can be written at the end of the script, enclosed by blue keywords: function and end pair, by providing a fx name (Assign1), inputs (xL, xU, ystop, and f), and outputs (xr, yr, and i), and solving for the outputs using input information. Within the fx, a while loop with a nested if statement is used. The opposite of the stopping condition is implemented in the while condition, called a running condition (the opposite of abs(yr)<=ystop is abs(yr)>ystop). All non-coded lines have to be commented out. The fx can now be used within the script to solve for the outputs: xr (solution), yr (f(xr) with value close to zero), and i (number of iterations to accomplish the solution). The semi-colon can be removed to display the values in the Command Window of the Live Script after running.

**Learner Template**then trimmed and edited to remove the information you want your students to complete. In this problem, students are required to assign all known values, write the anonymous function equation, bisection method equation, and while and if conditions.

To evaluate the students' performance in this problem, 3 **Assessment** methods were added (called **Test**). All assigned Test Type were MATLAB Code. The point distribution, totaling to 10 points, are as follows:

- Test 1: Given values = 3 points,
- Test 2: Anonymous function = 3 points, and
- Test 3: Root, f(root), iterations = 4 points

In the **Assessment Method**, **Weighted** is selected to implement the relative weight to each test assigned as: 30%, 30%, and 40%, respectively, then just multiplied to 10 after acquiring the students' scores.

Test 1 is to check the correctness of the known or given values. In the MATLAB Code section, the field names of the structure variable called **referenceVariables** are stored as separate variable names. For example, xLR = referenceVariables.xL. I added "R" to specify that these are the reference or correct values. The **cellfun** fx is used so evaluate all reference values compared to student inputs. Variables are vectorized as cells: reference (in double) and student inputs from script (in char class) and placed as 2nd and 3rd inputs. The 1st input is a function_handle (@) using the **assessVariableEqual** fx. If at least one given student input is incorrect then the **Feedback on Incorrect** message will show up.

Test 2 is to evaluate the anonymous function student input. For assessment, after extraction from the structure variable, it is compared to the student input that is previously-converted to symbolic class using assessVariableEqual. Getting this correct is needed for the bisection method (or other finding the roots methods) to work properly.

Finally, Test 3 is to check for the expected outputs: xr, yr, and i, implementing a similar method as in Test 1. In MATLAB Grader, under Assignment **Actions**, **Report** can be generated as an Excel file containing students submission information including solution codes and correct scores. I have written a MATLAB script

**Validate Reference Solution**button is clicked to check if the Assessments are working, prior to

**Save as Final**for posting the problem to students added as emails in

**Manage People**.

## Teaching Notes and Tips

- It takes a lot of time and effort to develop even a single problem but it is worth it since assignment problems in MATLAB Grader can be reused every semester and modification and improvement is quick and easy.
- A
**Function**Problem Type can be selected instead of a Script (similar to solving Cody problems ) in which variable names do not matter but I think is better suited for more advanced student users. - The current example bisection method problem can be tweaked to implement other finding the roots methods.
- bracketing method: change xr
- false position or linear interpolation method: xr = xU-f(xU)*(xL-xU)/(f(xL)-f(xU))

- open methods: provide a single initial value (xi), change the xr, add xi = xr, and remove the if statement in the loop
- fixed-point iteration: xr = f(xi)+xi
- Newton-Raphson: xr = xi-f(xi)/f1(xi), where f1 = matlabFunction(diff(sym(f)))
- sym converts f to symbolic
- diff gets the symbolic derivative of the function
- matlabFunction converts symbolic to a function_handle

- bracketing method: change xr

**your**modifications and improvements to this activity through the Community Contribution Tool »

## Assessment

**MATLAB Grader Grading** (Based on a 10 score)

The MATLAB Grader assessments were designed that Test 1 is a prerequisite for Test 2, and Test 2 for Test 3.

- Exemplary: 10 (3+3+4, correct for all Tests)
- Satisfactory: 6 (3+3, correct for Test 1 and 2)
- Unsatisfactory: 0-3 (0 or 3, correct or incorrect for Test 1)

**In Class Grading** (Based on a 10 score)

Students submit and explain different sections of their work.

*Scoring Rubric*

Exemplary Satisfactory Unsatisfactory

Working anonymous and custom fxs 3: working fxs, knows how to change inputs and outputs 2: working fxs 0-1: both or only one fx is working, cannot explain

Proper while loop 3: working with a running condition, monitors iterations 2: working while 0-1: runaway loop, incorrect structure

Correct root 4: correct formulas, and outputs: xr, yr, and i, with comments 2-3: correct xr 0-1: incorrect structure and values

## References and Resources

MathWorks - Makers of MATLAB and Simulink - MATLAB & Simulink

Textbook: Applied Numerical Methods with MATLAB for Engineers and Scientists, 4th Ed. (McGraw-Hill, 2017) by Steven C. Chapra