Undefined function or variable t

2 views (last 30 days)
Kenny Teo
Kenny Teo on 29 Mar 2021
Answered: James Tursa on 30 Mar 2021
How do i solve the undefined variable t error, do i have to run it from another file ?
function dxdt=Math(t,x1) % function name
x=x1(1)
y=x1(2)
z=x1(3)
dxdt(1,1)= 22222222*z*z %define function
dxdt(2,1)=-0.06*y+11111*x*z
dxdt(3,1)=0.06*y-22222222*z*z-11111*x*z
x0=[0 1 0] % initial value of variable
tspan=[0:1:20] % time interval
[t_out,x_out]=ode45(@(t,x1) fun(t,x1),tspan,x0) % ode45 solver
plot(t_out,x_out(:,1)) %plot of function
xlabel('Time') % x axis name
ylabel('x') % y-axis name
plot(t_out,x_out(:,2)) %plot of function
xlabel('Time') % x axis name
ylabel('y') % y-axis name
plot(t_out,x_out(:,3)) %plot of function
xlabel('Time') % x axis name
ylabel('z') % y-axis name
end

Answers (2)

KSSV
KSSV on 29 Mar 2021
It looks like you are running the function striaght away by hitting the run button. No it cannot be done like that. The functions takes in two inputs, you have to define them and then call the function.
t = your array ; % give your values of t
x1 = your array ; % give valus of x1
dxdt=Math(t,x1) ; % now call the function

James Tursa
James Tursa on 30 Mar 2021
Put this code in a separate file called Math.m
% file Math.m
function dxdt=Math(t,x1) % function name
x=x1(1)
y=x1(2)
z=x1(3)
dxdt(1,1)= 22222222*z*z %define function
dxdt(2,1)=-0.06*y+11111*x*z
dxdt(3,1)=0.06*y-22222222*z*z-11111*x*z
end
Then pass a Math function handle to ode45:
[t_out,x_out]=ode45(@Math,tspan,x0) % ode45 solver

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!