How to calculate error between 2 curves ?

551 次查看(过去 30 天)
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 个评论
Alex
Alex 2022-11-29
移动:Dyuman Joshi 2024-3-15
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?
Km Shraddha
Km Shraddha 2023-9-29
I am facing the same challenge. Did you find any solution?

请先登录,再进行评论。

回答(2 个)

KSSV
KSSV 2020-5-24
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 个评论
Hugo Keck
Hugo Keck 2020-5-25
Hi, I tried this but what happens when one of y0 values is equal to 0 ?

请先登录,再进行评论。


Muhammad Bilal Ahmad
编辑:Walter Roberson 2024-1-27
此 个回答 已被 Aditya Gurjar 标记
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])}")

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by