Creating a polyval function

조회 수: 21 (최근 30일)
Patrick Allaglo
Patrick Allaglo 2013년 12월 13일
편집: ledinh lam 2016년 11월 27일
Hi everybody, I'm actually trying to create a function wich would work like a polyval function but without any S and MU. Just with p and x : polyval(p,x). http://www.mathworks.com/help/matlab/ref/polyval.html But I have absolutely no idea how to do it... I know what i would have to do on a paper but no idea in MATLAB. For example, i don't know how to write this in MATLAB : y = p1*x^n + p2*x^n–1 + … + p*n^x + p*n+1 So if someone could just show me a tip or a way to find it... Thx :)
  댓글 수: 1
raviraja
raviraja 2013년 12월 13일
Same Problem Here... Need Answer

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

채택된 답변

Simon
Simon 2013년 12월 13일
Hi!
But you can use polyval with just p and x!
  댓글 수: 3
Simon
Simon 2013년 12월 13일
Maybe the best point to start is to enter
edit polyval
in your command window and take a look at the code.
Or you start from scratch, write a function that accepts a scalar/vector X and some polynomial coefficients P. Then of course you can directly calculate with
Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)
Maybe use a loop (for scalar X) like
Y = 0;
N = length(P) - 1;
e = N:-1:0;
for n = 1:N+1
Y = Y + P(n) * X^e(n);
end
I didn't try it, but it should get you started.
Patrick Allaglo
Patrick Allaglo 2013년 12월 13일
Well i just tried something and it works (with your loop suggestion :)) Thx

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

추가 답변 (4개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 13일
Example
x=0:10
If you want to calculate a polynomial p=4x^2+2*x+5
p=[4 2 5]
out=polyval(p,x)
  댓글 수: 1
Patrick Allaglo
Patrick Allaglo 2013년 12월 13일
No you don't understand, I'm trying to create a function which would work like polyval. I'm not trying to calculate a polynomial. :)

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


ledinh lam
ledinh lam 2016년 11월 27일
편집: ledinh lam 2016년 11월 27일
function p = poly_val(c0,c,x)
if isempty(c)==true
p =c0;
elseif isscalar(c)==true
p = c0+c*x;
else
w=c(:);
n=length(c);
a=x.^[1:n];
p=c0+a*w;
end
end
if true
% code
end

Jos (10584)
Jos (10584) 2013년 12월 13일
I do not really get your problem, but are you after something like this?
x = -5:5
p = [3 2 4]
y = p(1)
for k=2:numel(p),
y = y + p(k) * x.^(k-1) % you can change this to another formula f(k,p(k),x)
end

Patrick Allaglo
Patrick Allaglo 2013년 12월 13일
Hey again :D I have a little question, how can i save data from a loop with a variable evolving between -5 to 5 for example, because when I try to save data by using variable_stored, it says that i have to use positive integer or logical...
  댓글 수: 1
Simon
Simon 2013년 12월 13일
Make a table like
tt = -5:5;
Access your variable with index -3 like
V(tt==-3)

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

카테고리

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