How to calculate error between 2 curves ?

169 ビュー (過去 30 日間)
Nawfel Salha
Nawfel Salha 2020 年 5 月 24 日
移動済み: Dyuman Joshi 2024 年 3 月 15 日
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 ?
KSSV
KSSV 2020 年 5 月 25 日
You skip that value ..

サインインしてコメントする。


Muhammad Bilal Ahmad
Muhammad Bilal Ahmad 2024 年 1 月 27 日
編集済み: Walter Roberson 2024 年 1 月 27 日
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 Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by