New version of this program

# Project work Fys-292

By Irina Otdelnova

Visualization of hydrogen-like orbitals for atomic physics using MATLAB

## Problem definition

The task of the program is to visualize wave function for hydrogen-like atoms. The program creates two windows - one with interactive controls for the program and another one - for the figure of the wavefunction itself. In the control window it's possible to change: range of plotting, the quantum numbers n and l, and also switch between plotting R(r) and r*R(r). The 'exit' button is also present.

The programme is using the Symbolic Toolbox and the wavefunctions are present as analytical expressions. These may be extracted by a user, if necessary, as can be found in the listing.

### Algorithm

1. Evaluate the wavefunction using the multiple symbolic derivation for evaluation of Laguerre polynomials

```Radhyd(n,l,r)  %%  Symbolic evaluation of the wavefunction

radhyd0(n,l,r)  %%  to find the formula

lagguere(n+l,2*l+1,'x');   %% Laguerre polynomial
multiple symbolic derivation
df('f','x',n)
returns a character string

returns unnormalized wavefunction

normh(n,l)    %%  to find the   the norm
%% evaluates symbolically the normalization integer
integral = int(r^2 * Radhyd0^2 , 0, infinity)
returns the norm= 1/sqrt( integral )

returns  norm * radhyd0(n,l,r)  (normalized wavefunction)

```

2.The choice of the quantum numbers is done by the interface program visn.m

```visn %%
create control window
create drawing window

manage controls(callbacks manage drawing window)

continue until exit pressed
```

3. managing drawing window:

`Description of drawing window management:`
`       updaten action taken when n changes `
`       updatel action taken when n changes `
`       updater action taken when the range of r changes `
`       The shift of r*R and R plotting is managed in visn.m, `
`           in the callback strings for the button Rmul `
`end of description `
```
```

### Program listing: Part 1: User Interface

