remes算法matlab
时间: 2023-11-07 15:59:32 浏览: 44
Remes算法是一种用于函数逼近的算法,可以用来生成多项式,有多种实现方式。以下是一种用MATLAB实现的示例:
```matlab
function [p, err] = remes(x, y, m)
% REMES Compute the approximation of degree m of the function defined by
% the points (x, y) using the Remes algorithm.
% [P, ERR] = REMES(X, Y, M) computes the polynomial P of degree M that
% best approximates the function defined by the points (X, Y) in the
% least-squares sense. ERR is the maximum absolute error of the
% approximation.
%
% Example:
% x = linspace(0, pi, 5);
% y = sin(x);
% [p, err] = remes(x, y, 2);
% xx = linspace(0, pi, 100);
% yy = polyval(p, xx);
% plot(x, y, 'o', xx, yy);
% title(sprintf('Max error: %g', err));
n = length(x);
a = zeros(m+1, m+1);
b = zeros(m+1, 1);
for i = 1:m+1
for j = 1:m+1
a(i,j) = sum(x.^(i+j-2));
end
b(i) = sum(y .* x.^(i-1));
end
c = a \ b;
p = flipud(c);
err = max(abs(polyval(p, x) - y));
end
```
这个函数接受三个输入参数:x和y是定义函数的点的向量,m是多项式的阶数。返回值是多项式系数向量p和最大误差err。使用示例:
```matlab
x = linspace(0, pi, 5);
y = sin(x);
[p, err] = remes(x, y, 2);
xx = linspace(0, pi, 100);
yy = polyval(p, xx);
plot(x, y, 'o', xx, yy);
title(sprintf('Max error: %g', err));
```
这将生成一个包含原始数据点和多项式逼近的图形,并在标题中显示最大误差。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)