Unexpected error: Undefined function or variable

I have a matlab code
function ElementStiffness
global element_no;
global n_dof;
global p;
for s=1:n_dof
for t=1:n_dof
k(s,t)=0;
end
end
no_gp=NumberOfGaussPoints;
for i=1:no_gp
all_gauss_pts_wts=GaussPtsWts(no_gp);
gauss_pt=all_gauss_pts_wts(i,1);
gauss_wt=all_gauss_pts_wts(i,2);
for s=1:n_dof
for t=1:n_dof
dN=LagrangianShapeFnsDerivatives(gauss_pt);
k(s,t)=k(s,t)+(dN(s,1)*dN(t,1)*A(element_no)*E(element_no)*(1/J));
end
end
end
end
function [dN]=LagrangianShapeFnsDerivatives(gp)
global p;
term1=0;
for i=1:p+1
zi(i)=-1+((2/p)*(i-1));
prodDen=1;
sum=0;
for j=1:p+1
zi(j)=-1+((2/p)*(j-1));
if(j~=1)
sum=sum+zi(j);
prodDen=prodDen*(zi(i)-zi(j));
end
end
I get an errors
>> LagrangianShapeFnsDerivatives(gp)
Undefined function or variable 'gp'.*

9 Comments

Please can anyone help, thanks a ton
It looks like gp is never used in the function to which it's an argument; other than that I see no problem with it, per se.
It does appear there are at least a couple closing end statements in the last function.
Also, the global p appears unused???
Jon
Jon on 29 Jul 2015
Edited: Jon on 30 Jul 2015
I don't know how to help any more than the error helps. You haven't defined the variable gp, yet you try to pass it into the LagrangianShapeFnsDerivaties function in line 23.
Edit: dpb below is correct. gp is the dummy variable of the internal function.
"...yet you try to pass it into the LagrangianShapeFnsDerivaties function in line 23."
Unless there's something here my old eyes can't/don't see, that's a definition of an internal function (or another m-file, OP doesn't say which) where gp is the dummy argument.
Whatever it is that caused the problem, it's in a context other than that which is shown here.
The function is being called from the command line, passing in a variable "gp" that is not defined in the base workspace that is active at the command line.
If the function LagrangianShapeFnsDerivatives is stored in the same file as ElementStiffness.m then it is not possible to call LagrangianShapeFnsDerivatives from the command line unless you are stopped in the ElementStiffness function. But the test for whether the function being called is defined is not done until the argument being passed (gp) is tested.
Hi All,
Thanks a lot.
How can you say that I haven't defined 'gp'?
gp has been defined in the calling function.
See the code below; LagrangianShapeFnsDerivatives is being called by the function ElementStiffness and the calling function passes the argument whilst calling LagrangianShapeFnsDerivatives as below.
*gauss_pt=all_gauss_pts_wts(i,1);*
gauss_wt=all_gauss_pts_wts(i,2);
for s=1:n_dof
for t=1:n_dof
dN=LagrangianShapeFnsDerivatives(gauss_pt);
You wrote
>> LagrangianShapeFnsDerivatives(gp)
that is clearly calling the function from the command line, not from the code. Inside the code you have defined gauss_wt and you pass it to LagrangianShapeFnsDerivatives properly; inside the LagrangianShapeFnsDerivatives routine that parameter is known as gp. But "gp" as a name only exists inside the code for LagrangianShapeFnsDerivatives.
You are probably familiar with the concept of "local names for something" already. At home you might be called "Older Sister" and inside the home all references to "Older Sister" might always mean you. In your circle of friends you might be the only Kajal, so every time they talk about "Kajal" without qualification they might be referring to you. At university, there could be another Kajal Chopra so you might need to use Kajal S. Chopra there to be sure that they are talking about the right person. For tax purposes you might be assigned a 12 digit number and the tax people might even come to refer to you as "Dear Taxpayer #873838631140". In all cases the same person is being referred to, but within a particular restricted context a local name might be given for easier reference. "The party of the first part"; "the employee"; "the customer", and so on.
"gp" inside the LagrangianShapeFnsDerivatives routine is such a local name. Outside of that routine, "gp" has no meaning, just like when you go to a coffee store they are not going to call out "Latte for The Party Of The Second Part".
Thanks a lot again.
But, I'm NOT calling LagrangianShapeFnsDerivatives(gp)from the command line..
>> LagrangianShapeFnsDerivatives(gp) Undefined function or variable 'gp'.
Above is the error I get (displayed in the command line) when I run my code by hitting the run button.
The function LagrangianShapeFnsDerivatives(gp) is NOT called from the command line but from another function.
Secondly, do you mean gp should be renamed as gauss_pt throughout in LagrangianShapeFnsDerivatives ?
Thanks again, Kajal
"I'm NOT calling LagrangianShapeFnsDerivatives(gp)from the command line... [but] ...when I run my code by hitting the run button."
AHA! But that's simply another way to run the code from the command line if you've got that function in the editor when you hit "RUN".
So, you need gp defined in the workspace first (or use some other variable as the dummy argument that is defined altho it makes sense to define the array, not change code).

Sign in to comment.

 Accepted Answer

You cannot run your LagrangianShapeFnsDerivatives by using the Run menu item or the F5 key while you are in the routine at the editor. If you want to run it by itself, you need to go to the command line and type
LagrangianShapeFnsDerivatives(173.9)
or something similar -- that is, you need to pass a value in to the routine to be worked on there under the name "gp"
You can go to the ElementStiffness routine in the editor and use the Run menu item or F5 key there to start that routine, because that routine does not use any input arguments. Or you could go to the command line and type
ElementStiffness
there, and that would run that routine. ElementStiffness will ideally call LagrangianShapeFnsDerivatives with an appropriate argument.
Caution:
  1. never use "sum" as the name of a variable: that clashes with the use of "sum" as a very commonly use MATLAB routine. A routine that you could, incidentally, be using to make your code shorter.
  2. In the code shown you never give a value to the global variable "p" or "n_dof" or "element_no". If you try to run your routines without having assigned values to those global variables, you will definitely run into problems.
  3. In the code shown you give no definition for NumberOfGaussPoints, GaussPtsWts, A, E, J. If those are not the names of .m files or of "function" in a section of code you have not shown us, ElementStiffness will fail.
  4. You define your routine LagrangianShapeFnsDerivatives as returning a value in the variable dN, but you never assign a value to dN. You do assign a value to "sum" and to "prodDen" but you never use those after the loop.
  5. hint:
j = 1 : p;
zj = sum(-1+((2/p)*(j-1)); %not the variable "sum", the MATLAB routine "sum"
prodDen = prod(diff(zj)); %loop Ma, no loop!

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!