`MAIN PROGRAM OF THE USER INTERFACE`
`VISN`
```% VISN          The program for visualization of radial function.
%               Then the function is created symbolically.
%               Finally its graph is shown in a window. The user
%               has a possibility for controlling function parameters
%               interactively.

% Some notes: I use first two MATLAB windows (Figure #1
% and Figure #2). If there are any open windows when the
% program is using, they will remain untouched.

% n and l given unphysical values, range initialized.

n=-1;l=-1;range=30;
nold=n;lold=l;

% An array of numbers starting from 0 to range with step of range/100
% These are the points at which the function is evaluated.
% This array is used later on for graphs.

x=0:range/100:range;

% Open graph window #1

F1=figure;

% Open control window #2 with sliders

F2=figure;

% Resizing the control window

pos=get(F2,'position');
pos(3)=300;pos(4)=250;  % dimensions of the command window;
set(F2,'position',pos);

% Main Control Functions: Title, exit, type of displaying.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

TitleF= uicontrol('style','text', ...
'string','Controls for Wavefunction Tool', ...
'position', [10 225 275 20]);
ExitBut=uicontrol('style','pushbutton',...
'string','EXIT','position', [10 200 50 20],...
'callback','close(F1);close(F2);');
Rmulv=-1;
Rst=['Change to r*R(r)';' Change to R(r) '];
Rmul=uicontrol('style','pushbutton',...
'string','Change to r*R(r)','position', [10 175 150 20],...
'callback',...
'Rmulv=-Rmulv;nold=-1;updaten;set(Rmul,''string'',Rst(fix(1.6+Rmulv/2),:));');

% Control of Paramter 'n'
%%%%%%%%%%%%%%%%%%%%%%%%%%

% Text field with value of 'n'

n_text = uicontrol('style','text', ...
'string','n=', 'position',[10 20  80 20]);

% Callback for n_slide (updates value of 'n' in the text-field
% and redraws the graph; it is contained in script file updaten.m

n_slide = uicontrol('style','slider', ...
'min',0,'max',10.0, ...
'position',[100 20  160 20], ...
'value',n, ...
'callback',['n=get(n_slide,''value'');updaten;']);

% Control of Paramter 'l'
%%%%%%%%%%%%%%%%%%%%%%%%%%

% Text field with value of 'l'

l_text = uicontrol('style','text', ...
'string','l=', 'position',[10 60  80 20]);

% Callback for n_slide (updates value of 'n' in the text-field
% and redraws the graph

% The actual slider control

l_slide = uicontrol('style','slider', ...
'min',0,'max',10.0, ...
'position',[100 60  160 20], ...
'value',l, ...
'callback',['l=get(l_slide,''value'');updatel;']);

% Parameter 'range'
%%%%%%%%%%%%%%%%%%%%

% Text field with value of 'range'

r_text = uicontrol('style','text', ...
'string','range=', 'position',[10 100 80 20]);

% Callback for r_slide (updates value of 'range' in the text-field
% and redraws the graph

%updater='eval(update_graph);set(r_text,''string'',[''range='' num2str(range)]);';

% The actual slider control

r_slide = uicontrol('style','slider', ...
'min',0,'max',200, ...
'position',[100 100  160 20], ...
'value',range, ...
'callback',['range=get(r_slide,''value'');updater;']);

% Parameter 'Evaluating...'
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following code does not work with some Matlab versions. If you have such
% problem try to use a different version of it.
figure(F1);
mess1=uicontrol('style','edit', ...
'string','Evaluating the symbolics ..........', ...
'BackgroundColor','red',...
'ForegroundColor','white',...
'position',[10 200  300 20], 'visible','off'); drawnow;

% Leaves the control window on top
figure(F2);
```
```SUBROUTINES OF THE USER INTERFACE
```
`UPDATER`
```%  updater.m
% n is the integer such n>=1
if (n<0), n=1; end
% l is the integer such that 0<=l<n
l=fix(l); if (l<0), l=0; end; if (l>n-1), l=n-1; end;
figure(F2);
set(r_text,'string',[ 'range=' num2str(range)]); drawnow;
% Activate Drawing fingure
figure(F1);
% An array of numbers starting from 0 to range with step of range/100
% This array is used later on for graphs.
x=0:range/100:range;
if exist('com')==1
eval(com);
if Rmulv==-1
plot(x,y,[0 range], [0 0]);
else
plot(x,x.*y,[0 range], [0 0]);
end
end```
`UPDATEL`
```%  updatel.m
% n is the integer such that n>=1
if (n<0), n=1; end
% l is the integer such that 0<=l<n
l=fix(l); if (l<0), l=0; end; if (l>n-1), l=n-1; end;
% Activate Control figure
figure(F2);
% Setting the sliders labels and positions to n and l
set(n_text,'string',[ 'n=' num2str(n)]); drawnow;
set(l_text,'string',[ 'l=' num2str(l)]); drawnow;
set(n_slide,'value',n);drawnow;
set(l_slide,'value',l); drawnow;
figure(F1);
% Recalculate data only if it is necessary
if l ~= lold
figure(F1); set(mess1,'visible','on');drawnow;
figure(F1); set(mess1,'visible','off');drawnow;
% Make the full function
comnd=[ 'y=' expr ';'];
% Change the ordinary matrix operations into array operations
comnd=strrep(comnd,'^','.^');
comnd=strrep(comnd,'*','.*');
comnd=strrep(comnd,'/','./');
end

eval(comnd);

if Rmulv==-1
figure(F1); plot(x,y,[0 range], [0 0]);
else
figure(F1); plot(x,x.*y,[0 range], [0 0]);
end ```
`UPDATEN`
```%  updaten.m

% n is the integer such that n>=1
n=fix(n); if (n<1), n=1; end;

% l is the integer such that 0<=l<n
l=fix(l); if (l<0), l=0; end; if (l>n-1), l=n-1; end;

% Activate Control figure
figure(F2);
% Setting the sliders labels and positions to n and l
set(n_text,'string',[ 'n=' num2str(n)]); drawnow;
set(l_text,'string',[ 'l=' num2str(l)]); drawnow;
set(n_slide,'value',n);drawnow;
set(l_slide,'value',l); drawnow;

% Recalculate data only if it is necessary
if (n ~= nold)
figure(F1); set(mess1,'visible','on');drawnow;
figure(F1); set(mess1,'visible','off');drawnow;

% Make full function
comnd=[ 'y=' expr ';'];

% Change the ordinary matrix operations into array operations
comnd=strrep(comnd,'^','.^');
comnd=strrep(comnd,'*','.*');
comnd=strrep(comnd,'/','./');
end
eval(comnd);
% Plot in any case
if Rmulv==-1
figure(F1); plot(x,y,[0 range], [0 0]);
else
figure(F1); plot(x,x.*y,[0 range], [0 0]);
end
```

### Program listing: Part 2: Analytic Evaluation of the Wavefunctions

