c0 and x are scalars, c - vector, and p - scalar. If c is [ ], then p = c0. If c is a scalar, then p = c0 + c*x . Else, p =

4 ビュー (過去 30 日間)
function [p] = poly_val(c0,c,x)
N = length(c);
n=1:1:N;
if (N<=1)
if(isempty(c))
p=c0;
else
p= c0+(c*x);
end
end
if(N>1)
p = c0+(sum(c(n).*(power(x,n))));
end
end
  2 件のコメント
John D'Errico
John D'Errico 2016 年 11 月 10 日
What exactly is your question?
Subramanian Mahadevan
Subramanian Mahadevan 2016 年 11 月 10 日
My question is to create a function which performs in the above mentioned way.

サインインしてコメントする。

採用された回答

Thorsten
Thorsten 2016 年 11 月 14 日
function y = mypolyval(c0,c,x)
if isempty(c)
y = c0;
else
y = c0 + power(x, 1:numel(c))*c(:);
end
The case y = c0 + c*x is already covered by the else. And you can use matrix multiplication of a row and a column vector r*c instead of sum(r.*c').
  2 件のコメント
Vijayramanathan B.tech-EIE-118006077
Vijayramanathan B.tech-EIE-118006077 2018 年 2 月 11 日
This is the easiest method! Well done Mr.Thorsten
*Usage of c(:) is appreciated! :)*
Raunil Raj
Raunil Raj 2018 年 3 月 6 日
really! I went through the same problem. I however don't understand as to how c(:) can convert any row vector or column vector into a column vector. Can someone please explain?

サインインしてコメントする。

その他の回答 (6 件)

Jorge Briceño
Jorge Briceño 2018 年 1 月 29 日
Here is my solution:
function p = poly_val(c0,c,x)
format long
n=(1:1:length(c));
c=c(:)' & This part converts any array/matrix into a colunm vector and transpose...
% it afterwards, since you are working with row vector properties.
if isempty(c)
p=c0;
elseif isscalar(c)
p=c0+sum(c.*x);
else
p=c0+sum((c.*(x.^n)));
end
end

KSSV
KSSV 2016 年 11 月 10 日
編集済み: KSSV 2016 年 11 月 10 日
function [p] = poly_val(c0,c,x)
N = length(c); % length of c
if N == 0 % if c is empty
p = c0 ;
elseif N == 1 % if c is a scalar
p = c0+c*x ;
else % if c is a vector
p = c0+(sum(c.*(power(x,N))));
end
  4 件のコメント
Subramanian Mahadevan
Subramanian Mahadevan 2016 年 11 月 13 日
on changing n to N,isn't the polynomial evaluated for only the last value of n i.e N?
KSSV
KSSV 2016 年 11 月 14 日
N is the number of elements in C.

サインインしてコメントする。


Gabir Yusuf
Gabir Yusuf 2017 年 8 月 8 日
if true
function p = poly_val(c0,c,x)
n=length(c);
if sum(size(c))==0
p = c0;
elseif isscalar(c)
p = c0 + c*x;
else
y=1:n;
z=x.^y;
if size(c)==[1 n]
p=sum(c.*z)+c0;
else
c=c';
p=sum(c.*z)+c0;
end
end
end

Anshuman Panda
Anshuman Panda 2017 年 8 月 19 日
function p=poly_val(c0,c,x) a=length(c); if a==0 p=c0; else if a==1 p=c0+c*x; else p=c0 + power(x , 1:a)*c(:); end end end

Darío Pascual
Darío Pascual 2018 年 3 月 12 日
function p=poly_val(c0,c,x)
N = length(c);
n=1:1:N;
d=size(c);
if(isempty(c))
p=c0;
end
if N==1
p= c0+(c*x);
end
if N>1
if d(1)==1
p = c0+(sum(c(n).*(power(x,n))));
else
c=c'
p = c0+(sum(c(n).*(power(x,n))));
end
end

Govind Mishra
Govind Mishra 2018 年 3 月 14 日
function [p] = poly_val(c0,c,x)
if(iscolumn(c)) c=transpose(c); end
N = length(c); n=1:1:N; if (N<=1) if(isempty(c)) p=c0; else p= c0+(c*x); end end if(N>1) p = c0+(sum(c(n).*(power(x,n)))); end end

カテゴリ

Help Center および File ExchangeDimensionality Reduction and Feature Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by