Undefined function or variable t

9 次查看(过去 30 天)
Emily1234
Emily1234 2015-10-6
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 个评论
dpb
dpb 2015-10-6
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.

请先登录,再进行评论。

回答(2 个)

Colton
Colton 2015-10-6
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 2015-10-6
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.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by