What does colon mean in fortran? - matlab

I'm trying to convert fortran code to matlab, I was wondering if someone could help me with this subroutine.
I'm specifically asking what does the colon mean in these lines?
SUB Taper (a(), co(), Re(), Im())
FOR nd = 0 TO 31
n1 = 8 * nd: n2 = a(n1 + 4): n1 = a(n1): n0 = 255 - nd
a = .5 * (1 - co(n1)): b = .5 * (1 - co(n2))
Re(nd) = a * Re(nd): Im(nd) = b * Im(nd)
Re(n0) = b * Re(n0): Im(n0) = a * Im(n0)
NEXT
END SUB

The code fragment in your question has not a valid Fortran syntax. It is VB and colon is used as statement separator

Fortran90 and up allow you to access a single array value given an index, and access a subarray given a range of indices separated by a colon.
Fortran = Beginning : End : Increment
MatLab = Beginning : Increment : End
There is a table at the bottom of page 5 in this doc that shows the Fortran and MatLab equivalents.

Related

Simplify symbolic expression in matlab and get only the coeeficents

I'm looking for finding the coefficients from the Taylor's series in Matlab. The way that I'm doing is:
% Declare symbolic expression and function:
syms x;
f = exp(x);
% Calculate the taylor expansions in a concrete point:
T = taylor(f, x, 0.5);
% And finally I simplify the expression:
coefs = simplify(T)
But the returned expression is:
Yes, the expression it's simplified, but actually what I want is:
Where each term is multiplied by his coefficient. How can I this? Options suchs as simplify(f, x, 0.5, 10, where 10 refers to the simplification step,doesn't work in my case. Also I've been seeing this question and the problem is the same:
How get to simplify a symbolic and numeric mixed expression in Matlab
Depending on how many digits you want and in what exact format you want the result, here is an example:
>> c = double(coeffs(T))
c =
Columns 1 through 4
0.999966624859531 1.000395979357109 0.498051217190664 0.171741799031263
Columns 5 through 6
0.034348359806253 0.013739343922501
>> digits 15
>> x.^(0:numel(c)-1) * sym(c,'d').'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531
EDIT
Note that you could also use vpa( ) instead of converting to double first:
>> c = coeffs(T)
c =
[ (2329*exp(1/2))/3840, (233*exp(1/2))/384, (29*exp(1/2))/96, (5*exp(1/2))/48, exp(1/2)/48, exp(1/2)/120]
>> x.^(0:numel(c)-1) * vpa(c).'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531

IndexError: index 1 is out of bounds for axis 0 with size 1 in python 3.7

I am trying to plot Ph and Pu for the time range of 0 to 300 with time step of 0.01. But, while running this code, it is showing the error as stated in the title of this question. Please give your suggestions for the code below
Pu = (np.zeros((1,Nt)))
Ph = (np.zeros((1,Nt)))
dec = 0.01 *2 * np.pi
Pu[1] = 1
for i in range(2, Nt):
Pu[i] = dt * (-(dec) * Pu[i-1] + Pu[i-1])
return Pu
Ph[1] = 0
for i in range(2, Nt):
Ph[i] = dt * ((dec) * Pu[i-1] - kappa * 1e-12 * Ph[i-1] + Ph[i-1])
return Ph
**Statement which is showing error:
Pu[1] = 1
IndexError: index 1 is out of bounds for axis 0 with size 1
I see several mistakes here:
First, you will want to remove the parenthesis here:
Pu = (np.zeros((1,Nt)))
creates a tuple containing a numpy array, whereas:
Pu = np.zeros((1,Nt))
simply creates the numpy array you need.
Then, your array is of size (1,Nt) so in the for loop you should do:
Pu[0,i]=..., same goes for the other arrays.
Finally be careful, arrays in python starts at index 0 so the first element is Pu[0,0] and the last is Pu[0,Nt-1].
In addition, you should be just fine with Pu=np.zeros(Nt) it seems you do not require a 2D array here (and it you simply access its elements with P[i]).

problems with basic arithmetic in a function (Matlab)

I have defined a really basic function in matlab. It takes no input and returns an array of 10 floating point numbers.
The problem I have is that when I run the function to return the array I want I get incorrect values, however when I substitute in a value and simply print out the value from within the function I get the correct answer?!
I've posted samples from the code below:
% Calculate the terms in our expression
FirstTerm = sin(Alpha)*(atan(x+d)-atan(x-d));
SecondTerm = cos(Alpha)*0.5*log(((x+d).^2+h.^2)/((x-d).^2+h.^2));
% Combine and return result
Result = 2 * (FirstTerm - SecondTerm)
FirstTermTemp = sin(Alpha)*(atan(-8+d)-atan(-8-d));
SecondTermTemp = cos(Alpha)*0.5*log(((-8+d).^2+h.^2)/((-8-d).^2+h.^2));
ResultTemp = 2 * (FirstTermTemp - SecondTermTemp)
The array I want to calculate for starts at -8 so the results should match. Does anyone have any idea why they wouldn't?
Cheers
Jack
You have left off a . before your /
% //Calculate the terms in our expression
FirstTerm = sin(Alpha)*(atan(x+d)-atan(x-d));
SecondTerm = cos(Alpha)*0.5*log(((x+d).^2+h.^2)./((x-d).^2+h.^2));
% //Combine and return result
Result = 2 * (FirstTerm - SecondTerm)
Result =
Columns 1 through 7:
0.097944 0.133866 0.208270 0.425797 0.692904 -0.140347 -0.124798
Columns 8 and 9:
-0.095581 -0.076166

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

Advice in Understanding this Key generating Algorith for Blowfish Algorithm

The code is for MATLAB and i want to understand what exactly they are trying to do. I m new to matlab so need some advice. Please Help
function [key] = keyGen(n)
n = n*8;
% n = 2048*2048*16;
% n = 24 * 24 * 8;
bin_x = zeros(n,1,'uint8');
r = 3.9999998;
bin_x_N_Minus_1 = 0.300001;
x_N = 0;
tic
for ind = 2 : n
x_N = 1 - 2* bin_x_N_Minus_1 * bin_x_N_Minus_1;
if (x_N > 0.0)
bin_x(ind-1) = 1;
end
bin_x_N_Minus_1 = x_N;
end
toc
%save bin_sec bin_x;
t = uint8(0);
key = zeros(n/8,1,'uint8');
for ind1 = 1 : n/8
for ind2 = 1 : 8
key(ind1) = key(ind1) + bin_x(ind2*ind1)* 2 ^ (ind2-1);
end
end
The parameter n is the number of bytes in the key. n*8 is to convert that into a number of bits. bin_x is used to store the binary representation of the key. bin_x_N_Minus_1 is the value which we use to calculate the next bit.
In the first for loop, we loop through the bits in the key (the first bit is always a 0). We calculate x_N using that formula (bin_x_N_Minus_1 is the previous value of x_N). If x_N is positive, the corresponding bit in the key is 1, otherwise it is a zero.
tic and toc are used to time how long this for loop takes.
The second for loop converts the bits of the key into bytes, and stores them in output array, key. The Kth entry in key is the 8 bit number represented by taking every (N/8)th entry in bin_x, starting from K.
The variables r and t are unused.