Undefined function or variable t

3 views (last 30 days)
Emily1234
Emily1234 on 6 Oct 2015
Answered: Star Strider on 6 Oct 2015
So I'm very new to matlab and I have been trying to use the ODE45 function, but it keeps coming up with the following error when I try to run my function file. Theres probably something silly im doing wrong.
Undefined function or variable 't'.
Below is the start of the code
function dydt = react1(t,y)
dydt = zeros(size(t,y));
p1 = ***;
p2 = ***;
p3 = ***;
p4 = ***;
p5 = ***;
p6 = ***;
y(1)=G(t);
y(2)=X(t);
y(3)=I(t);
dydt(1) = -(p1+X(t))*G(t)+p1*Gb;
dydt(2) = -p2*X(t) +p3*(I(t)-Ib);
dydt(3) = p6*abs(G(t)-p5)-p4*(I(t)-Ib);
Any help would be appricated
  1 Comment
dpb
dpb on 6 Oct 2015
Need to see the call to ode45 and the error in context...the problem isn't in the code shown but in the way it is being called that isn't.

Sign in to comment.

Answers (2)

Colton
Colton on 6 Oct 2015
You cannot run function files in Matlab. You have to call the function from another script (after you have saved the function, here as 'dydt.m' in your working directory or your path) or call the function from the command window.
You will need to supply some vectors t and y when you call the function.

Star Strider
Star Strider on 6 Oct 2015
You can call external functions from ODE (and other) functions in MATLAB, but you have to define them as such as function files on your MATLAB path.
It is difficult to follow what you are doing, but if ‘G’, ‘X’, and ‘I’ are functions of time but not defined as such outside your ‘react1’ function, your ‘react1’ function will throw errors.
This could be the correct syntax for your function:
function dydt = react1(t,y)
dydt = zeros(length(y),1); % <- Changed, Must Return A Column Vector
p1 = ***;
p2 = ***;
p3 = ***;
p4 = ***;
p5 = ***;
p6 = ***;
G = y(1);
X = y(2);
I = y(3);
dydt(1) = -(p1+X)*G+p1*Gb;
dydt(2) = -p2*X +p3*(I-Ib);
dydt(3) = p6*abs(G-p5)-p4*(I-Ib);
NOTE ‘Gb’ and ‘Ib’ do not appear to be defined in your code, and they are not passed as additional arguments to ‘react1’. Not explicitly defining them or passing them as additional arguments will cause your ‘react1’ code to crash.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!