필터 지우기
필터 지우기

Is anything wrong with Eval function? I get value of pn =1 for all x values.

조회 수: 2 (최근 30일)
Muhtasim Ahmed
Muhtasim Ahmed 2018년 4월 8일
댓글: Are Mjaavatten 2018년 4월 9일
function HW_4_8()
y= @(x)(1/(1+25*x.^2));
%x=linspace(-1,1,41);
for i=0:5
x=cos(i*pi/40);
end
fprintf('Coefficient of the newton interpolation\n\n');
a= Coef(x,y);
for i=1:length(a)
fprintf('%5.0f\t%15.10f\n',i-1,a(i));
end
xp=linspace(-1,1,200);
fprintf('Approximate value of y(x)\n');
fprintf(' n x p(x)');
fprintf( ' y(x) diff\n');
for i=1:length(xp)
p=xp(i);
funval=(1./(1+25*p.^2));
% fplot(funval,[-1,1]);
pn= Eval(x,a,p);
D=abs(funval-pn);
% disp(D);
% fplot(diff,[-1,1]);
fprintf('%5.0f\t%5.10f\t%15.10f\t%15.10f\t%15.10f\n',i,p,pn,funval,D);
end
%fplot(D,[-1,1]);
end
function[a]=Coef(x,y)
mx=length(x);
my=length(y);
% if mx ~= my
% error('must be same lenght');
% end
T=zeros(mx,mx);
T(:,1)=(my)';
for j=2:mx
for i=1:(mx-j+1)
T(i,j)=(T(i+1,j-1)-T(i,j-1))/(x(i+j-1)-x(i));
end
end
a=T(1,:);
end
function pn=Eval(x,a,p)
m=length(a);
sum=0;
for i=1:m
temo=1;
for j=1:i-1
temo=temo*(p-x(j));
end
sum =sum+a(i)*temo;
end
pn=sum;
end
  댓글 수: 4
Are Mjaavatten
Are Mjaavatten 2018년 4월 9일
Thanks for the feedbacks from both of you.
Guillaume: This started out as a brief comment on the x vector, but it sort of grew as I wrote. I should have considered changing it to it an answer.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by