Remember the example from the video that showed how to return a function handle to a nested function that computed the value of a polynomial? Here it is:
function fh = get_polynomial_handle(p) function polynomial = poly(x) polynomial = 0; for ii = 1:length(p) polynomial = polynomial + p(ii) .* x.^(ii-1); end end fh = @poly;end
It takes a vector of coefficients p, defines a function that returns the value of the polynomial given the scalar input x, and returns a function handle to it. Here is an example run:
>> p = get_polynomial_handle(1:5)p = function_handle with value: @get_polynomial_handle/poly>> p(1)ans = 15
Your task is simple: modify the code above so that it does not use any loops.
Here is the code, i tried;
function fh = get_polynomial_handle(p) function fn = poly1(x) ii = 1:length(p); fn=poly(ii); function polynomial = poly(ii) if ii==1 polynomial= p(1); else polynomial = p(ii).*x.^(ii-1)+ poly(x,ii-1); end end end fh = @poly1;end
Commond window error.
>> p = get_polynomial_handle([1:5])p = function_handle with value: @get_polynomial_handle/poly1>> p(1)Error using get_polynomial_handle/poly1/polyToo many input arguments.Error in get_polynomial_handle/poly1/poly (line 9) polynomial = p(ii).*x.^(ii-1)+ poly(x,ii-1);Error in get_polynomial_handle/poly1 (line 4) fn=poly(ii);
I can not able to figure out, what is wrong in this code. Please help!
Best Answer