Problem with Polyval, potential bug

조회 수: 5 (최근 30일)
Robert
Robert 2021년 2월 17일
답변: Steven Lord 2021년 2월 17일
So i am having a bug with poly val.
I am issuing the function two vectors. The first being a vector of coeficients.
The second is a vector that is 1023X1 long. and those are my ex coordinates. It is of type double.
each number in the vector is a very small number 2.0e-9 for example
When polyval runs, it is running the x coordinates as 1,2,3,4,.....1023. so somehow, someway it is using the index of the vector instead of the data in the vector location.
I cant for the life of me figure out why. The vector is 1023X1 so there is no other data in the vector. Clicking on the vector and bringing it up in the table format of the workspace varieified whats in it. I think this is a bug.
Can anyone help?
  댓글 수: 3
Robert
Robert 2021년 2월 17일
I mean this is real simple
When you run this , the plot come sup and shows you the data points.
You can hover over the points with your mouse and it see the coordinates. The coordinates are 1,2,3 not .1,.2,.3
coef [2 0 ]
x = [.1,.2.3]
plot(polyval(Coef,x))
Robert
Robert 2021년 2월 17일
so in short, its evaluating the polynomial at the index not the data in the index
I am on 2018rb

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2021년 2월 17일
This is not a bug and this behavior is not caused by polyval but by plot. What happens if you call plot with one input argument? According to the documentation:
"plot(Y) creates a 2-D line plot of the data in Y versus the index of each value.
  • If Y is a vector, then the x-axis scale ranges from 1 to length(Y)."
So if you want to plot the values of the polynomial at the specified X coordinates, you need to pass those X coordinates into both polyval (to determine the Y coordinates of the points in the polynomial) and plot (to plot those points at the appropriate X coordinates.)
p = [1 2 1];
x = (1:12)./4;
subplot(2, 1, 1)
plot(polyval(p, x), 'o-')
title('One input to plot uses indices')
subplot(2, 1, 2)
plot(x, polyval(p, x), 'o-')
title('Two inputs to plot uses data')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by