Why am I getting undefined variable error?

Whenever I try to run my function, I keep getting "Undefined function or variable 'k'." error.
Undefined function or variable 'k'.
Error in @(x)-(k.^2)*cos(2.*pi.*k.*x./L)
However, k is one of the inputs to my function
[eN,uN] = femet(A1,k,L,u0,uL,N,dom,f,plott)
In the above line, "f" is a funcion handle, that uses k to define it, and when f comes into play in the code is when I get the error. Interestingly enough, whenever I run the code as a string (ie just assign all the "inputs" and run the code without the whole [out]=function[in] stuff) it runs fine. however, I need it to be a callable function :/
Thanks, Kevin

 采纳的回答

Before you call femet(), put this line of code
whos k
class(k)
and tell us what you see on the command line. Actually if you hover your cursor over all the variables in the line where you're calling femet(), you should see their values in a popup tooltip string. What value does it show for k? If k is not defined, there will be no tooltip string for it, and whos k won't show anything in the command window and the class line will say
Undefined function or variable 'k'.
which means that you're trying to use k before you ever assigned a value to it.

1 个评论

So I left a comment when I inteded on replying to you earlier; sorry about that! Thanks for your input.

请先登录,再进行评论。

更多回答(1 个)

Hey guys, thanks for responding and sorry for the late reply!
It turns out, Matlab didn't like the way I was defining one input variable in terms of another (f=(x) ... in terms of k and L)
[eN,uN] = femet(A1,k,L,u0,uL,N,dom,f,plott)
It seemed this caused an error for some reason when trying to integrate f later, because it evaluated it directly as it was entered as the input.
I was able to solve by simply removing f as an input, and hardcoding is a variable inside of the function (for the purposes of this assignment we only evaluated one function anyway so I can get away with that, I was just trying to make it as general as possible on the first run).
Thanks for your support!

Community Treasure Hunt

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

Start Hunting!

Translated by