Why do I get error undefined function or variable?

3 views (last 30 days)
% 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 Comments
KSSV
KSSV on 9 Oct 2020
You have to define the variables velocity1 ..did you define those?
Sudhakar Shinde
Sudhakar Shinde on 9 Oct 2020
It seems velocity1 & velocity2 variables are not defined.

Sign in to comment.

Answers (1)

Shashank Gupta
Shashank Gupta on 12 Oct 2020
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.

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!