Implement a Matlab function which completes the following task: Give the minimax approximation of a given function f on (a)a closed interval; (b)a set of finitely many points.
时间: 2023-12-10 08:39:58 浏览: 52
Here is a possible implementation of a Matlab function that provides the minimax approximation of a given function f on a closed interval or a set of finitely many points:
```matlab
function [p, err] = minimax(f, a, b, n)
% MINIMAX Computes the minimax approximation of f on [a, b] or on a set of n points.
% Define Chebyshev nodes and Chebyshev polynomials
x = cos(pi*(0:n-1)/(n-1)).';
T = cos(bsxfun(@times, acos(x), 0:n-1));
% Compute function values at the nodes or at the interval endpoints
if isscalar(a) && isscalar(b) % interval case
y = f((a+b)/2 + (b-a)/2*x);
else % points case
y = f(a + (b-a)*x);
end
% Compute the Chebyshev coefficients of the function
c = T\y;
% Construct the minimax polynomial using the Chebyshev coefficients
p = @(xx) (T*c).*cos(bsxfun(@times, acos(xx), 0:n-1))*[1/2; ones(n-2,1); 1/2];
% Compute the maximum absolute error
if isscalar(a) && isscalar(b) % interval case
err = norm(f((a+b)/2 + (b-a)/2*x) - p(x), Inf);
else % points case
err = norm(f(a + (b-a)*x) - p(x), Inf);
end
end
```
The input arguments of the function are:
- `f`: the function to be approximated. It should be a function handle that can be evaluated at a scalar or vector of inputs.
- `a`, `b`: the endpoints of the closed interval on which to approximate the function. If `a` and `b` are scalar, the function is approximated on the interval `[a, b]`. If they are vectors of the same size, the function is approximated on the set of `n=numel(a)` points `(a(1), b(1)), ..., (a(n), b(n))`.
- `n`: the number of Chebyshev nodes to use in the approximation. If `a` and `b` are scalar, `n` nodes are chosen on the interval `[a, b]`. If they are vectors, `n=numel(a)` nodes are used.
The function returns two values:
- `p`: a function handle to the minimax polynomial approximation of `f`.
- `err`: the maximum absolute error of the approximation.
The function works by first computing the Chebyshev nodes `x` and the Chebyshev polynomials `T` up to degree `n-1`. Then, it evaluates the function `f` at the nodes or at the endpoints of the interval, and computes the Chebyshev coefficients `c` of the function using a least-squares fit. Finally, it constructs the minimax polynomial using the Chebyshev coefficients, and computes the maximum absolute error of the approximation.
Note that the function uses vectorized operations to improve efficiency and avoid for loops. Also, the function assumes that `f` is continuous and bounded on the interval or set of points, and that the minimax polynomial exists. If these assumptions are not met, the function may produce inaccurate results or fail.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)