EGR_102_Lab_10A_Nested_Functions___Neste

EGR_102_Lab_10A_Nested_Functions___Neste - Nested Functions...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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
Background image of page 5

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

View Full DocumentRight 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
Background 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
Background image of page 7

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

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

This note was uploaded on 04/02/2012 for the course EGR 100 taught by Professor Hinds during the Fall '08 term at Michigan State University.

Page1 / 25

EGR_102_Lab_10A_Nested_Functions___Neste - Nested Functions...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online