Calculate root mean square?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate root mean square?
# 1  
Old 09-08-2015
Calculate root mean square?

Dear friend,

I know for a single case, this could be finished quickly with Excel. But if we have hundreds of files, we definitely want to do it with a script or a FORTRAN code. Since I have no knowledge of FORTRAN, I tried to work out a script to do it. The math is very simple. we chose one atom (in x, y, z, Cartesian coordinate), and we calculate the distance from other atom to the reference atom with the simple math sqrt((x(i)-x(1))^2+(y(i)-y(1))^2+(z(i)-z(1))^2)
Problem explanation: we have a list of numbers
Code:
P               1.219142       0.637315      -0.824280
N              -0.265369       0.551699      -1.665314
P              -1.232957      -0.291982      -0.536350
C               1.550001       2.370502      -0.242803
C               0.756639       2.983923       0.747041
C               1.003518       4.293274       1.173466
C               2.045233       5.026004       0.616725
C               2.835510       4.452541      -0.372444
C               2.587869       3.143957      -0.800209
C               2.645951       0.045340      -1.859172

assume we set the P1 as the center atom, now we need to calculate all the distance of other atoms from P, respectively.
and we print out the results as follows,
Code:
N2-P1 P3-P1 C4-P1 C5-P1 C6-P1 C7-P1 C8-P1 C9-P1 C10-P1
1.708 2.638 1.858  2.862 4.172  4.692 4.168 2.856 1.859

Thank you very much in advance!
Zhen

Last edited by liuzhencc; 09-08-2015 at 11:54 AM..
# 2  
Old 09-08-2015
Hi,

Try:
Code:
awk '
  NR==1 { 
    split($0,F)
    next
  }
  { 
    h=h $1 NR "-" F[1]1 OFS
    r=r sqrt(($2-F[2])^2+($3-F[3])^2+($4-F[4])^2) OFS
  }
  END {
    print h ORS r
  }
' CONVFMT="%.3f" file

----

You could try this variation for multiple files:
Code:
awk '
  FNR==1 {
    if(NR>1)
      print h ORS r
    split($0,F)
    h=r=x
    next
  }
  {
    h=h $1 FNR "-" F[1]1 OFS
    r=r sqrt(($2-F[2])^2+($3-F[3])^2+($4-F[4])^2) OFS
  }
  END {
    print h ORS r
  }
' CONVFMT="%.3f" file(s)


Last edited by Scrutinizer; 09-08-2015 at 12:44 PM..
# 3  
Old 09-08-2015
Thanks so much for the script. It works like a charm.
Would you please give me some explanation about this script? How does it works so fast. Since you use unformatted print inside awk, so the bond title and bond distance are not aligned with each other if there are hundreds of bond.
for instance, there are 19 bond titles and 24 distances
Code:
P2-Cr1 N3-Cr1 P4-Cr1 C5-Cr1 C6-Cr1 C7-Cr1 C8-Cr1 C9-Cr1 C10-Cr1 C11-Cr1 C12-Cr1 C13-Cr1 C14-Cr1 C15-Cr1 C16-Cr1 C17-Cr1 C18-Cr1 C19-Cr1 C20-Cr1
2.270 2.872 2.249 3.526 3.961 5.272 6.070 5.812 4.655 3.494 3.886 5.197 6.016 5.787 4.646 3.486 3.883 5.197 6.017 5.785 4.641 3.490 4.027 5.305

# 4  
Old 09-08-2015
lining up headers/values for Scrutinizer's code:
Code:
BEGIN {
  CONVFMT="%.3f"
}
NR==1 {
    split($0,F)
    next
  }
  {
    ht=sprintf("%-9s",$1 NR "-" F[1]1)
    rt=sprintf("%-9s",sqrt(($2-F[2])^2+($3-F[3])^2+($4-F[4])^2))
    h=(h)? h OFS ht:ht
    r=(r)?r OFS rt:rt
  }
  END {
    print h ORS r
  }

# 5  
Old 09-08-2015
Great! Now, these two rows are aligned within each column. The output is perfectly formatted.
Would you please explain a little bit on 'split', 'sprintf', 'OFS', 'h ORS r'?
How does it work? It's an advanced script with so many keywords I've never seen before.
Thanks you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

The square root code I coded on this site...

Hi all, (mainly Neo)... I keep noticing that the SQRT code I wrote recently for a POSIX shell keeps appearing, (the green colour sticks out like a sore thumb). So I decided to take a look on Google. Guess what? UNIX.COM comes first in Google's listing just from two words, see image... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Mean square root error from different files

For example, I have files called A.txt and B.txt. A.txt #x yj 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100B.txt #x ^yj 1 1 2 8 3 27 4 64 5 125 6 216 (2 Replies)
Discussion started by: Tzeronone
2 Replies

3. Solaris

Migration of system having UFS root FS with zones root to ZFS root FS

Hi All After downloading ZFS documentation from oracle site, I am able to successfully migrate UFS root FS without zones to ZFS root FS. But in case of UFS root file system with zones , I am successfully able to migrate global zone to zfs root file system but zone are still in UFS root file... (2 Replies)
Discussion started by: sb200
2 Replies

4. Shell Programming and Scripting

AWK script for standard deviation / root mean square deviation

I have a file with say 50 columns, each containing a whole lot of data. Each column contains data from a separate simulation, but each simulation is related to the data in the last (REFERENCE) column $50 I need to calculate the RMS deviation for each data line, i.e. column 1 relative to... (12 Replies)
Discussion started by: chrisjorg
12 Replies

5. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

7. UNIX for Dummies Questions & Answers

Area of a square

I am just starting out with bash scripting. I tried a simple script to find the area of a square and it didnt run. #!/bin/bash #script to find area of a square based on user input if then echo " Usage -$0 x " echo " where x is the dimension of the square " exit 1 n1=$1 ... (2 Replies)
Discussion started by: SnydeMz
2 Replies

8. Shell Programming and Scripting

Math Square Root

Hi I just stumbled on to these forums today, and I relatively new to unix. I am trying to figure out how to find the square root of a number using a shell script. I am using bash and I have searched and searched but cannot figure out square roots. Hopefully someone can point me in the right... (3 Replies)
Discussion started by: davex4285
3 Replies

9. UNIX for Dummies Questions & Answers

Run non-root script as root with non-root environment

All, I want to run a non-root script as the root user with non-root environment variables with crontab. The non-root user would have environment variables for database access such as Oracle or Sybase. The root user does not have the Oracle or Sybase enviroment variables. I thought you could do... (2 Replies)
Discussion started by: bubba112557
2 Replies

10. UNIX for Dummies Questions & Answers

square brackets

I would like to substitute a phrase which contains square brackets. change TO how? Thanks (2 Replies)
Discussion started by: gilead29
2 Replies
Login or Register to Ask a Question