- https://www.mathworks.com/help/matlab/ref/polyfit.html
- https://www.mathworks.com/help/matlab/ref/polyval.html
- https://www.mathworks.com/help/matlab/ref/fminbnd.html
Minimax approximation problem-How to get P1(x) and maximum error?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Prob. Give the interpolation nodes for the linear near-minimax approximation of this section, for the interval [-1,1]. Give the linear near-minimax approximation for f(x)=e^x on [-1,1].
How to get P1(x) and max|e^x-P1(x)| by using matlab code?
I need matlab code, please.
0 comentarios
Respuestas (1)
Abhimenyu
el 5 de Abr. de 2024
Hi Lee,
From the information shared, I inferred that you want to find the linear near-minimax approximation, P_1(x), of the function f(x) = e^x on the interval ([-1, 1]) and compute the maximum absolute error, max|e^x - P_1(x)|. MATLAB provides various built-in functions for the same.
For linear approximation, the goal is to find a line that best fits (f(x) = e^x) in a minimax sense over the interval. MATLAB's "polyfit" function can be used for the linear approximation and "fminbnd" function to find the maximum error.
Please find the below-attached MATLAB code that uses the above functions to find linear approximation:
% Define the function
f = @(x) exp(x);
% For linear approximation, we use two points. For a near-minimax, let's use
% the endpoints of the interval [-1, 1] for simplicity.
x = [-1, 1];
y = f(x);
% Fit a linear polynomial (degree = 1) to the data
p = polyfit(x, y, 1);
% Define the polynomial approximation function
P1 = @(x) polyval(p, x);
% Function to calculate the absolute error
errorFunc = @(x) abs(f(x) - P1(x));
% Find the maximum absolute error on the interval [-1, 1]
[maxError, maxErrorAt] = fminbnd(@(x) -errorFunc(x), -1, 1);
maxError = -maxError; % Correct the sign since we minimized the negative error
% Display the results
fprintf('The linear near-minimax approximation P1(x) coefficients: [%f, %f]\n', p(1), p(2));
fprintf('Maximum absolute error: %f, at x = %f\n', maxError, maxErrorAt);
% Optional: Plot the function and its approximation
xPlot = linspace(-1, 1, 100);
plot(xPlot, f(xPlot), 'b', xPlot, P1(xPlot), 'r--');
legend('f(x) = e^x', 'Linear Approximation P1(x)');
xlabel('x');
ylabel('y');
title('f(x) and its Linear Near-Minimax Approximation P1(x)');
grid on;
The above mentioned MATLAB code defines f(x) = e^x and a linear approximation P_1(x) using "polyfit" function with endpoints ([-1, 1]) for simplicity. It then finds the maximum absolute error between f(x) and P_1(x))over the interval ([-1, 1]) using "fminbnd" by minimizing the negative of the absolute error function. Finally, it plots f(x) and P_1(x) for visualization.
For more information on "polyfit", "polyval" and "fminbnd" functions, follow the below mentioned MATLAB R2024A documentation links:
Thanks,
Abhimenyu
0 comentarios
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!