Derivative Matrix for a Function Vector: Difference between revisions

From Class Wiki
Jump to navigation Jump to search
(Main Content)
 
(Moving code to its own page)
 
(2 intermediate revisions by the same user not shown)
Line 41: Line 41:
</math></center>
</math></center>


Note that we need to divide by <math>1/2(\Delta x)</math> because the distance between <math>x_{m-1}</math> and <math>x_{m+1}</math> is <math>2\Delta x</math>. Similarly, the nonzero entries in the first and last rows are 2 to cancel out the 2 in the denominator.
Note that we need to divide by <math>1/2(\Delta x)</math> because the distance between <math>x_{m-1}</math> and <math>x_{m+1}</math> is <math>2\Delta x</math>. Similarly, the nonzero entries in the first and last rows are 2 to cancel out the 2 in the denominator. Using this matrix, we find that


<center><math>
==Solution Picture==


\left(\frac{d}{dx}\right)y=\dfrac{1}{2(\Delta x)}\begin{bmatrix}
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 <math>dy/dx</math> 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.


-2&2&0&0&&&&&\\
[[Image:Derivative.gif]]


-1&0&1&0&&&\cdots&&\\
==Code==


0&-1&0&1&&&&&\\
The figure above was generated with the following code:


\\
<br />


&&&&\ddots&&&&\\
<source lang="matlab">


\\
%


&\vdots&&&&-1&0&1&0\\
% _-_,, ,, _-_- ,,


&&&&&0&-1&0&1\\
% ( // || /, _ ; || '


&&&&&0&0&-2&2
% _|| /'\\ ||/\\ \\/\\ || __ < \, \\/\/\ ||/\ \\ \\/\\ _-_,


\end{bmatrix}
% _|| || || || || || || ~||- - /-|| || | | ||_< || || || ||_.
\begin{bmatrix}y(x_1)\\y(x_2)\\y(x_3)\\ \\ \vdots \\ \\ y(x_{n-2})\\y(x_{n-1})\\y(x_n)\end{bmatrix}
=


\begin{bmatrix}
% || || || || || || || ||===|| (( || || | | || | || || || ~ ||


\left[y(x_2)-y(x_1)\right]/\Delta x\\
% -__-, \\,/ \\ |/ \\ \\ ( \_, | \/\\ \\/\\/ \\,\ \\ \\ \\ ,-_-


\left[y(x_3)-y(x_1)\right]/2(\Delta x)\\
% _/ `


\left[y(x_4)-y(x_2)\right]/2(\Delta x)\\
%


\\
% John Hawkins


\vdots \\
% LNA


\\
% Derivative Matrix Example


\left[y(x_{n-1})-y(x_{n-3})\right]/2(\Delta x)\\
% 2 Feb 2010


\left[y(x_n)-y(x_{n-2})\right]/2(\Delta x)\\
function DerivativeExample


\left[y(x_n)-y(x_{n-1})\right]/\Delta x
x=linspace(0,4*pi,500)';


\end{bmatrix}
y=sin(x);


</math></center>
dydx=cos(x);


which corresponds with the slopes we wished to obtain.
h.fig=figure('color','w','units','normalized','position',[.2 .2 .6 .6]);


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


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 <math>dy/dx</math> 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. One can easily imagine the limiting case. The code used to develop this image can be found [[Derivative Operator Picture Code|here]].
hold on;


[[Image:Derivative.gif]]
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==
==Reviewed By==

Latest revision as of 22:18, 4 February 2010

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. Using this matrix, we find that

which corresponds with the slopes we wished to obtain.

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. One can easily imagine the limiting case. The code used to develop this image can be found here.

Derivative.gif


Reviewed By

Read By

Comments