%% Introduction to @-functions in "standard" Matlab
% You should always start with this:

clearvars;           % clear all variables in the work space

% Standard Matlab (as opposed to "symbolic Matlab") uses machine numbers with about 16 digit accuracy. Therefore it gives approximate results (not exact results).
%% Using a function g(x)
% Example: Define the function g(x)=x\cdot\sin x

g = @(x) x*sin(x);

% Find y=g(3):

y = g(3)

% Matlab shows by default only 5 of 16 digits. We can see 15 digits by first using format long g

format long g
y

% If you perform several operations, not all of the displayed 15 digits will be correct (roundoff errors accumulate).
%% Plotting a function using fplot
% Plot the function g for 0\le x\le 20:

fplot(g,[0,20])

% You can ignore this warning. Everything is working fine. You can get rid of this type of warning by using warning('off','MATLAB:fplot:NotVectorized')

xlabel('x')                            % label x-axis
title('function g(x) = x\cdot sin x')  % title on top of graph
grid on                                % show grid

%% Definite integral
% For \int_a^b g(x)dx use integral(g,a,b,'Arr',true)  (with 'Arr',true this works even if we define g with * / ^ instead of .* ./ .^ )

I = integral(g,0,8,'Arr',true)

%% Finding a solution of a nonlinear equation g(x)=0 or g(x)=a
% We use xs=fzero(g,x0) where x0 is an initial guess.
% In our example there are infinitely many solutions.
% Find a solution x near 3:

xs = fzero(g,3)

% Find x such that g(x)=5, using initial guess 7:
% Here we need to find a zero of the function g(x)-5:

xs = fzero( @(x)g(x)-5 , 7 )

%
%% Using a function G(x,y)
% We can also use functions of more than one variable.
% Example: Define the function G(x,y)=x^4+y^4-4(x^2+y^2)+4

G = @(x,y) x^4+y^4-4*(x^2+y^2)+4;

% Evaluate z=G(1,2)

z = G(1,2)

% Plot the graph of G(x,y) for -2\le x\le2, -2\le y\le 2 as a surface over the xy-plane:

warning('off','MATLAB:fplot:NotVectorized')     % get rid of the NotVectorized warning
fsurf(G,[-2 2 -2 2])
xlabel('x'); ylabel('y'); title('function x^4+y^4-4(x^2+y^2)+4')

% Make a contour plot of G(x,y) for -2\le x\le2, -2\le y\le 2

fcontour(G,[-2 2 -2 2])
colorbar     % show colorbar on the right which explains colors of contours
axis equal   % use the same unit length for x and y
xlabel('x'); ylabel('y'); title('contour plot of x^4+y^4-4(x^2+y^2)+4')

% Here Matlab chooses the contour levels automatically. If you want more or fewer contours you can specify the spacing of the contours by using fcontour(G,[x1,x2,y1,y2],'LevelStep',dz):

fcontour(G,[-2 2 -2 2],'LevelStep',0.5)
colorbar; axis equal

% If you only want contours for a few specific z-values you can use

fcontour(G,[-2.1 2.1 -2.1 2.1],'LevelList',[0 1])
axis equal; 
title('points with G(x,y)=0 (blue) and G(x,y)=1 (yellow)')

% The blue points satisfy G(x,y)=0, the yellow points satisfy G(x,y)=1
% If you only want to see the points with G(x,y)=0 you can use fcontour(G,[x1,x2,y1,y2],'LevelList',[0]) or fimplicit(G,[x1,x2,y1,y2]):

fimplicit(G,[-2 2 -2 2]);
axis equal; xlabel('x'); ylabel('y'); title('points satisfying x^4+y^4-4(x^2+y^2)+4=0')

%
%