# Creating your own Optimization code using numerical methods

Initial Publication Date: September 12, 2022

## Summary

Students were taught how to use numerical methods such as the Golden Section search, Newton-Raphson and Secant method. These methods take a long time when doing by hand and the complexity of optimization problems once the model is achieved would require a long time to perform these calculations by hand or using Spreadsheet. Since they have access to MATLAB, this activity focuses on teaching students how to develop a code to run all three methods with a simple click. They are encouraged to develop their own code and use it in different settings such as chemical engineering, pharmaceutical or food industry related problems.

Used this activity? Share your experiences and modifications

## Learning Goals

The main goal is to show students how computational tools, more specifically, MATLAB help them solve problems faster and in a more reliable way.
Students are less prone to mistakes once the code is ready and, therefore, can focus on the application of problems.
This encourages them to be more creative and use the code as a template for their own purposes.
The key aspect of doing this in class is so that everyone can be helped in the process. Some students are more motivated to develop their own codes or even simplify or make them more complex. They are encouraged to do it so. At the end of the day, every students has that capability.

## Context for Use

This activity is taken by Juniors who are taking numerical methods. The class size for our campus is up to 10 students. This is a primarily undergraduate level 4-year public institution. It is a classroom activity and it requires the entire time of the class - 75 min. Students take computation tools in which they are introduced to MATLAB basic commands and functions. In numerical methods, they have to use these concepts such as loops, calculations, fprintf statements and plotting 2D, 3D graphs to perform optimization on chemical engineering related problems. This activity is a pre-step to garner student's confidence in MATLAB so that they can apply on their own real life situations. This activity is conducted after they were introduced to the optimization topic and have done the calculations by hand and in the spreadsheet. Students feel motivated to write a long code together and they all pay attention to the entire time of the class. They generally need additional help when graphs are employed as I require them to write every single code line.

## Description and Teaching Materials

This activity aims to using MATLAB to simplify and expedite the solution of numerical methods with optimization. They have solved it by hand and with spreadsheet when they reach this point. The purpose is to show them how easy it is to solve other problems using their MATLAB code. This activity started with basic tutorials on the subject and how students understand the concepts better. It also aligns previous MATLAB knowledge from previous classes and it gives a sense of refreshing content and applying them in a more chemical engineering sense. After this activity, students are encouraged to apply the codes on different types of scenarios.
Optimization 1 D tutorial (Acrobat (PDF) 208kB Aug9 22)

## Teaching Notes and Tips

This activity works best in small groups and students who do not feel comfortable writing codes on their own. Students at this point have had at least two classes using MATLAB and should feel comfortable with some basic functions. The main issue with a lengthy code is to use the appropriate variables. Common mistakes include typos and using the same variable for different methods. I encourage students to create their own code as we are working together and check to see if it works. This keeps the class engaging as we work on the code.

## Assessment

The exam that follows an activity like this include a series of problems in which they could reuse the code to find quick answers (otherwise, they would not be able to finish the exam on time). I also add a challenge problem in which they need to be creative and bring a chemical engineering related problem (or one related to their daily activities) and develop their own model and code. This is generally a bonus point.

## References and Resources

MATLAB session - Golden Search: https://www.youtube.com/watch?v=kvaxt3FyDdA
Numerical methods problem 13.8