How to fix 'Undefined function or variable 'temp' in ode45'.

My code below gives me the error
Undefined function or variable 'temp'.
when using the command
[t,u] = ode45(@temp,[0,20],[20]);
which I intent to create a graph with. As a note, I used 'global' to define and then set the values of my constants.
If necessary, the purpose of this code is to create a graph for a CPU temperature based on data from a lab.

 采纳的回答

James Tursa
James Tursa 2017-2-23
编辑:James Tursa 2017-2-23
Where is the file temp.m located? Is it in the current directory or on the MATLAB path? Is it a sub-function? The error indicates MATLAB does not see the file temp.m from where you call ode45.

4 个评论

It's just on my desktop; hasn't been moved but I had a similar problem before. I'll quickly change this and see if it works
I think you may be correct as it doesn't appear to have the correct file path however, I do not know how to define the location of the file 'temp.m' for the code.
Ahah! Found out how, set it all and it's working! Thank you so much for you help, I really appreciate it.
As an extra note for others who may have the problem though. I fixed it by setting a new 'Search Path'. In Matlab; Home>Set_Path>Add_Folder.

请先登录,再进行评论。

更多回答(1 个)

You have not stored the code in temp.m or temp.m is not on your path.
Note: global variables are the slowest kind of variables. You should avoid them. See https://www.mathworks.com/help/matlab/math/parameterizing-functions.html

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by