Question:
Matlab: 'undefined function or variable 't'' when trying to run a function?
4_Eternity
2012-10-07 20:05:18 UTC
When I enter
euler(t^2,0,10,1,0.1)
into Matlab to try and run it, the above error comes up. The m-file is in the correct directory. I was wondering what this is caused by/how I can fix it?
Thanks

The code:
function(x,y)=euler(f,xinit,yinit,xfin…

%Euler Approximation of ODEs.



h=(xfinal-xinit)/n %Calculation of step sizes
x=(xinit zeros (1,n));
y=(yinit zeros (1,n)); %Initialisation of x and y as arrays

for i=1:n
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*f(x(i),y(i));
end

end
Five answers:
Ian
2012-10-07 22:16:57 UTC
I put your function into matlab and there are a couple of issues, I'll start with your specific question then go from there:



-The error 'undefined function or variable 't'.' means that matlab does not know what value 't' is. You must define a value for 't' somewhere outside of the function (in the .m file) before you call the function. Example, if I have this code:



function yahooanswers



ans = chicken(t)



function[x]=chicken(p)

x = p + 2;

end

end



In my code I have used the function 'chicken' and I have told it, use 'chicken' with input 't'. But I have not told it what 't' is, so it will give me the same error you receive. To get the correct answer I must define 't' in my code, outside of the function, thus:



function yahooanswers

t = 2;

ans = chicken(t)



function[x]=chicken(p)

x = p + 2;

end

end



Now that matlab knows 't=2', when I call my function with input 't' it will run the function with this input and return 'ans = 4'. (you know, cuz 2+2=4)



Something else to be wary of, your function definition format is not entirely correct. Instead of using square brackets ' [ ] ' you are using round brackets ' () ' to define your outputs. Matlab won't recognize this. The format to define the function should be:

function[x,y] = funcname(a,b,c,d)

And to Call it:

[ans1,ans2] = funcname(w,x,y,z)



Also, it is unclear what you are attempting to do at the line: x=(xinit zeros (1,n)) because 'n' is not defined in your function name, but I assume it's your intended number of steps. I can tell you that your formatting will cause an error, even if 'n' is defined. Firstly, when defining arrays you must use square brackets. x = [ M N ] Secondly, you can't have a space between 'zeros' and (1,n).



Also, in the beginning of your comment that 'h' is your stepsize, therefore 'n' should be the number of steps you want to take. When you call your function you are calling 'n' as a 0.1 which will create an error since your number of steps must be an integer.



That is the most I can deduce until the above errors are corrected since I am not completely certain of your variable selection. Good luck! This class is a pain, hope you do well.
trickett
2016-10-03 12:02:33 UTC
Undefined Function Or Variable Matlab
Mercedez
2015-08-14 18:41:02 UTC
This Site Might Help You.



RE:

Matlab: 'undefined function or variable 't'' when trying to run a function?

When I enter

euler(t^2,0,10,1,0.1)

into Matlab to try and run it, the above error comes up. The m-file is in the correct directory. I was wondering what this is caused by/how I can fix it?

Thanks



The code:

function(x,y)=euler(f,xinit,yinit,xfin…



%Euler Approximation of...
Jonathan
2012-10-07 20:08:40 UTC
I didn't read all of your code, just the top half.. The above error means that you didn't assign "t" to any value. So you need to find a part in your code where say x=t, or y=t... I hated matlab, best of luck
2016-03-17 06:45:14 UTC
stupid hippie


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...