In Matlab Function generators. Write a nested function that evaluates a polynomi
ID: 3604735 • Letter: I
Question
In Matlab
Function generators. Write a nested function that evaluates a polynomial of the form y=ax2+bx+cy=ax2+bx+c. The host function genFunc() should be able to take varying number of arguments using varargin with maximum of 3 arguments (a,b,c) to initialize the coefficients of the polynomial. If there is only one argument, then b and c must be set to zero. If there are two input arguments, then c is set to zero. If none are given on input, then the returned function should be zero. If more than 3 arguments exist, then the function should display an error and stop. Also, if the input arguments are not real numbers, then the function should return and error and stop.
On output, the host function should create and return a function handle for the nested function evalFunc(). The nested function should calculate a value of yy for a given value of xx, using the values of aa, bb, and cc stored in the host function. This is called a function generator, since the host function generates and outputs another function that can be called and used later on in the program. Once you create your function generator, test it in the following way: Call genFunc(1,2,0) and save the output function handle in a variable, say h1. Call genFunc(1,2) and save the output function handle in a variable, say h2. Then these two function handles, should give the same result, given the same input x values.
8. Function generators. Write a nested function that evaluates a polynomial of the form | y = ax2 + bx + c. The host function genFuncO should be able to take varying number of arguments using varargin with maximum of 3 arguments (a, b,e) to initialize the coefficients of the polynomial. If there is only one argument, then b and e must be set to zero. If there are two input arguments, then e is set to zero. If none are given on input, then the returned function should be zero. If more than 3 arguments exist, then the function should display an error and stop. Also, if the input arguments are not real numbers, then the function should return and error and stop. On output, the host function should create and return a function handle for the nested function evalFunc). The nested function should calculate a value of y for a given value of x, using the values of a, b, and c stored in the host function. This is called a function generator, since the host function generates and outputs another function that can be called and used later on in the program. Once you create your function generator, test it in the following way: Call genFunc (1,2,0) and save the output function handle in a variable, say h1. Call genFunc(1,2) and save the output function handle in a variable, say h2. Then these two function handles, should give the same result, given the same input x values.Explanation / Answer
You can define functions within the body of another function. These are called nested functions. A nested function contains any or all of the components of any other function.
Nested functions are defined within the scope of another function and they share access to the containing function's workspace.
A nested function follows the following syntax -
function x = A(p1, p2)
...
B(p2)
function y = B(p3)
...
end
...
end
Let us rewrite the function quadratic, from previous example, however, this time the disc function will be a nested function.
Create a function file quadratic2.m and type the following code in it -
function [x1,x2] = quadratic2(a,b,c)
function disc % nested function
d = sqrt(b^2 - 4*a*c);
end % end of function disc
disc;
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of function quadratic2
You can call the above function from command prompt as -
quadratic2(2,4,-4)
MATLAB will execute the above statement and return the following result -
ans = 0.73205
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.