# Plotting a function (Horner Schema)

10 ビュー (過去 30 日間)
Luisa Maria Korb 2020 年 7 月 9 日
コメント済み: John D'Errico 2020 年 7 月 9 日 19:32
I am sorry if this question has already been asked, but I am totally new to Matlab and Programming but I need it for my class at university... So the task is to programm the "Horner Schema". This is what I got:
function y = horner(a,x)
n = length(a)-1;
y = zeros(length(x));
for i = n:-1:1
y = y(i)*x+a(i);
end
end
It seems toi work and gives back no errors to me but I don't know, if it gives back the right array.
Anyway, my problem lies within the fact that we should also analyse in the interval [-1,1] and I have absolutely no idea how to do this.
I am glad for any help that you can give me.

#### 5 件のコメント

Luisa Maria Korb 2020 年 7 月 9 日
Thank you, I also found that link but I don't really know what to do with it... I am sorry
Luisa Maria Korb 2020 年 7 月 9 日
I am sorry, English is not my mothertongue... I mean to take some numbers in [-1,1] and replace x in f(x) with these numbers
madhan ravi 2020 年 7 月 9 日
Ask your teacher/professor what they want you to do, seems like you’re not sure what you have to do.

### 採用された回答

John D'Errico 2020 年 7 月 9 日

While polynomic psychological analysis seems well beyond my talents, Horner's rule is pretty easy. :) And since you made a credible effort, I'll look at your code. The solution is easier than you think. You were pretty close, though with a couple of significant mistakes.
It looks like you want to evaluate a polynomial with coeffiicents provided in a, at a vector of points in x. So you preallocated y. That is good. And you looped over the coefficients in a. But first, what does Horner's rule mean?
Given a polynomial represented by coefficients in a, such that
a(n+1)*x^n + a(n)*x^n-1 + a(n-1)*x^n-2 + ... + a(1)
then we can write the polynomial as
a(1) + x*(a(2) + x*(a(3) + x*(a(4) + x*(..... ))))))
Horner's method starts in the very deepest part of the parens in that expression. That is, it starts with
y = a(n+1)
Then you multiply by x, and add the preceding coefficient. Repeat until you are done.
(I will point out that this is the opposite sequence the coefficients are stored for tools like polyfit and polyval., but that is not truly relevant here.)
I've carefully called the function myhorner, since there is also a function called horner in the symbolc toolbox. You can name it as you wish.
function y = myhorner(a,x)
% Horner's method to evaluate a polynomial
% a contains coefficient of the polynomial, stored in increasing order of the power of x.
% x may be a scalar, vector, or array of any size or shape.
% what degree polynomial is this?
n = length(a)-1;
% preallocate y to be the same shape and size as x, but
% initialized to contain copies of a(n+1). repmat serves
% this purpose this perfectly.
y = repmat(a(n+1),size(x));
for i = n:-1:1
% Note use of .* to multiply by x. Recall that y is potentially a vector
% or array, of the same shape and size as x. You wish to multiply every
% element of y by the corresponding element of x.
y = y.*x + a(i);
end
end
I've added comments in there to explain what I did, however the code itself is still very much yours in its flow and methodology.
Finally, some people might recommend not using i as a loop index. I sort of agee with them, as it then makes creating complex numbers more difficult at some point for you. At the same time, I come from an old school of fortran coders where i was always the loop index in all loops, so I am willing to forgive this use. :)
A quick test of the code is in order. First, does it do what we expected in terms of symbolic manipulations?
a = sym('a',[1,5])
a =
[ a1, a2, a3, a4, a5]
syms x
y = myhorner(a,x)
y =
a1 + x*(a2 + x*(a3 + x*(a4 + a5*x)))
expand(y)
ans =
a5*x^4 + a4*x^3 + a3*x^2 + a2*x + a1
Yes. That is indeed perfect.
A numerical test seems important too.
myhorner([1 2 3],[-1 0 1 5])
ans =
2 1 6 86
You can check those values. Or, we can flip the coefficients around, and use polyval as a check.
polyval([3 2 1],[-1 0 1 5])
ans =
2 1 6 86
And, finally, we can plot the function with those coefficients.
fplot(@(x) myhorner([1 2 3],x),[-5,5])
That looks quite reasonably quadratic to me.

#### 2 件のコメント

Luisa Maria Korb 2020 年 7 月 9 日
Thank you so much for your answer! I tried again and I finally got the solution and was also able to plot it in the right way!
John D'Errico 2020 年 7 月 9 日 19:32
:)