Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
My code:
import Pkg
Pkg.add("PyPlot")
using PyPlot
e = 0.0
eta = 1e - 4
wmin = -2.0
wmax = 2.0
Nw = 1000
w =range(wmin, wmax, Nw)
g = 1 ./ (w .- e .+ eta * im)
t = 1.0
g2 = g .^2
Gsemi = (1 ./ (g*2*t^2)) .* (1 .- sqrt(1 .- 4*t^2*g .^2)) //Error line
Ginf = Gsemi ./ (1 .- Gsemi .^ 2 * t^2)
xlabel(L"Energy $\omega $", fontsize=20)
ylabel("Density of states", fontsize=20)
axis([-2,2,0,1.4])
plot(w, (-1.0/pi)*imag(Ginf), linewidth=3.0)
The error message:
How can I fix this? sorry my english is bad, thank for anwers
This particular error happens because you cannot take the square root of a vector, since it does not make sense mathematically.
But you can take the square root of each element separately. To do that add a dot to the sqrt function call:
sqrt.(1 .- 4*t^2*g .^2)
But you should really add more dots on that line to save some temporary allocations. You can do that with the #. macro instead of dotting everything by hand, like this:
Gsemi = #. (1 / (g*2*t^2) * (1 - sqrt(1 - 4*t^2*g^2)))
Now, all operations will be element-wise.
Also, make sure to put your code inside a function.
Related
So I'm setting out to recreating some math functions from the math library from python.
One of those functions is the math.gamma-function. Since I know my way around JavaScript I thought I might try to translate the JavaScript implementation of the Lanczos approximation on Rosetta code into Applescript code:
on gamma(x)
set p to {1.0, 676.520368121885, -1259.139216722403, 771.323428777653, -176.615029162141, 12.507343278687, -0.138571095266, 9.98436957801957E-6, 1.50563273514931E-7}
set E to 2.718281828459045
set g to 7
if x < 0.5 then
return pi / (sin(pi * x) * (gamma(1 - x)))
end if
set x to x - 1
set a to item 1 of p
set t to x + g + 0.5
repeat with i from 2 to count of p
set a to a + ((item i of p) / (x + i))
end repeat
return ((2 * pi) ^ 0.5) * (t ^ x + 0.5) * (E ^ -t) * a
end gamma
The required function for this to run is:
on sin(x)
return (do shell script "python3 -c 'import math; print(math.sin(" & x & "))'") as number
end sin
All the other functions of the Javascript implementation have been removed to not have too many required functions, but the inline operations I introduced produce the same result.
This Javascript-code works great when trying to run it in the browser-console, but my Applescript implementation doesn't produce answers anywhere near the actual result. Is it because...
...I implemented something wrong?
...Applescript doesn't have enough precision?
...something else?
You made two mistakes in your code:
First of all, the i in your repeat statement starts at 2 rather than 1, which is fine for (item i of p), but it needs to be subtracted by 1 in the (x + i).
Secondly, in the code (t ^ x + 0.5) in the return statement, the t and x are being calculated first since they are exponents and then added to 0.5, but according to the JavaScript implementation the x and 0.5 need to be added together first instead.
I'm not understanding why the following snippet of code is returning a NoMethodError in Julia
using Calculus
nx = 101
nt = 101
dx = 2*pi / (nx - 1)
nu = 0.07
dt = dx*nu
function init(x, nu, t)
phi = exp( -x^2 / 4.0*nu ) + exp( -(x - 2.0*pi)^2 / 4.0*nu )
dphi_dx = derivative(phi)
u = ( 2.0*nu /phi )*dphi_dx + 4.0
return u
end
x = range(0.0,stop=2*pi,length=nx)
t = 0.0
u = [init(x0,nu,t) for x0 in x]
My aim here is to populate the elements of an array named u with values as calculated by my function init. The u array should have nx elements with u calculated at every x value in the range between 0.0 and 2*pi.
Next time please also post the error message and take a detailed at it before, so you can try to spot the mistake by yourself.
I don't really know the Calculus package but it seems you are using it wrong. Your phi is a number and not a function. You can't take a derivative from just a single number. Change it to
phi = x -> exp( -x^2 / 4.0*nu ) + exp( -(x - 2.0*pi)^2 / 4.0*nu )
an then call the phi and derivative at argument x, so phi(x) and derivative(phi,x) or dphi_x(x). As I don't know much about the Calculus package you should take a look at its documentation again to verify that the derivative command is doing exactly what you want like that.
Little extra: there are also element-wise operations in Julia (similar to Matlab for example) that apply functions to the whole array. Instead of [init(x0,nu,t) for x0 in x], you can also write init.(x,nu,t).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
How do I calculate velocity for bouncing objects with mass?
f1 = ( (m1-m2)/(m1+m2) )*v1 + ( (m2*2 )/(m1+m2) )*v2
f2 = ( (m1*2 )/(m1+m2) )*v1 + ( (m2-m1)/(m1+m2) )*v2
Doesn't work if both objects collide while heading in the same direction
Any help will be appreiciated
For 1d objects
v1 = (u1(m1-m2) + 2m2u2)/(m1+m2)
v2 = (u2(m2-m1) + 2m1u1)/(m1+m2)
Where v1 and v2 are the velocities after and u1 and u2 are velocities before, with m1 and m2 being the mass of each.
Which is the same but simplified version of the formula you have given. BTW your the code block has an error on line 1 5th var is b2 should be m2
This will give the correct velocities if the bodies are moving in the same direction. All I can think that you may be doing wrong is not including the sign. If two bodies are moving in the same direction then the two velocities input u1 and u2 will have the same sign. For all other collisions they will each have a different sign.
If you are applying the above in a 2D situation you will need to modify the formula. This is the solution for circles where the point of contact is along the angle 'p' between them.
//
// u1 * cos(d1 - p) * (m1 - m2) + 2 * m2 * u2 * cos(d2 - p)
//V1x = -------------------------------------------------------- * cos(p) + u1 * sin(d1 - p) * cos(p + PI / 2)
// m1 + m2
// u1 * cos(d1 - p) * (m1 - m2) + 2 * m2 * u2 * cos(d2 - p)
//V1y = -------------------------------------------------------- * sin(p) + u1 * sin(d1 - p) * sin(p + PI / 2)
// m1 + m2
And do the same for the other object
v1x,v1y are the resulting x,y components of the velocity
u1,u2 is velocity.
m1, m2 is the mass
d1 , d2 us the direction of movement in radians.
p is the angle of contact. This is the angle from the center of first
object to the center of second object and the point of contact is on
the line this angle describes.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have 2 3D points A and B I now assume the parametric equation like:
x = Ax + (Bx*t)
y = Ay + (By*t)
z = Az + (Bz*t)
So this can be refined to:
x = Ax + (Bx * ((y - Ay)/By));
is correct.
Given this I want to know what the coordinates of a point(on A-B) at height 0 are.
so I now do this:
float y = 0;
float t = ((y - Ay) / By)
float x = Ax + (Bx * t);
float z = Az + (Bz * t);
Is there anything mathematically wrong with this?
(my code is not doing what should be happening with this)
Thanks!
PS: The relevance of this question to programming:
In a game engine when projecting points(in this case corners of my view frustum) onto a plane such as the xz-plane with y = 0 this mathematical problem coincides with my game-programming
I would write them this way:
x = A*(1-t) + B*t
So x(0) = A and x(1) = B. Writing it this way assumes 0 <= t <= +1.
Another way to think about it is to assume -1 <= t <= +1. If you go that way the shape functions look like this:
x = A*(1-t)/2.0 + B*(1+t)/2.0
Once again x(-1) = A and x(+1) = B.
And it's easy to generalize to higher-order functions:
x(t) = A*t(t-1)/2 + C*(1-t^2) + B*t(t+1)/2
So x(0) = A, x(0.5) = C, x(1) = B.
I think your parametrization is wrong:
x = Ax + (Bx*t)
...
But it should be:
x = Ax + ((Bx-Ax)*t)
...
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I came across this in some brain teaser question bank.
What should be the answer of this?
( (3+1) / 3) * 6
I thought it is 7 but the correct answer is 8.
That can happen only when it is (3+1) * (6/3). Since there are parenthesis given in the expression how can I get 8? I tried to double check and the answer is correct.
Can somebody help me in understanding this?
Working with integers:
((3 + 1) / 3) * 6
= (4 / 3) * 6
= 1 * 6
= 6
Working with floats:
((3 + 1) / 3) * 6
= (4 / 3) * 6
= (4 / 3) * (6 / 1)
As a fraction:
= (6 * 4) / (3 * 1)
= 24 / 3
= 8
This is just simple arithmetic - nothing to do with programming, nor the difference between infix, prefix and postfix notations:
((3+1) / 3) * 6 = (4 / 3) * 6
= 1.333... * 6
= 8
This brain teaser question bank is clearly expecting you to use the usual rules of mathematics - it's not a C brain teaser question bank after all. Even if you were using integer division instead (where 4 / 3 would give 1), the answer would be 6.
See the result on Wolfram Alpha.
By the sound of it, this is basic arithmetic and has nothing whatsoever to do with programming:
(3 + 1) / 3 * 6 =
4 / 3 * 6 =
(4 * 6) / 3 =
24 / 3 =
8