Derivative Matrix for a Function Vector

From Class Wiki
Revision as of 22:47, 2 February 2010 by John.hawkins (talk | contribs)
Jump to navigation Jump to search

Author

John Hawkins

Problem

The following problem statement is that proposed by Prof. Frohne on the main class page:

  1. Explore how a linear operator, like for example can be represented as some kind of a matrix multiply (with perhaps an infinite number of dimensions).

Solution

The solution presented here is limited to presenting a matrix representation of the operator .

First, let us consider the finite case. Let be the independent variable with components and let the components of be defined such that for all and for some constant . Also let . We wish to find the slope of the function at for any . To do this for the finite case, we will approximate the slope at a point as being the slope of the line between the two points and given that . For the case when or , we approximate as the slope the lines between and respectively. These approximations are accomplished by the following matrix:

Note that we need to divide by because the distance between and is . Similarly, the nonzero entries in the first and last rows are 2 to cancel out the 2 in the denominator.

Solution Picture

The figure below was created to demonstrate how the above matrix works. First, 10 points of the function are displayed. Then for each point the line between the two adjacent points is drawn and the slope of that line is shown to be for the original point, calculated using the matrix shown above. After this has been done in the case of 10 points, the function and derivative vectors are shown for 15, 20, 30, 50, 100, 200, and 500 points.

Derivative.gif

Code

The figure above was generated with the following code:


<source lang="matlab">

%

% _-_,, ,, _-_- ,,

% ( // || /, _  ; || '

% _|| /'\\ ||/\\ \\/\\ || __ < \, \\/\/\ ||/\ \\ \\/\\ _-_,

% _|| || || || || || || ~||- - /-|| || | | ||_< || || || ||_.

% || || || || || || || ||===|| (( || || | | || | || || || ~ ||

% -__-, \\,/ \\ |/ \\ \\ ( \_, | \/\\ \\/\\/ \\,\ \\ \\ \\ ,-_-

% _/ `

%

% John Hawkins

% LNA

% Derivative Matrix Example

% 2 Feb 2010

function DerivativeExample

x=linspace(0,4*pi,500)';

y=sin(x);

dydx=cos(x);

h.fig=figure('color','w','units','normalized','position',[.2 .2 .6 .6]);

plot(x,y,'color',[.8 .8 .8]);

hold on;

plot(x,dydx,'color',[.8 .8 .8]);

numStartPoints=10;

x=linspace(0,4*pi,numStartPoints)';

y=sin(x);

dydx=DERIV(x,y);

h.y=plot(x,y,'b.');

h.construct=plot(0,0,'r');

h.dydx=plot(0,0,'r.');

legend([h.y,h.construct,h.dydx],'y','Construction Line','dy/dx');

xlabel('x'); ylabel('y');

axis equal;

title('Taking the Derivative of a Finite Function Vector for y=sin x',...

   'fontname','times','fontsize',20);

set([h.construct,h.dydx],'visible','off');

fig = getframe(h.fig);

[im,map] = rgb2ind(fig.cdata,256,'nodither');

set(h.construct,'visible','on','xdata',x([1,2]),'ydata',y([1,2]));

im=CAPTUREFIG(im,map,h);

set(h.dydx,'visible','on','xdata',x(1),'ydata',dydx(1));

im=CAPTUREFIG(im,map,h);

for n=2:numStartPoints-1

   set(h.construct,'xdata',x([n-1,n+1]),'ydata',y([n-1,n+1]));
   im=CAPTUREFIG(im,map,h);
   set(h.dydx,'xdata',x(1:n),'ydata',dydx(1:n));
   im=CAPTUREFIG(im,map,h);

end

set(h.construct,'xdata',x([numStartPoints-1,numStartPoints]),...

   'ydata',y([numStartPoints-1,numStartPoints]));

im=CAPTUREFIG(im,map,h);

set(h.dydx,'xdata',x,'ydata',dydx);

im=CAPTUREFIG(im,map,h);

set(h.construct,'visible','off');

im=CAPTUREFIG(im,map,h);

for k=1:5

   im=CAPTUREFIG(im,map,h);

end

for n=[15 20 30 50 100 200 500]

   x=linspace(0,4*pi,n)';
   y=sin(x);
   dydx=DERIV(x,y);
   set(h.y,'xdata',x,'ydata',y);
   set(h.dydx,'xdata',x,'ydata',dydx);
   im=CAPTUREFIG(im,map,h);

end

for k=1:5

   im=CAPTUREFIG(im,map,h);

end

imwrite(im,map,'Derivative.gif','DelayTime',.5,'LoopCount',inf)

function dydx=DERIV(x,y)

num=numel(y);

deltax=x(2)-x(1);

operator=zeros(num);

operator(1,1)=-2;

operator(1,2)=2;

for k=2:num-1

   operator(k,k+1)=1;
   operator(k,k-1)=-1;

end

operator(num,num-1)=-2;

operator(num,num)=2;

operator=operator/(2*deltax);

dydx=operator*y;

function im=CAPTUREFIG(im,map,h)

fig = getframe(h.fig);

im(:,:,1,size(im,4)+1) = rgb2ind(fig.cdata,map,'nodither');

</source>

Reviewed By

Read By

Comments