%% FINDING INVERSE ERROR FUNCTION
% In a previous blog at autarkaw.wordpress.com (Sep 1, 2010), we set up a
% nonlinear equation to find the inverse error function.
% In this blog, we will solve this equation
% by using interpolation.
% The problem is given at
% http://numericalmethods.eng.usf.edu/blog/inverseerror.pdf
% and we are solving Exercise 2 of the pdf file.

%% TOPIC
% Finding inverse error function

%% SUMMARY

% Language : Matlab 2008a; 
% Authors : Autar Kaw; 
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/inverse_erf_interp_matlab.m; 
% Last Revised : October 4 2010
% Abstract: This program shows you how to find the inverse error function
% using interpolation
clc
clear all

%% INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you how to')
disp('   find the inverse error function')
disp(' ')
disp('AUTHOR')
disp('   Autar K Kaw of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp(' http://numericalmethods.eng.usf.edu/blog/inverse_erf_interp_matlab.m')
disp('  ')
disp('PROBLEM STATEMENT')
disp(' http://numericalmethods.eng.usf.edu/blog/inverseerror.pdf  Exercise 2')
disp(' ')
disp('LAST REVISED')
disp('   October 4, 2010')
disp(' ')

%% INPUTS
% Value of error function
erfx=0.1125;
% Table of erf(x) vs x
xx=[0  0.1  0.25  0.75  1.0  1.5  2.0  5.0];
erfxx=[0  0.1125  0.2763  0.7112  0.8427  0.9661  0.9953  1.0000];

%% DISPLAYING INPUTS

disp('INPUTS')
fprintf(' Inverse error function is to be found for= %g',erfx)
disp('  ')
disp(' Given erf(x) vs x values')
disp('_______________________')
disp('    x          erfx  ')
disp('________________________')
dataval=[xx;erfxx]';
disp(dataval)

%% CODE
if erfx>1.0 | erfx<0
    disp('Invalid value. erf(x) only takes values in [0,1] range')
else
inverse_erf=interp1(erfxx,xx,erfx,'cubic');
%% DISPLAYING OUTPUTS

disp('OUTPUTS')
fprintf(' Value of inverse error func from this mfile is= %g',inverse_erf)
fprintf(' \n Value of inverse error func from MATLAB is    = %g',erfinv(erfx))
disp('  ')
end