function fout = factor(n)
%factor(n():
% computes the
product of all the integers from 1 to n.
x=1;
for i = 1:n
x = x * i;
end
fout = x;
end
%This code is a numerical solution
to the linear version of the bungee
%jummper problem using
for loop. The results are also
compared against
%analytical solution g = gravity; m=mass; c=drag coeff; delt=time step size
%endtime = end of simutlation time
clear
clc
g=9.8;
m=68.1;
c=12.5; %kg/sec
delt=2;
endtime=20;%simulate until 20 seconds
no_steps=(endtime/delt)+1;
v(1)=0;
vn(1)=0;
time(1)=0;
for i = 2:1:no_steps
time(i)=time(i-1)+delt;
vn(i)=vn(i-1)+(g-(c/m)*vn(i-1))*delt;
v(i)=(g*m/c)*(1-exp(-c*time(i)/m));
end
plot(time,vn,'-r+',time,v,'--b');
xlabel('time
[sec]');
disp('time v_numerical v_analytical')
[time' vn' v']
function f = func(x)
f = 0.125*x.^3 - 1.125*x.^2 + 2.75*x+1;
function favg = funcavg (f,a,b,n)
%funcavg (a,b,n):
%average value of function
%input:
% a = lower bound
% b = upper bound
% n = number of
intervals
%output:
% favg = average value of function
x = linspace (a,b,n);
y = feval(f,x);
favg = mean(y);
function sgn=mysign(x)
%tells if a number is above positive
if x>0
sgn='positive';
elseif x<0
sgn= 'negative';
else
sgn='zero';
end
%general program to compute roots of a quadratic equation
%developed by DR. Clement
%a,b,c
are the coefficient of the equn and r1 and r2 are
real roots
%rp is the real part of imaginary
root, and ipl and ip2 are imaginary
%parts
function quad =imaginaryroots(a,b,c)
if (a==0)
if(b~=0)
r1=-c/b;
disp('Problem has a single root')
r1
else
disp('Trivial Problem')
disp('Both a & b values are zero')
end
else
discr=b^2-4*a*c;
if(discr>=0)
r1 = (-b+sqrt(discr))/(2*a);
r2=(-b-sqrt(discr))/(2*a);
disp('The problem has areal roots')
r1
r2
else
rp=-b/(2*a);
ip1=sqrt(abs(discr))/(2*a);
ip2=-ip1;
disp('The problem has imaginary roots')
disp('The real part is')
rp
disp('The imaginary parts are:')
ip1
ip2
end
end
qo=10;
R=50;
L=5;
C=1e-4;
delt = linspace(0,.5);
qt = qo*exp(-R*delt/(2*L)).*cos(sqrt(1/(L*C)-(R/(2*L))^2)*delt);
plot(delt,qt)
xlabel('time
[sec]');
ylabel(' charge
');
%This code is a numerical solution
to the linear version of the bungee
%jumper problem
clear
g=9.8;m=68.1;c=12.5;
ti=0;tf=20;dt=2;
i=1;
vn(i)=0;t(i)=0;
while(1)
dydt=g-(c/m)*vn(i);
vn(i+1)=vn(i)+ dydt*dt;
i=i+1;
t(i)=t(i-1)+dt;
if t(i)>=tf,break,end
end
disp('time v_numerical')
[t'
vn']
function out = whiledemo ()
% example of while loop
x = 8
while x >0
x = x - 3;
disp (x)
end