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
Thanks in Advance,
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
|
|
|
|
Reply
|
Martin
|
12/26/2009 4:04:03 PM |
|
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
|
|
|
|
Reply
|
ImageAnalyst
|
12/26/2009 4:38:13 PM
|
|