I need to create a script that will calculate the overlap orbital of two 1s orbitals. The integral is given by
I tried calculating this using code but my answer is nowhere near the analytic result of S=(1+R+R^2/3)exp(-R). Could someone help me figure where I went wrong?
The code:
import Foundation
var sum: Double = 0.0 //The integral result
var step_size: Double = 0.0000025
var a: Double = 0.0
var R: Double = 5.0
var next_point: Double = 0.0
var midpoint: Double = 0.0
var height: Double = 0.0
var r_val: Double = 0.0
func psi_func(r_val: Double) -> Double {
return exp(-r_val)
}
//Integration
while next_point < R {
next_point = a + step_size
midpoint = a + step_size/2
height = psi_func(r_val: midpoint)
sum += psi_func(r_val: midpoint)*step_size
a = a + step_size
}
print("S = ", 2*3.14159*3.14159*sum) // This is a 3-D orbital, so I multiply by 2*pi*pi
For R = 5.0
My answer: 19.61
Analytic answer: 0.097
Two problems I can see:
I see a single wavefunction and not the product of two in your code
It is incorrect to just do a 1d integral and multiply by 2pi^2 in the end
Try doing a proper 3d integral with the correct integrand.
Related
I try to compute the autospectrum of a recorded signal, I use the function vDSP_zaspec. Normally the output array is real.
class func autoSpectrum (input: [Float])->[Float] {
var real = [Float](input)
var imaginary = [Float](repeating: 0.0, count : input.count)
var Output:[Float] = [Float](repeating:0 , count: input.count)
let length = vDSP_Length(real.count/2)
real.withUnsafeMutableBufferPointer {realBP in
imaginary.withUnsafeMutableBufferPointer {imaginaryBP in
var splitComplex = DSPSplitComplex(realp: realBP.baseAddress!, imagp: imaginaryBP.baseAddress!)
vDSP_zaspec(&splitComplex, &Output, vDSP_Length(input.count))
}
}
let value = vDSP.rootMeanSquare(Output)
Output = vDSP.divide(Output, value)
return Output
}
I did a test with a 500 Hz sin wave and this is what the Output array looks like :
The chart is far from the expected result...The result looks like the absolute value of the recorded audio file...
If someone could help me, it will be great !
vDSP_zaspec returns the sum of each real part squared and the corresponding imaginary part squared. The Apple documentation describes it as "Compute the element-wise sum of the squares of the real and imaginary parts of a complex vector".
The following pieces of code calculate the same results:
var real: [Float] = [-2, 7, -3]
var imaginary: [Float] = [4, -1, -4]
var vDSPresult: [Float] = [0, 0, 0]
var scalarResult: [Float] = [0, 0, 0]
real.withUnsafeMutableBufferPointer {realBP in
imaginary.withUnsafeMutableBufferPointer {imaginaryBP in
var splitComplex = DSPSplitComplex(realp: realBP.baseAddress!,
imagp: imaginaryBP.baseAddress!)
vDSP_zaspec(&splitComplex,
&vDSPresult,
vDSP_Length(vDSPresult.count))
}
}
print(vDSPresult)
for i in 0 ..< scalarResult.count {
scalarResult[i] = pow(real[i], 2) + pow(imaginary[i], 2)
}
print(scalarResult)
So, I have 14 checkmarks in total and I want to press one and have a number calculate the overall completion for all the checkmarks(14 in total).
I researched through the web and I tried to solved it like this...First, I thought I needed to declare two variables; one for the counter and another for the total (14) , then I declare a constant (which would represent my total value * 100 , and I will finally do the percentage calculation.
My problem comes when I have to define what the overall percentage is calculated.
Code is here:
import UIKit
var checkmarksDouble: Float = 100.0 //this is my total on percentage
var totalCheckmarksCounted: Float = 14.0 //my total on checkmarks
var counter: Float = + 1 //Counter
let perCent = 100*counter/(checkmarksDouble + counter)
var perCentCGFloat = CGFloat(perCent)
Any suggestions?
if u want to calculate total percentage according to checkmark.
var checkmarksDouble: Float = 100.0 //this is my total on percentage
var totalCheckmarksCounted: Float = 14.0 //my total on checkmarks
var counter: Float = 0 //Counter
var perCentCGFloat:Float = 0
var percentageOfOneCheckmark:Float = 0
now calculate percentage for single checkmark
percentageOfOneCheckmark = checkmarksDouble / totalCheckmarksCounted
call this method when you click checkmark
func checkmarkClickForCalculaationPercentage() -> Void
{
counter += 1
perCentCGFloat = percentageOfOneCheckmark *counter
}
This question already has answers here:
How to round a Double to the nearest Int in swift?
(9 answers)
Closed 4 years ago.
I try to use swift code to calculate 10 * 75% = 7.5
var b : Int = 10
var a : Int = 75
// result x is 0
var x = b * (a / 100)
However the result is zero. How to get 7.5 result without changing the type and value of a and b?
UPDATE:
I got it right by:
var x: Double = (Double(b) * (Double(a) / 100)) // x is 7.5
Now, how can I round it to 8 as a Int type?
You're using integer (i.e. whole number) arithmetic, when what you want is floating-point arithmetic. Change one of the types to Float, and Swift will figure out that x is also a Float.
var b : Int = 10
var a : Int = 75
var x = Float(b) * (Float(a) / 100.0) // now x is a Float (.75)
For doing arithmetic in Swift, both sides of the equation must be using the same type. Try using this code:
let b = 10.0
let a = 75.0
let x = b * (a / 100.0
print(x)
7.5
To make it round up, use the built in ceil function
print(ceil(x))
8.0
Ok then you just need to do:
var b : Int = 10
let c = (Double(b) * 0.75)
let restult = ceil(c)
I'm trying to create a game in which a projectile is launched at a random angle.
To do this I need to be able to generate two random Int's. I looked up some tutorials and came up with this:
var random = CGFloat(Int(arc4random()) % 1500)
var random2 = CGFloat(Int(arc4random()) % -300)
self.addChild(bullet)
bullet.physicsBody!.velocity = CGVectorMake((random2), (random))
It worked for a while but now it just crashes.
Any help would be appreciated.
What I find I use the most is arc4random_uniform(upperBound) which returns a random Int ranging from zero to upperBound -1.
let random = arc4random_uniform(1500)
let random2 = arc4random_uniform(300) * -1
//pick a number between 1 and 10
let pick = arc4random_uniform(10)+1
The lowdown on the arc4 functions: arc4random man page
GameplayKit has a nice class wrapping the arc4 functions: GameplayKit Randomness
and a handy reference: Random Hipster
I dont understand very well your issue but I think it could be useful:
func getRandomPointFromCircle(radius:Float, center:CGPoint) -> CGPoint {
let randomAngle = Float(arc4random())/Float(UInt32.max-1) * Float(M_PI) * 2.0
// polar => cartesian
let x = radius * cosf(theta)
let y = radius * sinf(theta)
return CGPointMake(CGFloat(x)+center.x,CGFloat(y)+center.y)
}
I would like to round a Double to a certain number of decimals always rounding down.
Example
rounding to .00 => 1.5679999 to 1.56
rounding to .000 => 1.5679999 to 1.567
round(1.5679999 * 100) / 100 //returns 1.57
round(1.5679999 * 1000) / 1000 //returns 1.568
I want a Double not a concatenated String
Just replace round with floor!
As of swift 3.0:
myDouble.rounded(.down)
SWIFT4+
ways to round on different cases
var w = 6.5
w.round(.toNearestOrAwayFromZero)
// w == 7.0
// Equivalent to the C 'trunc' function:
var x = 6.5
x.round(.towardZero)
// x == 6.0
// Equivalent to the C 'ceil' function:
var y = 6.5
y.round(.up)
// y == 7.0
// Equivalent to the C 'floor' function:
var z = 6.5
z.round(.down)
// z == 6.0