Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
"undefined function or variable "z""

Subject: "undefined function or variable "z""

From: Yoshi Amin

Date: 17 May, 2010 05:37:03

Message: 1 of 4

hi all, i am a new user of MATLAB and i've been trying to make the following m-file for my research.
function dc=dcsdt(t,c)
K=0.8;
c0=0.16;
vmax=0.36;
n=length(t);
for i=1:n-1
if t(i)==0;
    f0=0; % the volumetric flow rate of rice mixture and amylase in mL
    fhcl=0; % units mL
    f1=0; % in the first half hour no chyme is pumped out
    V(i)=17.5;% food in mouth initiates cephalic phase hence the 17.5 in mL
    z=f0/(f0+fhcl-f1);
    tau=V/(f0+fhcl-f1);
elseif t(i)>0 && t(i)<=30
    f0=10;
    fhcl=3.5;
    f1=0;
    V(i)=V(i-1)+(f0*(t(i)-t(i-1)))+(fhcl*(t(i)-t(i-1)))-(f1*(t(i)-t(i-1)));
    z=f0/(f0+fhcl-f1);
    tau=V/(f0+fhcl-f1);
elseif t(i)>=30
    f0=0;
    fhcl=3.5;
    f1=5;
    V(i)=V(i-1)+(f0*(t(i)-t(i-1)))+(fhcl*(t(i)-t(i-1)))-(f1*(t(i)-t(i-1)));
    z=f0/(f0+fhcl-f1);
    tau=V/(f0+fhcl-f1);
end
end
R=vmax*c/(K+c);
dc=((z*c)+((R/(f0+fhcl))*V)-c0)/tau;
end
however, when i try to run the program, it says undefined function or variable "z", can someone please point out what did i do wrong?

Subject: "undefined function or variable "z""

From: Sean

Date: 17 May, 2010 12:18:04

Message: 2 of 4

"Yoshi Amin" <yoshi.amin@gmail.com> wrote in message <hsqklv$hrp$1@fred.mathworks.com>...
> hi all, i am a new user of MATLAB and i've been trying to make the following m-file for my research.
> function dc=dcsdt(t,c)
> K=0.8;
> c0=0.16;
> vmax=0.36;
> n=length(t);
> for i=1:n-1
> if t(i)==0;
> f0=0; % the volumetric flow rate of rice mixture and amylase in mL
> fhcl=0; % units mL
> f1=0; % in the first half hour no chyme is pumped out
> V(i)=17.5;% food in mouth initiates cephalic phase hence the 17.5 in mL
> z=f0/(f0+fhcl-f1);
> tau=V/(f0+fhcl-f1);
> elseif t(i)>0 && t(i)<=30
> f0=10;
> fhcl=3.5;
> f1=0;
> V(i)=V(i-1)+(f0*(t(i)-t(i-1)))+(fhcl*(t(i)-t(i-1)))-(f1*(t(i)-t(i-1)));
> z=f0/(f0+fhcl-f1);
> tau=V/(f0+fhcl-f1);
> elseif t(i)>=30
> f0=0;
> fhcl=3.5;
> f1=5;
> V(i)=V(i-1)+(f0*(t(i)-t(i-1)))+(fhcl*(t(i)-t(i-1)))-(f1*(t(i)-t(i-1)));
> z=f0/(f0+fhcl-f1);
> tau=V/(f0+fhcl-f1);
> end
> end
> R=vmax*c/(K+c);
> dc=((z*c)+((R/(f0+fhcl))*V)-c0)/tau;
> end
> however, when i try to run the program, it says undefined function or variable "z", can someone please point out what did i do wrong?


I don't know what you're importing for t. If t is a vector then there should be no issue, but if you've been putting in t as a scalar, as I tried it fails. It fails because you define:
>>n = length(t);
if t = 30; then length(t) = 1, thus n = 1 and the for loop is never entered.

You should also preallocate V as M-Lint suggests. If not it will result in an error undefined function or variable V.

Subject: "undefined function or variable "z""

From: Yoshi Amin

Date: 18 May, 2010 02:49:05

Message: 3 of 4

i will be putting a vector for both t and c. the problem was that matlab can not process the dc=.. line. it says z as being undefined then.

Subject: "undefined function or variable "z""

From: TideMan

Date: 18 May, 2010 03:04:25

Message: 4 of 4

On May 18, 2:49 pm, "Yoshi Amin" <yoshi.a...@gmail.com> wrote:
> i will be putting a vector for both t and c. the problem was that matlab can not process the dc=.. line. it says z as being undefined then.

I think it doesn't process it because length(t) is 1, in which case
the loop doesn't get executed because the index goes from 1 to 0.
That mean the loop gets skipped altogether.

Also, I guess you realise that you calculate z and tau for each
iteration within the loop, but only the very last values
(corresponding to i=n-1) will be available to the dc= line. This
seems pointless to me, but I don't know what you're trying to do.

As the other responder suggested, you should preallocate V before the
loop. Do it like this:
V=zeros(n,1);

It is bad practice to use i or j as indexes for loops because by
default they are sqrt(-1). This may seem irrelevant to you right now,
but just wait until you try using i or j as sqrt(-1) and you'll be
sorry. Use something like ix or it instead.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us