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*sin(x);
Find :
y = g(3)
y = 0.423360024179602
Matlab shows by default only 5 of 16 digits. We can see 15 digits by first using format long g
format long g
y
y = 0.423360024179602
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 :
fplot(g,[0,20])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
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 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)
I = 2.15335851709229

Finding a solution of a nonlinear equation or

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)
xs = 3.14159265358979
Find x such that , using initial guess 7:
Here we need to find a zero of the function :
xs = fzero( @(x)g(x)-5 , 7 )
xs = 7.06889140339507

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;
Evaluate
z = G(1,2)
z = 1
Plot the graph of for , as a surface over the -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 for ,
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 , the yellow points satisfy
If you only want to see the points with 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')