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

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

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..Thanks again for helping. I think I will be able to plot v vs q(1) and q(2) If I somehow manage q(1) and q(2) the same length as that of t. The length of t is 133 which I don't know why ? and The length of both q1 and q2 is 1 which again I don't know why ? If I define some function v1 exactly as v as follows.
f1=10000;
v01=7.5;
t1=0:0.0000001:0.0005;
v1=v01*sin(2*3.14*f1*t1);
Now by doing so, I know the length of v1 and I also know that v1=v as I defined it as the same. But the problem is the length of q(1) and q(2) which is 1. Now what can I do in this situation. Moreover, Do you think that the length of q(1) and q(2) should be the same as the length of v and t??? Thanks
@Walter Roberson.. I declared v as global. Now it is accessible and its length is 1 the same as of q(1) and q(2) but when I plotted v vs q(1) it gives me nothing. Following is the plotting line code.
plot(v,q(:,1))
Both v and q(1) has got the length 1 which is scaler. That is why I am not getting any plot I think. If there is some way to vectorize both q(1) and v , then I guess I can obtain a plot. As both v and q(1) q(2) plots I have obtained individually over a time spam.
The value of
length(q(:,1)
is 133 and the value of
length(t)
is also 133. Now Same should be the value of
length(v)
Which is NOT and its 1 actually. I also tried for length(v(:)) which is also 1
And Yes , I solved it. I just replaced tspan1 with
t1=0:0.0000001:0.0005;
And that gives me length(q(:,1) as 5001 which I plotted against v1 .
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.
@Walter...Thanks alot.Your help was great. I really appreciate that

Sign in to comment.

More Answers (0)

Categories

Find more on Code Execution in Help Center and File Exchange

Asked:

on 8 May 2015

Edited:

on 8 May 2015

Community Treasure Hunt

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

Start Hunting!