This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment4_pH.m
63 lines (40 loc) · 1.41 KB
/
experiment4_pH.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
% Comprehensive Chemical Experiment 4
% Electrochemical synthesis of polyaniline, performance measurement, and application in measuring pH value
% pH value measurement
% Initialization
clear;
clc;
% Original Data
data = table;
data.pH = [2, 4, 5, 6.9, 9]'; % Remember to replace the example with your own data!!!
data.E = [0.2503, 0.1910, 0.1395, 0.0208, -0.1350]'; % Remember to replace the example with your own data!!!
% Fit
% E = p(1) * pH + p(2)
[p, S] = polyfit(data.pH, data.E, 1);
std = table;
std.pH = data.pH;
[std.E, delta] = polyval(p, std.pH, S);
% R Solution
meanValue = table;
meanValue.pH = mean(data.pH);
meanValue.E = mean(data.E);
Lxx = sum(data.pH .^ 2) - length(data.pH) * meanValue.pH .^ 2;
Lyy = sum(data.E .^ 2) - length(data.E) * meanValue.E .^ 2;
Lxy = sum(data.pH .* data.E) - length(data.pH) * meanValue.pH .* meanValue.E;
Rsq = Lxy ^ 2 / (Lxx * Lyy);
% Test
Ex = 0.1533; % Remember to replace the example with your own data!!!
pHx = (Ex - p(2)) / p(1);
% Plot
plotline = table;
plotline.pH = 0: 14;
plotline.E = polyval(p, plotline.pH);
plot(data.pH, data.E, 'ro', plotline.pH, plotline.E, 'b-', meanValue.pH, meanValue.E, 'g*');
xlabel('$$ pH $$', 'Interpreter', 'latex');
ylabel('$$ E / V $$', 'Interpreter', 'latex');
title('$$ E - pH $$', 'Interpreter', 'latex');
hold off;
% Output
fprintf('E = %.2f * pH + %.2f\n', p(1), p(2));
fprintf('R ^ 2 = %.2f\n', Rsq);
fprintf('pH = %.2f\n', pHx);