Undefined function or variable t

조회 수: 1 (최근 30일)
Kenny Teo
Kenny Teo 2021년 3월 29일
답변: James Tursa 2021년 3월 30일
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

답변 (2개)

KSSV
KSSV 2021년 3월 29일
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 2021년 3월 30일
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

카테고리

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