In: Computer Science
Use MATLAB to write a function subroutine horner_yourlastname(a, x0, d) that uses Horner’s algorithm to evaluate a polynomial at a given point x0.
This code should have three inputs in the order specified above:
a: a vector of coefficients of the polynomial of interest. The first element of the vector should be the leading coefficient, and the last element should be the trailing coefficient. i. e. The polynomial p(x) = anx n + an−1x n−1 . . . a1x + a0 should have associated vector a = [an, an−1, . . . , a1, a0].
x0: the point at which the polynomial is to be evaluated.
d ∈ {0, 1}: a toggle that allows the function to be evaluated at the point if d = 0 or its derivative to be evaluated at the point if d = 1.
Hey,
Note: Brother function file are not meant to be run directly they are either run through the command window or some script and should be saved with the same name mentioned below. In case of any queries brother just comment in box I would be very happy to assist all your queries.
=====================================
Save file as horner_yourlastname.m
=====================================
function y=horner_yourlastname(a, x0, d)
y=0;
if(d==0)
for i=1:length(a)
y=y*x0+a(i);
end
else
n=length(a)-1;
vec=n:-1:1;
a(length(a))=[];
v=a.*vec;
for i=1:length(v)
y=y*x0+v(i);
end
end
end
=====================================
Executable command to test
====================================
horner_yourlastname([5:-1:1],1.3,1)
Kindly revert for any queries
Thanks.