From 183c55c09e62c069d65b2f5d9f5d7c065ad16228 Mon Sep 17 00:00:00 2001 From: Christian Kolset Date: Wed, 3 Sep 2025 13:19:49 -0600 Subject: Updated entries --- tutorials/module_2/1_problem_solving_strategies.md | 54 ++++++++++ tutorials/module_2/2_num_methods_1.md | 119 +++++++++++++++++++++ tutorials/module_2/num_methods_1.md | 119 --------------------- tutorials/module_2/problem_solving_strategies.md | 54 ---------- 4 files changed, 173 insertions(+), 173 deletions(-) create mode 100644 tutorials/module_2/1_problem_solving_strategies.md create mode 100644 tutorials/module_2/2_num_methods_1.md delete mode 100644 tutorials/module_2/num_methods_1.md delete mode 100644 tutorials/module_2/problem_solving_strategies.md (limited to 'tutorials/module_2') diff --git a/tutorials/module_2/1_problem_solving_strategies.md b/tutorials/module_2/1_problem_solving_strategies.md new file mode 100644 index 0000000..4fa20e9 --- /dev/null +++ b/tutorials/module_2/1_problem_solving_strategies.md @@ -0,0 +1,54 @@ +# Algorithmic thinking + +^da584e + +In engineering, solving a problem begins long before we start coding or building models. Like any other engineering challenge, computational problems must first be clearly framed and understood. In this section, you will learn to **apply algorithmic thinking** to systematically approach engineering problems, **translate real-world situations into structured programming logic**, and **use computational tools to implement, test, and refine solutions**. + +Before diving into code, it's crucial to define the problem carefully, frame the problem so that logically so that a computer can understand then execute so that + + +## Define the Problem + +As any other engineering problem, we need to frame it before we can start working on it. So before jumping straight into coding or building models, clearly define the engineering problem. + +1. List your givens, this includes any constants or equations. What inputs do we know? +2. Find: List what you're trying to solve for. What outputs do we need to find? +3. Establish the assumptions based on your engineering knowledge that you deem to be appropriate to use for the problem. This determines what mathematical models we can apply to the problem (i.e. equations or formulas). +4. Solution: Show the works of the problem, this will include any code used together with documentation or any explanations of the code. +5. Comment: reflect and comment on your findings. + +## Think Algorithmically + +Since we are going to use computers to compute our calculate we first need to break the problem into logical steps that a computer can follow. This can be done with tools such as flowchart or psuedo-code. + +- **Define the inputs and outputs.** What variables will the program take in, and what results will it produce? +- **Break the problem into sub-tasks.** Identify steps such as data input, logic processing and output. +- **Outline the algorithm.** Write pseudo-code or flowcharts that describe the computational steps. +- **Identify patterns or formulas.** Can loops, conditionals, or equations be used to automate parts of the solution? + +### Flowchart for fixing lamp +![Lamp Flowchart](figures/LampFlowchart.png) + +### Psuedo-Code for processing and plotting stress-strain data: +1. Import force and displacement data from file. +2. Convert data from force and displacement to stress and strain. +3. Plot the stress-strain curve. +4. Identify the yield point or modulus. + +## Write & Execute the Code +When writing the code it is important to ask yourself whether you're using the right tools, libraries or method to solve the problem. **Check for any syntax and logic errors** then debug line-by-line using print statements or by using a debugging tool. + +## Verify and Validate +When writing code it is crucial to test and confirm your code. It is therefore important to ask yourself the following questions. Does the code do what you intended it to do? And, is the mathematical model used in the code valid for the current problem? +## Exercise: Rubrics Cube problem + + + +## Exercise: Design a derivative finding algorithm +Set up the problem and write pseudo-code to calculate the gradient of an unknown function. + +1. **Given:** +2. **Find: +3. **Assumptions:** +4. **Solution:** +5. **Comment:** \ No newline at end of file diff --git a/tutorials/module_2/2_num_methods_1.md b/tutorials/module_2/2_num_methods_1.md new file mode 100644 index 0000000..1ab4fa8 --- /dev/null +++ b/tutorials/module_2/2_num_methods_1.md @@ -0,0 +1,119 @@ +# Solving non-linear equations + +^ca27a3 + +## Introduction + + +## Prerequisites + +```python +import numpy +import scipy +import sympy +``` + + +## fsolve from SciPy + +```python +from scipy.optimize import fsolve + +def equations(vars): + x, y = vars + eq1 = x**2 + y**2 - 25 + eq2 = x**2 - y + return [eq1, eq2] + +initial_guess = [1, 1] +solution = fsolve(equations, initial_guess) +print("Solution:", solution) +``` + +## root from SciPy +```python +from scipy.optimize import root + +def equations(vars): + x, y = vars + eq1 = x**2 + y**2 - 25 + eq2 = x**2 - y + return [eq1, eq2] + +initial_guess = [1, 1] +solution = root(equations, initial_guess) +print("Solution:", solution.x) +``` + + +## minimize from SciPy +```python +from scipy.optimize import minimize + +# Define the equations +def equation1(x, y): + return x**2 + y**2 - 25 + +def equation2(x, y): + return x**2 - y + +# Define the objective function for optimization +def objective(xy): + x, y = xy + return equation1(x, y)**2 + equation2(x, y)**2 + +# Initial guess +initial_guess = [1, 1] + +# Perform optimization +result = minimize(objective, initial_guess) +solution_optimization = result.x + +print("Optimization Method Solution:", solution_optimization) + +``` + +## nsolve from SymPy +```python +from sympy import symbols, Eq, nsolve + +# Define the variables +x, y = symbols('x y') + +# Define the equations +eq1 = Eq(x**2 + y**2, 25) +eq2 = Eq(x - y, 0) + +# Initial guess for the solution +initial_guess = [1, 1] + +# Use nsolve to find the solution +solution = nsolve([eq1, eq2], [x, y], initial_guess) +print("Solution:", solution) +``` + +## newton_method from NumPy + +```python +import numpy as np + +def equations(vars): + x, y = vars + eq1 = x**2 + y**2 - 25 + eq2 = x**2 - y + return np.array([eq1, eq2]) + +def newton_method(initial_guess, tolerance=1e-6, max_iter=100): + vars = np.array(initial_guess, dtype=float) + for _ in range(max_iter): + J = np.array([[2 * vars[0], 2 * vars[1]], [2 * vars[0], -1]]) + F = equations(vars) + delta = np.linalg.solve(J, -F) + vars += delta + if np.linalg.norm(delta) < tolerance: + return vars + +initial_guess = [1, 1] +solution = newton_method(initial_guess) +print("Solution:", solution) +``` \ No newline at end of file diff --git a/tutorials/module_2/num_methods_1.md b/tutorials/module_2/num_methods_1.md deleted file mode 100644 index 1ab4fa8..0000000 --- a/tutorials/module_2/num_methods_1.md +++ /dev/null @@ -1,119 +0,0 @@ -# Solving non-linear equations - -^ca27a3 - -## Introduction - - -## Prerequisites - -```python -import numpy -import scipy -import sympy -``` - - -## fsolve from SciPy - -```python -from scipy.optimize import fsolve - -def equations(vars): - x, y = vars - eq1 = x**2 + y**2 - 25 - eq2 = x**2 - y - return [eq1, eq2] - -initial_guess = [1, 1] -solution = fsolve(equations, initial_guess) -print("Solution:", solution) -``` - -## root from SciPy -```python -from scipy.optimize import root - -def equations(vars): - x, y = vars - eq1 = x**2 + y**2 - 25 - eq2 = x**2 - y - return [eq1, eq2] - -initial_guess = [1, 1] -solution = root(equations, initial_guess) -print("Solution:", solution.x) -``` - - -## minimize from SciPy -```python -from scipy.optimize import minimize - -# Define the equations -def equation1(x, y): - return x**2 + y**2 - 25 - -def equation2(x, y): - return x**2 - y - -# Define the objective function for optimization -def objective(xy): - x, y = xy - return equation1(x, y)**2 + equation2(x, y)**2 - -# Initial guess -initial_guess = [1, 1] - -# Perform optimization -result = minimize(objective, initial_guess) -solution_optimization = result.x - -print("Optimization Method Solution:", solution_optimization) - -``` - -## nsolve from SymPy -```python -from sympy import symbols, Eq, nsolve - -# Define the variables -x, y = symbols('x y') - -# Define the equations -eq1 = Eq(x**2 + y**2, 25) -eq2 = Eq(x - y, 0) - -# Initial guess for the solution -initial_guess = [1, 1] - -# Use nsolve to find the solution -solution = nsolve([eq1, eq2], [x, y], initial_guess) -print("Solution:", solution) -``` - -## newton_method from NumPy - -```python -import numpy as np - -def equations(vars): - x, y = vars - eq1 = x**2 + y**2 - 25 - eq2 = x**2 - y - return np.array([eq1, eq2]) - -def newton_method(initial_guess, tolerance=1e-6, max_iter=100): - vars = np.array(initial_guess, dtype=float) - for _ in range(max_iter): - J = np.array([[2 * vars[0], 2 * vars[1]], [2 * vars[0], -1]]) - F = equations(vars) - delta = np.linalg.solve(J, -F) - vars += delta - if np.linalg.norm(delta) < tolerance: - return vars - -initial_guess = [1, 1] -solution = newton_method(initial_guess) -print("Solution:", solution) -``` \ No newline at end of file diff --git a/tutorials/module_2/problem_solving_strategies.md b/tutorials/module_2/problem_solving_strategies.md deleted file mode 100644 index 4fa20e9..0000000 --- a/tutorials/module_2/problem_solving_strategies.md +++ /dev/null @@ -1,54 +0,0 @@ -# Algorithmic thinking - -^da584e - -In engineering, solving a problem begins long before we start coding or building models. Like any other engineering challenge, computational problems must first be clearly framed and understood. In this section, you will learn to **apply algorithmic thinking** to systematically approach engineering problems, **translate real-world situations into structured programming logic**, and **use computational tools to implement, test, and refine solutions**. - -Before diving into code, it's crucial to define the problem carefully, frame the problem so that logically so that a computer can understand then execute so that - - -## Define the Problem - -As any other engineering problem, we need to frame it before we can start working on it. So before jumping straight into coding or building models, clearly define the engineering problem. - -1. List your givens, this includes any constants or equations. What inputs do we know? -2. Find: List what you're trying to solve for. What outputs do we need to find? -3. Establish the assumptions based on your engineering knowledge that you deem to be appropriate to use for the problem. This determines what mathematical models we can apply to the problem (i.e. equations or formulas). -4. Solution: Show the works of the problem, this will include any code used together with documentation or any explanations of the code. -5. Comment: reflect and comment on your findings. - -## Think Algorithmically - -Since we are going to use computers to compute our calculate we first need to break the problem into logical steps that a computer can follow. This can be done with tools such as flowchart or psuedo-code. - -- **Define the inputs and outputs.** What variables will the program take in, and what results will it produce? -- **Break the problem into sub-tasks.** Identify steps such as data input, logic processing and output. -- **Outline the algorithm.** Write pseudo-code or flowcharts that describe the computational steps. -- **Identify patterns or formulas.** Can loops, conditionals, or equations be used to automate parts of the solution? - -### Flowchart for fixing lamp -![Lamp Flowchart](figures/LampFlowchart.png) - -### Psuedo-Code for processing and plotting stress-strain data: -1. Import force and displacement data from file. -2. Convert data from force and displacement to stress and strain. -3. Plot the stress-strain curve. -4. Identify the yield point or modulus. - -## Write & Execute the Code -When writing the code it is important to ask yourself whether you're using the right tools, libraries or method to solve the problem. **Check for any syntax and logic errors** then debug line-by-line using print statements or by using a debugging tool. - -## Verify and Validate -When writing code it is crucial to test and confirm your code. It is therefore important to ask yourself the following questions. Does the code do what you intended it to do? And, is the mathematical model used in the code valid for the current problem? -## Exercise: Rubrics Cube problem - - - -## Exercise: Design a derivative finding algorithm -Set up the problem and write pseudo-code to calculate the gradient of an unknown function. - -1. **Given:** -2. **Find: -3. **Assumptions:** -4. **Solution:** -5. **Comment:** \ No newline at end of file -- cgit v1.2.3