Undefined variable while computing ode45 in matlab and issue with the vector lengths after computing ode.

1 view (last 30 days)
I computed my results using ode45. The main function m file is given below.
function qprime=memcapfinal(t,q);
e = 1.6*10^(-19); %C electron charge%
h = 6.63*10^(-34); %J/s planks constant%
phi = (0.3)*1.6*10^(-19); %J barrier height%
R=1;
Epsilon0 = 8.85*10^-12;
k1 = 100;
k2 = 10;
d=10*10^(-9);
s = 6*10^(-9);
m = 9.11*10^(-31); %kg electron mass%
A = 10^(-4);
b = -4*3.14*s/h;
c0=(A*Epsilon0*k1)/d;
f=10000;
v0=7.5;
T=5/f;
v=v0*sin(2*3.14*f*t);
qprime=[((v(:)/R)-(1/R)*(((d*q(1))+(s*q(2)))/(c0*d)));(sqrt(2*m*phi)/s*(e/h)^2*(((q(1)+q(2))/(2*A*Epsilon0*k2))*s)*exp(b*sqrt(2*m*phi)))*A];
AND calling m file is give as follows.
clear all;
clc;
f1=10000;
v01=7.5;
t1=0:0.0000001:0.0005;
v1=v01*sin(2*3.14*f1*t1);
q0=[10^(-20) 10^(-20)];
tspan1=[0,0.0005];
[t,q]=ode45(@memcapfinal,tspan1,q0);
subplot(2,2,2)
plot(t,q(:,1))
subplot(2,2,3)
plot(t,q(:,2))
subplot(2,2,4)
plot(t,v)
Error: ??? Undefined function or variable 'v'.
Error in ==> ode45call at 21 plot(t,v)
Comment : Although I have defined the v in memcapfinal.m file. Moreover I am getting the length(t) of t as 133 which I don't why is it so? My goal is to plot q(1) vs v and this q(1) is calculated by ode45. for that both must have the same lengths but q(1) length is 1 and v is undefined.

Accepted Answer

Walter Roberson
Walter Roberson on 8 May 2015
You are creating v in the workspace of the function memcapfinal. Variables defined in function workspaces disappear when the function returns, except under some limited conditions
  • the variable is marked as persistent in the function. In this case the variable continues to exist until the function is "clear"'d from memory, but the variable is not accessible from outside the function (at least not without difficulty)
  • the variable is marked as global in the function. In this case the variable would be available to any other function or script that used "global" with the same variable name
  • a reference to the variable is created in an anonymous function and the anonymous function is returned out of the function
  • some cases involving handle objects and static objects
In addition, the value of a variable might get saved away in an accessible location
  • by using the seldom-recommended assignin
  • by assigning the value to a UserData field of an object
  • by using setappdata() including by way of guidata()
If you try any of these, you will quickly encounter the problem that the "v" you manage to retrieve is a scalar. Because every call you use to save it out from your function will overwrite the previous version. For the purposes of your plotting what you want is to save all of the values. That requires you to be a bit more clever about what you save and where.
  7 Comments
Walter Roberson
Walter Roberson on 8 May 2015
Before the line
[t,q]=ode45(@memcapfinal,tspan1,q0);
add the two lines
global v
v = [];
Now modify your function to become
function qprime=memcapfinal(t,q);
global v; %NEW
e = 1.6*10^(-19); %C electron charge%
h = 6.63*10^(-34); %J/s planks constant%
phi = (0.3)*1.6*10^(-19); %J barrier height%
R=1;
Epsilon0 = 8.85*10^-12;
k1 = 100;
k2 = 10;
d=10*10^(-9);
s = 6*10^(-9);
m = 9.11*10^(-31); %kg electron mass%
A = 10^(-4);
b = -4 * pi * s / h; %CHANGED
c0=(A*Epsilon0*k1)/d;
f=10000;
v0=7.5;
T=5/f;
v(end+1) = v0 * sin(2 * pi * f * t); %CHANGED
qprime = [((v(end) / R) - (1/R) * (((d * q(1)) + (s * q(2))) / (c0 * d))); (sqrt(2 * m * phi) / s * (e / h)^2 * (((q(1) + q(2)) / (2 * A * Epsilon0 * k2)) * s) * exp(b * sqrt(2 * m * phi))) * A]; %CHANGED
The first CHANGED is just changing 3.14 to become pi, to improve accuracy. The second CHANGED has that adjustment as well, but also assigns the computation result to v(end+1) which is important for your purpose in order to keep track of all the v that ever get calculated.
The last change makes v(:) into v(end) so that you access only the latest value of v, the one specific to this run.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!