Unexpected error: Undefined function or variable
Afficher commentaires plus anciens
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 commentaires
kajalschopra
le 29 Juil 2015
dpb
le 29 Juil 2015
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???
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.
dpb
le 30 Juil 2015
"...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.
Walter Roberson
le 30 Juil 2015
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.
kajalschopra
le 30 Juil 2015
Modifié(e) : kajalschopra
le 30 Juil 2015
Walter Roberson
le 30 Juil 2015
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".
kajalschopra
le 30 Juil 2015
dpb
le 30 Juil 2015
"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).
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!