[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: global variables, or ...

From: Carlo de Falco
Subject: Re: global variables, or ...
Date: Wed, 19 Dec 2012 07:40:25 +0100

2012/12/19 gary bollenbach <address@hidden>
Hello Tony,

All variations failed until I posted the thing.  Copied from browser and it
ran.  Sorry for disturbing.


BTW even though your code now runs as expected, it could still be made slightly cleaner
avoiding the use of globals as shown below:

# physics problem

a = input( " What is the speed of the two trains, m/s [20]: " );
b = input( " How far apart are the trains when they spot each other, km [2]:" );

# (a) calculate the deceleration
# -v^2/(2*y) = decel;
decel = (-a^2/2)*(2/(b*1000));

# (b) calculate the distance from decelerating train to impact point
function y = f(x,a,b,decel)
y(1) = decel/2*x(1)^2 + a*x(1) -x(2) + 0;
y(2) = 0*x(1)^2 + a*x(1) +x(2) -b*1000;

[x, fval, info] = fsolve(@(x) f(a,b,decel), [0; 0])

printf( " (a) The mutual, equal deceleration, m/s^s, is [-0.2]: %.4f \n",decel );
printf( " (b) The first x represents the time, s, to impact [58.6]. \n" );
printf( " (b) The second x represents the distance, m, from the \n" );
printf( " (b) decelerating train start point to impact point [828]. \n" );


reply via email to

[Prev in Thread] Current Thread [Next in Thread]