COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Why doesn't the function "dt" always return a number in this MATLAB .m file? See http://pastebay.com/78608

• Follow

```Why doesn't the function "dt" always return a number in this MATLAB .m file?
Please see http://pastebay.com/78608 for the .m script
Martin

..M file:
============================
xs = -.0288952;
xe = 0.0288952;
n = 10;
m = 0.0085;

dx = (xe-xs)/n;
x = (xs:dx:xe);
y = (-1.1061e+011)*x.^7 + (1.5525e+008)*x.^5 + (-17338)*x.^3 + (-29.371)*x;
a = y/m;
t = (0:n);
v = (0:n);
disp(a);

for i = 1 : 1 :n
dt =  sqrt(((v(i).^2)./(a(i).^2))+(2*dx./abs(a(i)))) - v(i)./a(i);
disp(dt);
disp(v(i));
disp(dx/dt);
if a(i)<0
v(i+1) = v(i) - dx/dt;
else
v(i+1) = v(i) + dx/dt;
end
t(i+1) = t(i) + dt;
end
============================
```
 0

```Scripts just put variables into the workspace.  Those variables can
live past when the script finishes running.  However, scripts don't
"return" variables.  To do that you need to turn your script into a
function by putting a function statement at the top of your script.
Besides, dt is a variable, NOT a FUNCTION.

dt should have a value since n = 10 so that means that it MUST enter
your loop and thus dt MUST get assigned a value.  But in your loop,
sometimes a = 0 which means that you'll have Nan for the expression
(((v(i).^2)./(a(i).^2))+(2*dx./abs(a(i)))) - v(i)./a(i)
and thus, dt will also be NaN.
```
 0