Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

01 Python sample scripts

The document contains various Python code snippets demonstrating different programming concepts such as function definition, loops, differentiation, and numerical methods. It includes examples for converting temperature, calculating sine values, simulating ball motion, and implementing the Newton-Raphson method for solving equations. Each section illustrates specific mathematical computations and their implementations in Python.

Uploaded by

athishnatarajan1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

01 Python sample scripts

The document contains various Python code snippets demonstrating different programming concepts such as function definition, loops, differentiation, and numerical methods. It includes examples for converting temperature, calculating sine values, simulating ball motion, and implementing the Newton-Raphson method for solving equations. Each section illustrates specific mathematical computations and their implementations in Python.

Uploaded by

athishnatarajan1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Function

def convertF(TC):

# Converts from centigrade to Fahrenheit

TF=(9/5)*TC+32

return TF

2. Loop

for i in range(10):

x=i*0.1

print(round(x,25))

3. Loop – Sin x
from math import*
x0, x1, dx=0, 10, 0.1
n=int(((x1-x0)/dx)+1)
for i in range(n):
x=x0+i*dx
y=sin((pi/180)*x)
print(i)
print(x)
print(y)

4. While loop – Ball position

t0, dt=0,1

i=int((1000-4.9*(t0**2)))

while i>=0:
t=t0+i*dt

x=1000-4.9*(t**2)

i=i-100

print('The ball touches at every',t,'seconds','of',i,'increments','on position of',x)

5. Incremental loop – Sinx


from math import*
dx=0.1
x,i=0,0
while i<=10:
x=x+i*dx
y=sin((pi/180)*x)
i=i+1
print('Incrementaion of' ,i, 'gives the value of', x, 'with the solution is', y)

6. Exponential loop – Sinx


from math import*
import csv
for x in range(floor(11/0.3)):
i=0.3*x
y=sin((pi/180)*i)
print('The solution of sinx for', round(i,2), 'equals to', round(y,4))

7. Incremental loop – For


from math import*
a=1
for x in range(11):
f=(x**2)*(exp(-a*x))*(sin(pi*x))
print('The solution of the function is',f)
[(x**2)*exp(-a*x)*(sin(pi*x)) for x in range(11)]
8. 1st order differentiation
# f(x)=e^-x^2 and its derivative using the formula
# f'(x)=(f(x+h)-f(x-h))/2h

from math import*


for x in range(11):
y=exp(-x**2)
h=1
if h==1:
i=float(x)+float(h)
j=float(x)-float(h)
Fi=exp(-i**2)
Fj=exp(-j**2)
F=((Fi-Fj)/2*h) #Differentiation
print(x,y,F)

#You can print each variable at once to store the data in the excel till you
#come to know about the csv file format

#While loop function

k=0
while k<=10:
k=k+1
z=sin(k/10)
print(k,z)
9. Acceleration
g,y0=9.8,1.6
time=[]
displacement=[]
v=[]
for i in range(6):
t=round((0.1*i),3)
x=round((y0-(1/2)*g*(t**2)),3)
print('Position',round(x,3),'@',t)
time.append(t)
displacement.append(x)
print(time)
print(displacement)
velocity=[]
delta_time=[]
for j in range(len(displacement)-1):
X=displacement[j+1]-displacement[j]
t=time[j+1]-time[j]
v=round((X/t),3)
t=round(t,3)
velocity.append(v)
delta_time.append(t)
print('delta_time',delta_time)
print(velocity)
acceleration=[]
time_acceleration=[]
for k in range(1,(len(velocity))):
V=velocity[k]-velocity[k-1]
t=delta_time[k]
a=round((V/t),3)
acceleration.append(a)
print(acceleration)
10. Acceleration to displacement
time=[]
acceleration=[0,1.43,2.8,4.13,5.62,7.21]
for i in range(len(acceleration)+1):
t=round((0.1*i),3)
time.append(t)
print(time)
print(acceleration)
velocity=[]
velocity.append(0)
for j in range(len(acceleration)-1):
v=abs(round((velocity[0]+(acceleration[j+1]*(time[j+1]-
time[j])+acceleration[j]*(time[j+1]-time[j]))),3))
velocity.append(v)
print(velocity)
displacement=[]
displacement.append(0)
for k in range(len(velocity)-1):
x=abs(round((displacement[0]+(velocity[k+1]*(time[k+1]-
time[k])+velocity[k]*(time[k+1]-time[j]))),3))
displacement.append(x)
print(displacement)

#del_time=[]
#for j in range(1,len(time)):
# del_t=round((time[j]-time[j-1]),3)
# del_time.append(del_t)
#print(del_time)
#velocity=[]
#velocity.append(0)
#for k in range(1,len(time)):
# v=round((velocity[k-1]+acceleration[k-1]*del_time[k-1]),3)
# velocity.append(v)
#print(velocity)

#displacement=[]
#displacement.append(0)
#for l in range(1,len(time)):
# x=round((displacement[l-1]+velocity[l-1]*del_time[l-1]),3)
# displacement.append(x)
#print(displacement)

11. Newton-raphson method


# ------------------------------------------------
# Program truss
# ------------------------------------------------

# Newton-Raphson solver for 1 D.O.F. truss example


# Inputs: d-horizontal span; x,a -initial height and area
# E-Young modulus, nincr-no. of load increments,
# fincr-force increment, cnorm-residual force convergence norm
# miter-max. no of Newton-Raphson iterations

from math import*


d,x,a,E,f,resid=2500,2500,100,5e5,0,0
nincr,fincr,cnorm,miter=1,1.5e7,1e-20,20
#Initialize geometry data and stiffness
lzero=sqrt(x**2+d**2)
vol=a*lzero
stiff=(a/lzero)*E*((x/lzero)**2)
#Starts load increments
incrm=0
while incrm<=nincr:
f=f+fincr
resid=resid-fincr
#Newton-Raphson iteration
rnorm=cnorm*2
niter=0
while((rnorm>=cnorm)and(niter<=miter)):
niter=niter+1
#find geometry increment
u=-resid/stiff
x=x+u
l=sqrt(x**2+d**2)
a=vol/l
#find stresses and residual force
stress=E*log(l/lzero)
t=stress*a*(x/l)
resid=t-f
rnorm=abs(resid/f)
print(incrm, niter, rnorm,x, f)
#find stiffness and check stability
stiff=(a/l)*(E**(-2))*stress*(x/l)*(x/l)+(stress*(a/l))
if(abs(stiff)<=1e-20):
print('Near zero stiffness and STOP')

You might also like