How to calculate error between 2 curves ?
553 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I want to calculate and plot the percentage of error between two curves: (one is interpolated from the other with the function interp1).
The first (experimental) curve is drawn from 3357 points (xi, yi), the interpolated curve is drawn from 274 (xq, yq).
How do I calculate and plot the error?
here is the program:
dataset =xlsread('Ecrouissage-na.xlsx','Sheet1','A5:B3361');
T=dataset(:,1);
H=dataset(:,2);
xi= T.'
yi=H.'
xq = 0 : 0.001 : 0.2732;
yq = interp1(xi,yi,xq,'linear');
plot(xi,yi,'--', xq,yq,'m')
legend('reel','interpolation')
4 comentarios
Alex
el 29 de Nov. de 2022
Movida: Dyuman Joshi
el 15 de Mzo. de 2024
Hello how can you compute the real error?
i.e. Taking a symbolic function and substracting to the interpolation? Then I would integrate in the interval of the interpolation.
The problem is I cannot operate the interpolation (double array) with the symbolic function.
Is there any method in MATLAB to do that?
Respuestas (2)
KSSV
el 24 de Mayo de 2020
If y0 is original value and y1 is obtained value. You can get the error using :
dy = y0-y1 ; % error
abs_dy = abs(y0-y1) ; % absolute error
relerr = abs(y0-y1)./y0 ; % relative error
pererr = abs(y0-y1)./y0*100 ; % percentage error
mean_err = mean(abs(y0-y1)) ; % mean absolute error
MSE = mean((y0-y1).^2) ; % Mean square error
RMSE = sqrt(mean((y0-y1).^2)) ; % Root mean square error
4 comentarios
Hugo Keck
el 25 de Mayo de 2020
Hi, I tried this but what happens when one of y0 values is equal to 0 ?
Muhammad Bilal Ahmad
el 27 de En. de 2024
Editada: Walter Roberson
el 27 de En. de 2024
Aditya Gurjar
ha marcado con alerta este/a respuesta
import numpy as np
import matplotlib.pyplot as plt
# Function definition
def f(x):
return 1/x
# Quadratic polynomial definition
def P2(x):
return 1 - 0.25*(x - 1) + 0.0208*(x - 1)*(x - 2)
# Error bound calculation
h = 4 - 1
M = 0.0208 # Assuming M is an upper bound for the third derivative, derived from the divided difference table
error_bound = (M / 8) * h**2
# Generate x values for plotting
x_values = np.linspace(1, 4, 100)
# Calculate y values for the original function and the quadratic polynomial
y_original = f(x_values)
y_interpolation = P2(x_values)
# Plot the graphs
plt.plot(x_values, y_original, label='f(x) = 1/x', color='blue')
plt.plot(x_values, y_interpolation, label='P2(x)', linestyle='dashed', color='red')
# Mark the interpolation points
plt.scatter([1, 2, 4], [f(1), f(2), f(4)], color='black', marker='o', label='Interpolation Points')
# Highlight the largest real error point
max_error_index = np.argmax(np.abs(y_original - y_interpolation))
plt.scatter(x_values[max_error_index], y_interpolation[max_error_index], color='green', marker='x', label='Max Real Error')
# Add legend and labels
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interpolation and Original Function')
# Display the plot
plt.show()
# Compare real error with error bound
print(f"Error Bound: {error_bound}")
print(f"Real Error at Max Point: {np.abs(y_original[max_error_index] - y_interpolation[max_error_index])}")
0 comentarios
Ver también
Categorías
Más información sobre Interpolation 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!