GRAPHICS FUNCTION CODE ERROR

4 visualizzazioni (ultimi 30 giorni)
Varshitha Yashvanth
Varshitha Yashvanth il 28 Gen 2016
Commentato: Geoff Hayes il 29 Gen 2016
title('Wave Function for a Particle in a 2D Box') % Title
drawnow
f = getframe(1);
im = frame2im(f);
[Q,map] = rgb2ind(im,256);
outfile = '2DParticle44.gif'; % FIle name to save the generated .gif file
Generates an error saying
Error using graphicsversion
Input was not a valid graphics object
Error in getframe (line 50)
usingMATLABClasses = ~graphicsversion(parentFig, 'handlegraphics');
Error in PROJ2 (line 40)
f = getframe(1);
I'm using MATLAB_R2013A.Please help debugging the code.

Risposte (1)

Geoff Hayes
Geoff Hayes il 28 Gen 2016
Varshitha - in your call to getframe, you are passing 1. What does this refer to? According to the documentation for R2014a, F = getframe(h) gets a frame from the figure or axes identified by handle h. The error message is telling you that the 1 you are passing is not the handle of any figure.
If you are unsure what the handle is for your current figure, then just use gcf instead as
f = getframe(gcf);
Else determine the function handle and use that instead. For example,
hFunc = figure;
% your other code to plot something on the figure
f = getframe(hFunc);
Try the above and see what happens!
  2 Commenti
Varshitha Yashvanth
Varshitha Yashvanth il 29 Gen 2016
Modificato: Walter Roberson il 29 Gen 2016
Dear Geoff,
thank you . But It still gives me the same error n a blank plot.
Below is the complete code,
please Have a look
%Solution of Schrödinger equation for a particle in a 2D box
%Reference; Solid state Electronic Devices, Streetman, Classnotes
% https://www.physicsforums.com/insights/visualizing-2-d-particle-box/
clear all
close all
clc % Define constants and variables
h=6.63e-34; %Planck's constant in Joules
m=9.11e-31; % Free Electron mass
hbar=h/(2*pi);
for n=1:1:3
nx=n; % number of energy states along x direction
ny=n; % number of energy states along x direction
Lx =1e-6; % Box length along the x-direction
Ly =1e-6; % Box length along the y-direction
kx =nx*pi/Lx;
ky =ny*pi/Ly;
E=n^2*pi^2*hbar^2/(2*m)*(1/Lx^2+1/Ly^2) % Energy of a particle in a 2D box
w =hbar*(kx^2+ky^2)/2*m % frequency from E=hf
w=5*w./max(w) % Frequency scaling for visualization
A = sqrt(4/(Lx*Ly)) % Wave function amplitude
PD_peak=A^2; %peak value of the probability density function
Particle_velocity=sqrt(2*E/m) %particle velocity
t = linspace(0,5,100); %Time vector
x=0:Lx/100:Lx;
y=0:Ly/100:Ly;
for ii=1:1:length(x)
for jj=1:1:length(y)
psi(ii,jj)=A*sin(kx*x(ii)).*sin(ky.*y(jj)); % Time independent wave function
end
end
for kk=1:1:length(t)
phi(kk)=exp(-j.*w.*t(kk)); % Time dependent wave function
xx=psi.*phi(kk);
surf (x,y,real(xx)); % 2DPlotting
xlim([0 Lx]), ylim([0 Ly]), zlim([-A A]); %Axes range setting
title('Wave Function for a Particle in a 2D Box') % Title
drawnow
frame = getframe;
im = frame2im(frame);
[Q,map] = rgb2ind(im,256);
outfile = '2DParticle44.gif'; % FIle name to save the generated .gif file
if kk==1;
imwrite(Q,map,outfile,'gif','LoopCount',Inf,'DelayTime',10);
else
imwrite(Q,map,outfile,'gif','WriteMode','overwrite','DelayTime',10);
end
end
Geoff Hayes
Geoff Hayes il 29 Gen 2016
Varshitha - when I run your code, I observe the following error
Error using writegif (line 138)
3-D data not supported for GIF files. Data must be 2-D or 4-D.
Error in imwrite (line 472)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in XXXXX (line 43)
imwrite(Q,map,outfile,'gif','LoopCount',Inf,'DelayTime',10);
Is that the error that you are seeing (which is different than the one that you originally posted)? I am running R2014a so that might explain a difference between your system and mine. (Though I get this same error when using an example from imwrite within the R2014a documentation…?)
As for the figure being "blank", have you stepped through the code to verify that the data that you are passing into surf makes sense?

Accedi per commentare.

Categorie

Scopri di più su Animation in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by