Clear Filters
Clear Filters

why its giving NaN (Not a Number)

2 views (last 30 days)
Ahmad Sheikh
Ahmad Sheikh on 26 Jun 2015
Commented: Ahmad Sheikh on 26 Jun 2015
I am working on takagi Sugeno fuzzy modeling
the example i have done is this one
function derivative_x = book_TS(t,x)
derivative_x = zeros(2,1);
z1=x(1).*(x(2).^2);
z2=(3+x(2)).*(x(1).^2);
a1=1;
a2=-1;
b1=4;
b2=0;
M1=(a1-z1)./(a1-a2)
M2=(z1-a2)./(a1-a2)
M3=(b1-z2)./(b1-b2)
M4=(z2-b2)./(b1-b2)
h1= M1.*M3
h2= M1.*M4
h3= M2.*M3
h4= M2.*M4
h=h1+h2+h3+h4
A1=[-1 1;4 -1];
A2=[-1 1;0 -1];
A3=[-1 -1;4 -1];
A4=[-1 -1;0 -1];
derivative_x(1)= -h1*x(1)+ 4*h1*x(2)-h2*x(1)-h3*x(1)+4*h3*x(2)-h4*x(1);
derivative_x(2)=h1*x(1)-h1*x(2)+h2*x(1)-h2*x(2)-h3*x(1)-h3*x(2)-h4*x(1)-h4*x(2);
when i run this function in a seperate file j get correct results
Initial_Time=0;
Final_Time=10;
[x1,x2] = meshgrid(-2:0.1:2);
[t,x] = ode45(@book_TS, [Initial_Time Final_Time], [0.5 0.5]);
figure(1);
plot(t,x(:,1));
but now i am working on some different example
function derivative_x = example_aircraft(t,x)
derivative_x = zeros(5,1);
z1=-35.137.*x(1).*x(2)-0.6678.*x(1)+0.7870.*x(2)-0.10977;
z2=-8.50255.*x(2)-0.66728-1.043.*x(3);
z3=-132.7./x(1);
a1 = 0.0269;
a2 = -0.0660;
b1 = -7.91;
b2 = -8.73;
c1 = -0.923;
c2 = -2.09;
a= a1-a2;
b= b1-b2;
c= c1-c2;
M11= (a1 - z1) ./ a;
M21= (z1 - a2) ./ a;
M12= (b1 - z2) ./ b;
M22= (z2 - b2) ./ b;
M13= (c1 - z3) ./ c;
M23= (z3 - c2) ./ c;
h1= M11 .* M12 .* M13
h2= M11 .* M12 .* M23
h3= M11 .* M22 .* M13
h4= M11 .* M22 .* M23
h5= M21 .* M12 .* M13
h6= M21 .* M12 .* M23
h7= M21 .* M22 .* M13
h8= M21 .* M22 .* M23
h=h1+h2+h3+h4+h5+h6+h7+h8
derivative_x(1)= -0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4)-0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4)-0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4)-0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4)-0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4)-0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4)-0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4)-0.0142*x(1)+1.47*x(2)+0.0374*x(3)-9.80*x(4);
derivative_x(2)= -0.000368*x(1)-0.758*x(2)+0.934*x(3)-0.000368*x(1)-0.758*x(2)+0.934*x(3)-0.000368*x(1)-0.758*x(2)+0.934*x(3)-0.000368*x(1)-0.758*x(2)+0.934*x(3)-0.000368*x(1)-0.758*x(2)+0.934*x(3)-0.000368*x(1)-0.758*x(2)+0.934*x(3)-0.000368*x(1)-0.758*x(2)+0.934*x(3)-0.000368*x(1)-0.758*x(2)+0.934*x(3);
derivative_x(3)= -0.0660*x(1)-8.73*x(2)-2.09*x(3)-0.0660*x(1)-8.73*x(2)-0.923*x(3)-0.0660*x(1)-7.91*x(2)-2.09*x(3)-0.0660*x(1)-7.91*x(2)-0.923*x(3)+0.0269*x(1)-8.73*x(2)-2.09*x(3)+0.0269*x(1)-8.73*x(2)-0.923*x(3)+0.0269*x(1)-7.91*x(2)-2.09*x(3)+0.0269*x(1)-7.91*x(2)-0.923*x(3);
derivative_x(4)= 8*x(3);
derivative_x(5)= 150*x(2)-150*x(4)+150*x(2)-150*x(4)+150*x(2)-150*x(4)+150*x(2)-150*x(4)+150*x(2)-150*x(4)+150*x(2)-150*x(4)+150*x(2)-150*x(4)+150*x(2)-150*x(4);
Now when i am running this one
Initial_Time=0;
Final_Time=10;
[x1,x2] = meshgrid(-2:0.1:2);
[t,x] = ode45(@example_aircraft, [Initial_Time Final_Time], [0 12 13 0 0]);
figure(1);
plot(t,x(:,1));
xlabel('time')
ylabel('state x1k')
its saying that h1 h2 h3 h4 ...h8 are not a number i-e NaN
kindly do help if any one know why its saying like this.......

Answers (1)

Walter Roberson
Walter Roberson on 26 Jun 2015
You divide by x(1) . Your initial x vector is [0 12 13 0 0] which starts with 0 so your x(1) is going to be 0. You are dividing by 0. You are dividing a positive number by that 0 so the result will be infinity. That infinity is going to "poison" the equations, and if you ever multiply the infinity by 0 you will get NaN as the result. Any arithmetic operation on the NaN is going to result in NaN.
  1 Comment
Ahmad Sheikh
Ahmad Sheikh on 26 Jun 2015
Thanks for your answer but i have changed the initial conditions to
[1 12 13 1 1]
but still giving an error.....

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!