您的位置:首页 > 编程语言 > MATLAB

matlab实现插值法sin函数

2015-07-09 23:44 661 查看
插值法实现sin函数:

%calculate and print the sine function
%input: x
%output: sin(x) similar
function y = sin2(x)
%save a copy of x
x_temp = x;
%calculate the interpolation polynomial
%save the coefficient
n = 4;
b = pi/4 + (pi/4)*cos((1:2:2*n-1)*pi/(2*n));
yb = sin(b);
c = newtdd(b, yb, n);c
%for any x, exchange it to base
%interpolation method calculation
len = size(x, 2);
s = ones(1, len);
y = zeros(1, len);
for i = 1 : len
if x(i) < 0
s(i) = s(i) * -1;
x(i) = x(i) * -1;
end
x1(i) =  mod(x(i), 2*pi);
if x1(i) > pi
x1(i) = 2*pi-x1(i);
s(i) = s(i) * -1;
end
if x1(i) > pi/2
x1(i) = pi - x1(i);
end
y(i) = s(i) * nest(n-1, c, x1(i), b);
end
plot(x_temp, y);
grid on
title('sin2(x)');
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: