How to calculate error between 2 curves ?

162 visualizzazioni (ultimi 30 giorni)
Nawfel Salha
Nawfel Salha il 24 Mag 2020
Spostato: Dyuman Joshi il 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 Commenti
Alex
Alex il 29 Nov 2022
Spostato: Dyuman Joshi il 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 il 29 Set 2023
I am facing the same challenge. Did you find any solution?

Accedi per commentare.

Risposte (2)

KSSV
KSSV il 24 Mag 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 Commenti
Hugo Keck
Hugo Keck il 25 Mag 2020
Hi, I tried this but what happens when one of y0 values is equal to 0 ?
KSSV
KSSV il 25 Mag 2020
You skip that value ..

Accedi per commentare.


Muhammad Bilal Ahmad
Muhammad Bilal Ahmad il 27 Gen 2024
Modificato: Walter Roberson il 27 Gen 2024
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])}")

Categorie

Scopri di più su Descriptive Statistics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by