`SUBROUTINES `
`DF`
```function deriv = df(instr, var, n)
%% return n-th derivative
if(nargin==2) , n=1; end
locstr=instr;
for k=1:n
locstr=diff(locstr,var);
end
deriv=locstr;```
`LAGGUERE`
```function secder = lagguere(k,j,r)
%% return the old Lagguerre polynomial
%% k,j must be integers, r must be a string
%% Built to emulate the REDUCE procedure:
%%% procedure lagguere(k,j,r);
%%%   begin scalar secder, deriv;
%%%   deriv:=e^r * df(r^k * e^(-r),r,k);
%%%   secder:=df(deriv,r,j);
%%%   return secder
%%% end;
%
%  deriv=exp(r) * df(r^k * exp(-r),r,k);
str1=[ r '^' num2str(k) ' * exp(-' r ')'];
deriv=[ 'exp(' r ') * (' df(str1,r,k) ')'] ;
deriv=simplify(deriv);
%  the next is as in Reduce:
secder=df(deriv,r,j);```
`RADHYD`
```function str2 = radhyd(n,l,r)
%% return the normalized radial function as a string
%% evaluating the unnormalized radial function radhyd0
%% and the normalization integral normh(n,l)
%% needs the following m-functions
%%   df.m        emulates the multiple derivative
%%   lagguere.m  evaluates the 'old style' Laggurre
%%   normh.m     evaluates the normalization constant
str2=normh(n,l);
str2=symmul(str1,str2);
str2=symmul(str2,'-1');
```
`RADHYD0`
```function str2 = radhyd0(n,l,r)
%% return the unnormalized radial function as a string
% procedure rad(n,l,r); sub(x=2*r/n,lagguere(n+l,2*l+1,x));
str1=[ '2*' r '/' num2str(n) ];  % The string for subs;
str1=simplify(str1);
%
%
%
str2=[ r '^' num2str(l) '*exp(-' r '/' num2str(n) ')' ];
str2=simplify(str2);
str2=[ '(' rad ')*' str2];
str2=simplify(str2);

NORMH```
```function str2 = normh(n,l)
%% return the inverse of the square root of the
%% normalization integral int(R(z)^2 z^2 d z)
%% radhyd0(n,l,'z') is the unnormalized radial function
%%
str1=symmul(str1,str1);
str1=symmul(str1,'zz^2');
str1=simplify(str1);
str1=simplify(expand(str1));
str1=int(str1,'zz');
str2=subs(str1,0,'zz');
str2=['-(' str2 ')'];
str2=simplify(str2);
str2=sympow(str2,-1/2);
str2=simplify(str2);
```

### Documentation

The program VISN visualize the wave function which is build in the program. User is allowed to change interactively some parameters such as the value sof corresponding quantum numbers and the range. Also it is possible to switch between R(r) and r*R(r).

### Subprograms

VISN
Main program of the user interface
UPDATER
Change the 'range' label text and replot the graph according to the new range.
UPDATEN
UPDATEL
are used to update the sliders to show n and l values. And redraw the graph according to the new values.
Calculate the normalized radial function as a string. (main function)
The function radhyd0 is used internaly by radhyd.
NORMH
Calculate the inverse of the square of the normalization integral.
LAGGUERE
Calculate odd Lagguere polynomial.
DF
Calculate n-th derivative

### Variables

n_slide, l_slide, r_slide
refer to the slider objects that decide the current values n, l, r.
Rmul
is the button object, which decides between plotting R(r) or r*R(r)
(in its call back routine is done the switching of the modes)
ExitBut
is the button object, which ends the program
n and l
Hold the values of corresponding quantum numbers of wave function in hydrogen like atom.
range
Holds the current range.
expr
contains the character string which represents the current wavefunction
If the user is interested in the symbolic form of the wavefunction, it can be obtained by typing
expr in the Matlab command window anywhere during the execution of the program.

### Instructions for the user

The programme is written in Matlab and can be used only from the Matlab command. The user must have available the routines as listed above:

`visn.m updatel.m updaten.m updater.m  `
`radhyd.m df.m normh.m radhyd0.m lagguere.m  `

Running Matlab on any computer which has available the Symbolic Toolbox, the user starts by typing

visn

Two windows appear, one is the control window, the other will later show the wavefunction.

The mode of plotting is controled by the selfinstructing names of the control elements.

The control window looks like this, before any action is applied

During the execution of the program:

If the user is interested in the symbolic form of the wavefunction, it can be obtained by typing

expr

in the Matlab command window anywhere during the execution of the program.