%% Example 1
clc; clear all;
close all;

x = [2100 2300 2500 2700 2900 3100 3300 3500 3700 3900 4100 4300]'; % input
n = [48 42 31 34 31 21 23 23 21 16 17 21]'; % trials
y = [1 2 0 3 8 8 14 17 19 15 17 21]';% number of successes in each trial (y/n)

b = glmfit(x, [y n], 'binomial', 'link', 'probit'); %Fit a generalized linear model
y_pred = glmval(b, x, 'probit', 'size', n); % Predict values for a generalized linear model
plot(x, y./n, 'o', x, y_pred./n, '-') % plot the prediction
xlabel('X')
ylabel('Y/n')
title('Generalized Linear Model Fit')
legend('Real Values','Prediction','Location','northwest')
error=sum((y_pred-y).^2) % error