# I can't find the mistake in the code

2 visualizaciones (últimos 30 días)
Jesus Alejandro Rodriguez Morales el 20 de Sept. de 2020
Respondida: Alberto Zekry el 14 de Oct. de 2020
The following code takes a vector of coefficient p, defines a function that returns the value of the polynomial given the scalar input x, and returns a function to handle it. However, when the code is assessed with random polynomials fails. For example, "Incorrect answer for pf = poly_fun([ 0 6 7 5 0 8 1 8 6 -7 -4 ])". Any suggestions?
function fh = poly_fun(p)
function polynomial = poly(x)
n = length(p)-1;
polynomial = sum(p.*x.^(n:-1:0));
end
fh = @poly;
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Steven Lord el 20 de Sept. de 2020
I've seen two different conventions for representing polynomials as vectors of coefficients. The one used by functions like polyfit and polyval has the high order term as the first element.
p = [1 2 3];
x = 4;
v1 = polyval(p, x) % x^2+2*x+3 evaluated at x = 4 is 27
The other has the high order term as the last element.
v2 = p(1)+x*p(2)+x.^2*p(3) % 1+2*x+3*x^2 evaluated at x = 4 is 57
Based on the fact that the sample polynomial used in the grading of your assignment has 0 as its first element, I'm wondering if they're using the latter convention.
Alternately, does your assignment say your function should return a function handle or the numeric result of evaluating that polynomial?
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Jesus Alejandro Rodriguez Morales el 21 de Sept. de 2020
Thank you for your answer, Steven. Yeah, the function should return a function handle and after we put the value of x should show the numeric result. For example, if I run the following values works perfectly.
pf = poly_fun(1:5);
pf(1)
pf = poly_fun(ones(1,10));
pf(2)
But, when the grader run something like pf = poly_fun([ -10 5 0 -4 -4 2 -2 10 -6 ]) my function fail the test.

Iniciar sesión para comentar.

### Más respuestas (3)

Thiago Henrique Gomes Lobato el 20 de Sept. de 2020
You can't use "abs" since the coefficients/x values can be negative.
##### 2 comentariosMostrar NingunoOcultar Ninguno
Jesus Alejandro Rodriguez Morales el 20 de Sept. de 2020
Thanks for the observation, the code was without the "abs", I just edited. Any suggestions?
Thiago Henrique Gomes Lobato el 20 de Sept. de 2020
Depending of your inputs the direction of the sum may be wrong. This code is basically the same as yours but using the "(... ,2)" in the sum so the dimensions are right:
n = length(p)-1;
polynomial = @(x,p)(sum(p.*x.^((length(p)-1):-1:0),2 ));
A = polynomial( [-1:0.01:1]',[ 0 6 7 5 0 8 1 8 6 -7 -4 ] ); % Note the array dimensions
B = polyval([ 0 6 7 5 0 8 1 8 6 -7 -4 ],[-1:0.01:1]');
norm(A-B) % Compare with matlab polyval
ans =
1.9746e-14

Iniciar sesión para comentar.

Hèrren Thomas D'Souza el 27 de Sept. de 2020
If you workout the algorithm on a piece of paper or mentally by giving smaller arrays as input co-effcients like,
>> z = poly_fun([-1,2])
>> ans = z(2)
You'll now understand clearly how polynomial = sum(p.*x.^(n:-1:0)); is working.
For your logic to work, you can use, flip(p) before the nested function. Or you can use the same sum function to iterate from 0, without needing to flip.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Alberto Zekry el 14 de Oct. de 2020
function fh = poly_fun(p)
polynomial=0
function polynomial = poly(x)
polynomial=polyval(p(end:-1:1),x);
end
fh = @poly;
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Logical en Help Center y File Exchange.

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by