function P5()
% Checks out HConTriangle on a given function MyF(x,y) defined below.
% It is recommended that you play with the vertices different MyF's of your
% own design.

% The vertices of the triangle and the function values...
x1 = 1.00; y1 = 2.3; [Fval1,Fx1,Fy1] = MyF(x1,y1);
x2 = 1.10; y2 = 2.2; [Fval2,Fx2,Fy2] = MyF(x2,y2);
x3 = 1.05; y3 = 2.5; [Fval3,Fx3,Fy3] = MyF(x3,y3);

% Encapsulate...
P = {struct('x',x1,'y',y1),...
struct('x',x2,'y',y2),...
struct('x',x3,'y',y3)};
F = {struct('fvalue',Fval1,'fx',Fx1,'fy',Fy1),...
struct('fvalue',Fval2,'fx',Fx2,'fy',Fy2),...
struct('fvalue',Fval3,'fx',Fx3,'fy',Fy3)};
clc
fprintf(' alpha(1) alpha(2) alpha(3) |fTrue - fAppx|\n')
fprintf('-----------------------------------------------------------\n')
% "Cover" the triangle with sample points...
for alfa1 = 0:.125:1
for alfa2 = 0:.125:1-alfa1
alfa3 = 1-alfa1-alfa2;
% Using Hermite cubic interpolation...
fAppx = HConTriangle(P,F,[alfa1,alfa2,alfa3]);
% The exact value...
xtilde = alfa1*P{1}.x + alfa2*P{2}.x + alfa3*P{3}.x;
ytilde = alfa1*P{1}.y + alfa2*P{2}.y + alfa3*P{3}.y;
[fTrue,Fx,Fy] = MyF(xtilde,ytilde);
% Display...
fprintf(' %5.3f %5.3f %5.3f %12.6e\n',alfa1,alfa2,alfa3,abs(fTrue-fAppx))
end
end

function [Fval,Fx,Fy] = MyF(x,y)
% Underlying function: f(x,y) = exp(-a(x-x0)^2 - b(y-y0)^2)
% depending on a and b, more nonlinear near (x0,y0). Increasingly flat
% away from this point.
a = .2; b = .6; x0 = 1; y0 = 2;
Fval = exp(-a*(x-1)^2 - b*(y-2)^2);
Fx = -2*a*(x-x0)*Fval;
Fy = -2*b*(y-y0)*Fval;