Evaluating a polynomial can be an \"expensive\'\' thing for a computer. The \"ch
ID: 1996145 • Letter: E
Question
Evaluating a polynomial can be an "expensive'' thing for a computer. The "cheapest'' way to evaluate a polynomial is to use a recur algorithm. For example, if p_4(x) = 3 - 2x + x^2 - 5x^3 + x^4, begin by rewrite it in a modified expanded form as follows: p_4(x) = 3 + x (-2 + x (1 + x (-5 + x))). Then p_4(x) - 3 + xp_3(x), where p_3(x) = -2 + xp_2(x), p_2(x) = 1 + xp_1(x), and P_1 (x) = -5 + x. To evaluate p_4(2) we calculate as follows: P_1(2) = -3 rightarrow P_2(2) = 1 + 2p_1(2) = -5 rightarrow fp_3(2)= -2 + 2p_2(2) = -12 rightarrow P_4(2) = 3 + 2p_3(2) = -21. Write a recourse Mat lab function called reapply to calculate the value of any polynomial function given a list of its coefficients (in ascending order) and a value of x. Program specifications and sample function calls are below. input parameter cleat a vector of coefficients in ascending order input parameter ins value(s) at which to evaluate the polynomial output parameter outs value(s) of the polynomial at ins Matlab has a built-in function called polygala which accomplishes the sun- job as your function reapply but you are forbidden to use it in your program. However. you may use it to your code: reapply(clast, ins) should produce the same value as the built-in Mat lab function polygala (flipper(clast) ins) for any value ins und any vector of coefficients clast.Explanation / Answer
Matlab Code:
function outs = revalpoly(clist,ins)
for j=1:length(ins)
px0 = 1;
for i=length(clist):-1:2
if i==length(clist)%checks the first iteration
px = clist(i-1)+ins(j)*clist(i);
px0 = px;
else%checks the later iterations
px = clist(i-1)+ins(j)*px0;
px0 = px;
end
disp(px0);
end
outs(j)=px0;
end
end
Command window output:
>> revalpoly([3 -2 1 -5 1],[2 3])
-3
-5
-12
-21
-2
-5
-17
-48
ans =
-21 -48
>> polyval(fliplr([3 -2 1 -5 1]),3)
ans =
-48
>>
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.