How to calculate error between 2 curves ?

551 views (last 30 days)
Nawfel Salha
Nawfel Salha on 24 May 2020
Moved: Dyuman Joshi on 15 Mar 2024
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 Comments
Alex
Alex on 29 Nov 2022
Moved: Dyuman Joshi on 15 Mar 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?
Km Shraddha
Km Shraddha on 29 Sep 2023
I am facing the same challenge. Did you find any solution?

Sign in to comment.

Answers (2)

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

Sign in to comment.


Muhammad Bilal Ahmad
Muhammad Bilal Ahmad on 27 Jan 2024
Edited: Walter Roberson on 27 Jan 2024
This answer was flagged by 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])}")

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!