% Example 9.7
% Nonlinear model fit for centre of the yield surface
% model: alpha = C*(1-exp(-gamma*eps))

clear all
close all
clc

x = [0.0001 0.005 0.0075 0.008 0.009 0.01 0.02 0.03 0.04 0.05 0.06 0.075 0.1 0.17 0.2];
y = [2.067 3.96 6.22 8.07 9.32 12.02 13.2 15.22 15.51 15.44 15.1 15.76 15.11 15.53 15.26];

% Using the MATLAB function nlinfit
init = [1; 1];
[coeffs,R] = nlinfit(x,y,@(a,x) armstrong(a,x),init);

sr = sum(R.^2);
st = sum(y.^2);
R2 = 1-sr/st;

disp('--------------------------------------------------------------------------')
disp('Model Parameters')
fprintf('      C        gamma       R^2\n')
fprintf('%10.6g %10.6g %10.6g\n',coeffs(1),coeffs(2),R2)

xplot = linspace(min(x),max(x));
yplot = coeffs(1).*(1-exp(-coeffs(2).*xplot));

figure
hold on
plot(xplot,yplot,'LineWidth',2)
plot(x,y,'ok','MarkerSize',7,'MarkerFaceColor','k')
xlabel('Plastic Strain')
ylabel('Alpha')
xlim([min(x),max(x)])
title('Centre of the Yield Surface vs Plastic Strain')