{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

EGR_102_Lab_10A_Nested_Functions___Neste

# EGR_102_Lab_10A_Nested_Functions___Neste - Nested Functions...

This preview shows pages 1–9. Sign up to view the full content.

Nested Functions EGR 102 - Lab 10A 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Nested Functions It is possible to call other functions (nested) from inside a main function (primary) The primary function and all the nested functions must be saved within the MATLAB Current Folder It is useful for complex programs where the same function may be used multiple times It allows users to create programs which can be broken down into logical blocks or subroutines It is easier to read and understand a program in which each function completes a specific purpose EGR 102 - Lab 10A 2
Example First, create two nested functions which will be used later within the primary function Each file should be in its own M-file, both in the same directory In this case, we will create two simple functions called add and subtract . function [output] = add(input) % Comments… output = input+10; function [output] = subtract(input) % Comments… output = input-5; EGR 102 - Lab 10A 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Example 1 Create a primary function which calls the other two functions: The primary function nested1 takes a number as input. It passes that value to the nested add function which adds 10 to the value. Then, the output of the nested add function is passed to the nested subtract function, which subtracts 5. function [output2] = nested1(input1) % Comments output1 = add(input1); input2 = output1; output2 = subtract(input2); EGR 102 - Lab 10A 4
Example 2 Create a primary function which uses a nested function repeatedly within a loop: The primary function nested2 takes a number as input. It then runs the nested add function ten times within a for loop. function [output] = nested2(input) % Comments for i = 1:10 output = add(input); input = output; end EGR 102 - Lab 10A 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Using the Newton-Raphson method to estimate the root of a function Example: Use the function Newton-Raphson EGR 102 - Lab 10A 6
Newton-Raphson First, create two nested functions for f(x) and f’(x) Each file should be in its own M-file, both in the same directory function [yprime] = fprime(x) % Comments yprime = -exp(-x) - 1; function [y] = f(x) % Comments y = exp(-x) - x; EGR 102 - Lab 10A 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
function [root] = NR(initial_guess)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern