{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

EGR_102_Lab_10A_Nested_Functions___Neste

EGR_102_Lab_10A_Nested_Functions___Neste - Nested Functions...

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

View Full Document Right Arrow Icon
Nested Functions EGR 102 - Lab 10A 1
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 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
Image of page 5

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

View Full Document Right Arrow Icon
Using the Newton-Raphson method to estimate the root of a function Example: Use the function Newton-Raphson EGR 102 - Lab 10A 6
Image of page 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
Image of page 7

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

View Full Document Right Arrow Icon
function [root] = NR(initial_guess)
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern