0% found this document useful (0 votes)
10 views3 pages

A1 2

This document provides a MATLAB implementation for non-linear regression of the Butler-Volmer kinetic equation using data from an Excel file. It includes functions for calculating the fitted curve, its derivatives, and the R-squared value to assess the fit quality. The results indicate coefficients a0 and a1, along with a high R-squared value, suggesting a strong fit to the data.

Uploaded by

Saalik Mubeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

A1 2

This document provides a MATLAB implementation for non-linear regression of the Butler-Volmer kinetic equation using data from an Excel file. It includes functions for calculating the fitted curve, its derivatives, and the R-squared value to assess the fit quality. The results indicate coefficients a0 and a1, along with a high R-squared value, suggesting a strong fit to the data.

Uploaded by

Saalik Mubeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

% MATLAB Implementation of Non-Linear Regression for Butler-Volmer Kinetic Equation

clc; clear; close all;

% Read Excel file


file_path = 'Homework1_Data_410bf06f-67af-423c-af64-13d71207a314.xlsx';

data = readtable(file_path, 'Sheet', 'Sheet1');

% Extract variables
x = data.Voltage;
y = data.Current;

% Perform non-linear regression


[a0, a1] = non_linear_reg(x, y, 1, 5, 1000, 1e-6);

% Generate predicted values


y_pred = func(x, a0, a1);

% Calculate R-squared
r2 = calculate_r2(y, y_pred);

% Plot results
figure;
scatter(x, y, 'bo', 'DisplayName', 'Data points'); hold on;

% Generate smooth curve for the fitted function


x_smooth = linspace(min(x), max(x), 200)';
y_smooth = func(x_smooth, a0, a1);
plot(x_smooth, y_smooth, 'r-', 'LineWidth', 2, 'DisplayName', 'Fitted Curve');

xlabel('Voltage');
ylabel('Current');
title('Butler-Volmer Kinetic Equation Fit');
legend;
grid on;
hold off;

% Display results
fprintf("a0 = %.6f\n", a0);
fprintf("a1 = %.6f\n", a1);
fprintf("R² = %.6f\n", r2);

% --- Function Definitions --- %

% Function to compute f(x) = a0 * sinh(a1 * x)


function y = func(x, a0, a1)
y = a0 .* sinh(a1 .* x);
end

% Partial derivative w.r.t a0


function y = d_func_a0(x, a1)
y = sinh(a1 .* x);
end

% Partial derivative w.r.t a1


function y = d_func_a1(x, a0, a1)
y = x .* a0 .* cosh(a1 .* x);
end
% Non-linear regression function using iterative optimization
function [a0, a1] = non_linear_reg(x, y, a0, a1, it, err_tol)
coeff = [a0; a1]; % Initial coefficient vector
n = length(x);

for i = 1:it
% Compute Jacobian matrix Z
Z = zeros(n, 2);
Z(:, 1) = d_func_a0(x, a1);
Z(:, 2) = d_func_a1(x, a0, a1);

% Compute A = Z^T * Z
A = Z' * Z;

% Compute f(x) with current coefficients


f = func(x, a0, a1);

% Compute b = Z^T * (y - f)
b = Z' * (y - f);

% Solve for coefficient updates


del_a = A \ b;
coeff_new = coeff + del_a;

% Compute relative error


err = max(abs((coeff - coeff_new) ./ coeff));

% Check for convergence


if err < err_tol
break;
end

coeff = coeff_new;
a0 = coeff(1);
a1 = coeff(2);
end
end

% Function to calculate R²
function r2 = calculate_r2(y_true, y_pred)
ss_res = sum((y_true - y_pred).^2);
ss_tot = sum((y_true - mean(y_true)).^2);
r2 = 1 - (ss_res / ss_tot);
end

a0 = -0.000289
a1 = -10.884055
R² = 0.999463

You might also like