Code covered by the BSD License  

Highlights from
Draw grpah of a function on matrix

image thumbnail
from Draw grpah of a function on matrix by David Silver
FCN2MAT Convert user-defined single-valued function to matrix

fcn2mat(fcnstr,minX,maxX,perX,lineWidth,perY,minY,maxY)
function [ grph,approx ] = fcn2mat(fcnstr,minX,maxX,perX,lineWidth,perY,minY,maxY)

%FCN2MAT Convert user-defined single-valued function to matrix
%  FCN2MAT is used to 'draw' the graph of a single-valued function on a matrix
% 
% GRPH = FCN2MAT(FCNSTR, MINX, MAXX, PERX, LINEWIDTH, PERY, MINY, MAXY) returns a matrix with the graph of fcnstr(x). 
% 
% Input arguments - 
%     FCNSTR - A string of a function of 'x'. EXAMPLE: fcnstr = 'sin(x)-log(x^2)'. DEFAULT: fcnstr = 'sin(x)'.
%     MINX - Integer. The lower limit of the x axis. DEFAULT: minX = -3.
%     MAXX - Integer. The upper limit of the x axis. DEFAULT: maxX = 3.
%     PERX - Double. The step size for the discretization of the x axis. DEFAULT: perX = 0.05.
%     LINEWIDTH - Integer. The width of the line to draw, in pixels. DEFAULT: lineWidth = 1.
%     PERY - Double. The step size for the discretization of the y axis. DEFAULT: perY = perX.
%     
%     -----For auto y-axis scaling leave minY and maxY empty!-----
%     MINY - Integer. The lower limit of the y axis. DEFAULT: minY = The minimum of the function on [minX maxX].
%     MAXY - Integer. The upper limit of the y axis. DEFAULT: maxY = The maximum of the function on [minX maxX].
%   -----Don't use auto-scaling if the function has an asymptote in the  range! -----
%
%  Output - 
%     GRPH - Logical matrix with the graph of the function.
%     APPROX - Gray image of the function's graph with the approximate position of the graph
%                            (better when the  function is not everywhere defined).
%     
%     
%     
%   EXAPMLES:
% 
%    [ grph,approx ] = fcn2mat('sin(x)*cos(2*x)',-6,6,0.03,3,0.03,-2,2);
%   imshow(approx)
%
%    [ grph,approx ] = fcn2mat('sin(x)/cos(log(abs(x)))',-20,20,0.03,3,0.03,-2,2);
%    imshow(approx);
%     
%    Or simply: imshow(fcn2mat)
%
%   BUG REPORT:
%         Please send your bug reports, comments and suggestions to
%         dsilver@tx.technion.ac.il .
%         Thanks.
% 
%  Author: David Silver
%           Department of Biology
%           Technion - Israel Institute of Technology, Haifa, Israel
%           dsilver@tx.technion.ac.il
%  
%    Version: 1.00. Date: 2011/07/04  16:23
% 
%  ==============================================================================
% 

if nargin < 1, fcnstr = 'sin(x)'; end

eval(['f_x = @(x) ',vectorize(fcnstr),';'])


if nargin < 3, minX=-3; maxX = 3; end
if nargin < 4, perX = 0.05; end
if nargin < 5, lineWidth = 1; end
if nargin < 6, perY = perX; end
if nargin < 8
    eval(['f_xM = @(x) -(',vectorize(fcnstr),');'])
    [x,minVal] = fminbnd(f_x,minX, maxX);
    [x,maxVal] = fminbnd(f_xM,minX, maxX);
    maxVal=-maxVal;
    minY=minVal-lineWidth*perY;
    maxY=maxVal+lineWidth*perY;
end


[X,Y]=meshgrid(minX:perX:maxX,minY:perY:maxY);

f_X=f_x(X);

Dist=abs(f_X-Y);

[SortDist,I]=sort(Dist);
I2=1:size(I,2);
grph=zeros(size(X));

for j=1:lineWidth
    Indx(j,:)=sub2ind(size(grph),I(j,:),I2);
end

grph(Indx(:))=1;
approx=1-mat2gray(Dist,[0 1]);

end

Contact us