MATLAB Answers

Polynomial with nested and anonymous functions.

10 views (last 30 days)
Wai Han
Wai Han on 22 Oct 2020
Commented: Ameer Hamza on 23 Oct 2020
How can I write this polynomial function without loops?
function fh = get_polynomial_handle(p)
function polynomial = poly(x)
polynomial = 0;
for i = 1 : length(p)
polynomial = polynomial + p(i) .* x.^(i-1);
fh = @poly;
% I try to make this function recursively, but anyway, I am having some errors in writting this function without loop.
% Can anyone explain me?
% Note: The function is not necessary to be recursive, it's just one of the ways I've thought of.


Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 22 Oct 2020
Something like this
polynomial = sum(p .* x.^(0:numel(p)-1));
Also see polyval().

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!