Why do I get error undefined function or variable?
显示 更早的评论
% Runge Kutta
h = 0.1;
t1 = 0:h:50;
n = length(t1);
v1 = zeros(1,n);
v1(1)=0;
for i = 1:n-1
k1 = velocity1(t1(i),v1(i));
k2 = velocity1(t1(i)+h/2,v1(i)+0.5*k1*h);
k3 = velocity1(t1(i)+h/2,v1(i)+0.5*k2*h);
k4 = velocity1(t1(i)+h,v1(i)+k3*h);
v1(i+1) = v1(i) + (k1+2*k2+2*k3+k4)*h/6;
if ( abs(v1(i+1))<0 || ~isreal(v1(i+1)) )
tf = t1(i);
break
end
end
vel1_RK4 = v1(1:i);
t1_RK4 = t1(1:i);
h = 0.1;
t2 = 50:h:330;
n = length(t2);
v2 = zeros(1,n);
v2(1) = 10;
for i = 1:n-1
k1 = velocity2(t2(i),v2(i));
k2 = velocity2(t2(i)+h/2,v2(i)+0.5*k1*h);
k3 = velocity2(t2(i)+h/2,v2(i)+0.5*k2*h);
k4 = velocity2(t2(i)+h,v2(i)+k3*h);
v2(i+1) = v2(i) + (k1+2*k2+2*k3+k4)*h/6;
if ( abs(v2(i+1))<0 || ~isreal(v2(i+1)) )
tf = t2(i);
break
end
end
vel2_RK4 = v2(1:i);
t2_RK4 = t2(1:i);
figure
plot (t1_RK4,vel1_RK4,t2_RK4,vel2_RK4)
xlabel( 'Time(s)' )
ylabel( 'Velocity (m/s)' )
title( 'Velocity VS Time using Runge-Kutta' )
legend( 'V1(Before ShutDown)' , 'V2 (After ShutDown)' )
2 个评论
KSSV
2020-10-9
You have to define the variables velocity1 ..did you define those?
Sudhakar Shinde
2020-10-9
It seems velocity1 & velocity2 variables are not defined.
回答(1 个)
Shashank Gupta
2020-10-12
0 个投票
you haven't define some variable in your code. these error occur when compiler trying to access a function or variables and it is not defined. Do check your code and see if all variable are properly declared. From the first look, "velocity" does seems like not defined. check other variable too.
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!