Multiple variable assignment with Coffeescript - coffeescript

How can I initialize multiple variables at once?
var alpha, beta, theta = null
console.log alpha #=> null
console.log beta #=> null
console.log theta #=> null

should be something like
alpha = beta = theta = null

Related

Getting a not enough input arguments error

I'm trying to plot the Yukawa Potential in Matlab and I want to have my program go through user inputs for the values alpha (called alph in my program) and l. The values I need to use are 0.1, 0.2 and 0.3 for alpha with values of 0, 1 and 2 of l for each value of alpha. I know I could set up a loop for this but it doesn't have to be pretty and I want to test the values one at a time. Anyway I keep getting an error after I input the values for alpha, the error I keep getting is in my function, saying that I don't have enough input arguments. The output should be the T matrix, the Hamiltonian matrix and a plot of the first 10 eigenfunctions.
I've tried going in and simply defining alpha as the numbers I want to look at and the program works fine with displaying the output I'm looking for. I just want to be able to change the values for alpha without having to change the program itself. I haven't had any problems with the l inputs.
r = linspace(0.05,19.95,1999)
n = 1999
dr = 0.05
a = full(gallery("tridiag",n,1,-2,1))
T = -0.5*a/(dr^2)
l = input('Input a value for l.')
alph = input('Input a value for alpha.')
v = arrayfun(#(r) yuk_pot(r,l),r);
V = diag(v)
H = T + V
[O,D] = eig(H);
plot(r,O(:,1),r,O(:,2),r,O(:,3),r,O(:,4),r,O(:,5),r,O(:,6),r,O(:,7),r,O(:,8),r,O(:,9),r,O(:,10))
function v = yuk_pot(r,alph,l)
v = (-exp(-alph*r)/r) + 0.5*(l*(l+1)/(r^2));
end
your function function v = yuk_pot(r,alph,l) has 3 input arguments.
you call it with 2 arguments (r and l)
v = arrayfun(#(r) yuk_pot(r,l),r);
what about the second alph argument?

converting/ translate from Python to Octave or Matlab

I have a Python-Code and want to rewrite it in Octave, but I meet so many problems during the converting. I found a solution for some of them and some of them still need your help. Now i would start with this part of the code :
INVOLUTE_FI = 0
INVOLUTE_FO = 1
INVOLUTE_OI = 2
INVOLUTE_OO = 3
def coords_inv(phi, geo, theta, inv):
"""
Coordinates of the involutes
Parameters
----------
phi : float
The involute angle
geo : struct
The structure with the geometry obtained from get_geo()
theta : float
The crank angle, between 0 and 2*pi
inv : int
The key for the involute to be considered
"""
rb = geo.rb
ro = rb*(pi - geo.phi_fi0 + geo.phi_oo0)
Theta = geo.phi_fie - theta - pi/2.0
if inv == INVOLUTE_FI:
x = rb*cos(phi)+rb*(phi-geo.phi_fi0)*sin(phi)
y = rb*sin(phi)-rb*(phi-geo.phi_fi0)*cos(phi)
elif inv == INVOLUTE_FO:
x = rb*cos(phi)+rb*(phi-geo.phi_fo0)*sin(phi)
y = rb*sin(phi)-rb*(phi-geo.phi_fo0)*cos(phi)
elif inv == INVOLUTE_OI:
x = -rb*cos(phi)-rb*(phi-geo.phi_oi0)*sin(phi)+ro*cos(Theta)
y = -rb*sin(phi)+rb*(phi-geo.phi_oi0)*cos(phi)+ro*sin(Theta)
elif inv == INVOLUTE_OO:
x = -rb*cos(phi)-rb*(phi-geo.phi_oo0)*sin(phi)+ro*cos(Theta)
y = -rb*sin(phi)+rb*(phi-geo.phi_oo0)*cos(phi)+ro*sin(Theta)
else:
raise ValueError('flag not valid')
return x,y
def CVcoords(CVkey, geo, theta, N = 1000):
"""
Return a tuple of numpy arrays for x,y coordinates for the lines which
determine the boundary of the control volume
Parameters
----------
CVkey : string
The key for the control volume for which the polygon is desired
geo : struct
The structure with the geometry obtained from get_geo()
theta : float
The crank angle, between 0 and 2*pi
N : int
How many elements to include in each entry in the polygon
Returns
-------
x : numpy array
X-coordinates of the outline of the control volume
y : numpy array
Y-coordinates of the outline of the control volume
"""
Nc1 = Nc(theta, geo, 1)
Nc2 = Nc(theta, geo, 2)
if CVkey == 'sa':
r = (2*pi*geo.rb-geo.t)/2.0
xee,yee = coords_inv(geo.phi_fie,geo,0.0,'fi')
xse,yse = coords_inv(geo.phi_foe-2*pi,geo,0.0,'fo')
xoie,yoie = coords_inv(geo.phi_oie,geo,theta,'oi')
xooe,yooe = coords_inv(geo.phi_ooe,geo,theta,'oo')
x0,y0 = (xee+xse)/2,(yee+yse)/2
beta = atan2(yee-y0,xee-x0)
t = np.linspace(beta,beta+pi,1000)
x,y = x0+r*np.cos(t),y0+r*np.sin(t)
return np.r_[x,xoie,xooe,x[0]],np.r_[y,yoie,yooe,y[0]]
https://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html I just don´t understand the last Output, and I am still confuse what´s mean _r here, and how can I write it by Octave?....I read what is written in the link, but it still not clear for me.
return np.r_[x,xoie,xooe,x[0]], np.r_[y,yoie,yooe,y[0]]
The function returns 2 values, both arrays created by np.r_.
np.r_[....] has indexing syntax, and ends up being translated into a function call to the np.r_ object. The result is just the concatenation of the arguments:
In [355]: np.r_[1, 3, 6:8, np.array([3,2,1])]
Out[355]: array([1, 3, 6, 7, 3, 2, 1])
With the [] notation it can accept slice like objects (6:8) though I don't see any of those here. I'd have to study the rest of the code to identify whether the other arguments are scalars (single values) or arrays.
My Octave is rusty (though I could experiment with the conversion).
t = np.lispace... # I think that exists in Octave, a 1000 values
x = x0+r*np.cos(t) # a derived array of 1000 values
xoie one of the values returned by coords_inv; may be scalar or array. x[0] the first value of x. So the r_ probably produces a 1d array made up of x, and the subsequent values.

matlab: variables in terms of functions

I have a problem similar to below:
A(n-1) = n*A(n) + A(n+1)
Given
A(20) = 0
A(19) = beta
Calculate beta if
A(0) = 0.5
So for example I'll get for
n = 19, A(18) = 19*beta
n = 18, A(17) = 18*19*beta - beta
My question is, can instead of manually expanding, can I store each A(n) value in a matrix in terms of beta and expand so that I get a final A(0) = ... some function in terms of beta.
Would it be possible to use anonymous functions?
This will take you as far back as A(1).
syms b;
A = [repmat(0,[1,18]),b,0]
for n = 19:-1:2
A(n-1) = n*A(n) + A(n+1);
end
Then you just need A(1), A(2) and your already known value for A(0), to create the final equation you need to solve/subs.
You do not need to compute this using a variable. Since everything is linear, the sequence with A(19)=beta is the same as the sequence with A(19)=1, where then every sequence element is multiplied with beta.
Thus, compute A(0) from A(20)=0 and A(19)=1 and set beta=0.05/A(0).

Compute the convolution of two arrays in MATLAB

I am trying to generate an array from some starting values using this formula in MATLAB:
yt = a0 + ∑i=1p (ai ⋅ yt-i), t ≥ p
p is some small number compared to T (max t). I have been able to make this using two for cycles but it is really slow. Is there some easy way to do it?
First p values of y are provided and vector a (its length is p+1) is provided too...
This is what I have so far, but now when I tried it, it doesn't work 100% (I think it's because of indexing from 1 in MATLAB):
y1 = zeros(T+1, 1);
y1(1:p) = y(1:p);
for t = p+1:T+1
value = a1(1);
for j = 2:p+1
value = value + a1(j)*y1(t-j+1);
end
y1(t) = value;
end
EDIT: I solved it, I am just not used to Matlab indexing from 1...
This statement
if(p>=t)
looks odd inside a loop whose index expression is
for t = p+1:T+1
which seems to guarantee that t>p for the entire duration of the loop. Is that what you meant to write ?
EDIT in response to comment
Inside a loop indexed with this statement
for j = 2:p
how does the reference you make to a(j) ever call for a(0) ?
y1 = zeros(T+1, 1);
y1(1:p) = y(1:p);
for t = p+1:T+1
value = a1(1);
for j = 2:p+1
value = value + a1(j)*y1(t-j+1);
end
y1(t) = value;
end

Matlab string array

All i am trying to do is this:
type = cell(size(A));
...
i = find(A == 0);
type{i} = 'pasok';
However it miserably fails is size(A) > 1 or if i is empty.
Is there any workaround for this problem?
UPDATE -ERROR
type =
[] []
ans =
1 2
i =
1 2
The right hand side of this assignment has too few values to satisfy
the left hand side.
Error in ellipse (line 48)
type{i} ='pasok';
To assign one value to multiple cell-entries at once, you can use
[type{i}] = deal('pasok');
Note that type{i} has to be in square brackets.