Compile error of the .f source code in gfortran; reading a text file into 2D array - fortran90

I have a problem with the following source. When it is compiled in gfortran, it does not work properly and then two error message showed up.
How do I solve this problem?
Any comment would be very helpful.
Thanks in advance.
program driver
integer i,ln,n,e,count,x,a,b,total
character driverid*12,var*12,ch*12
parameter (n=720321)
c parameter (n=55062)
dimension var(n),a(n),b(n)
write(*,*) 'input run id(text)'
read(*,55) driverid
55 format(a)
ln=index(driverid,' ')-1
open(6,file=driverid(1:ln)//'.out',form='formatted'
+,status='unknown')
open(1,file=driverid(1:ln)//'.txt',status='old')
do i=1,2
read(1,*)
end do
read(1,*) (var(i),i=1,n)
close(1)
total=0
count=1
do i=1,n
b(i)=0
read(var(i),*,iostat=e) x
if (e .eq. 0) then
a(count)=x
count=count+1
else
ln=index(var(i),' ')-1
if (var(i)(ln-1:ln-1) .eq. 'r') then
var(i)=var(i)(1:ln-2)
else
var(i)=var(i)(1:ln-1)
end if
read(var(i),'(i)') b(count-1)
end if
end do
do i=1,count
total=total+1+b(i)
end do
do i=1,total
write(6,'(10i)') (a(i),j=1,b(i))
end do
close(6)
end
Error message is following as
$ gfortran driver.f
driver.f:43.23:
read(var(i),'(i)') b(count-1)
1
Error: Nonnegative width required in format string at (1)
driver.f:53.20:
write(6,'(10i)') (a(i),j=1,b(i))
1
Error: Nonnegative width required in format string at (1)

When specifying a format for an integer, you must specify the field width. If you want to have it flexible, you can set it to zero:
write(6, "(10I0)") ...

Related

Lsode throwing INTDY-- T (=R1) ILLEGAL and invalid input detected

I have my function:
function [result] = my_func(x,y)
result = y^2*(1-3*x)-3*y;
endfunction
Also, my vector with Ts, my function address and my initial variable x_0
load file_with_ts
# Add my limits as I also want to calculate those
# (all values in file_with_ts are within those limits.)
t_points = [-1, file_with_ts, 2]
myfunc = str2func("my_func")
x_0 = 0.9142
I am trying to execute the following line:
lsode_d1 = lsode(myfunc, x_0, t_points)
And expecting a result, but getting the following error:
INTDY-- T (=R1) ILLEGAL
In above message, R1 = 0.7987082301475D+00
T NOT IN INTERVAL TCUR - HU (= R1) TO TCUR (=R2)
In above, R1 = 0.8091168896311D+00 R2 = 0.8280400838323D+00
LSODE-- TROUBLE FROM INTDY. ITASK = I1, TOUT = R1
In above message, I1 = 1
In above message, R1 = 0.7987082301475D+00
error: lsode: invalid input detected (see printed message)
error: called from
main at line 20 column 10
Also, the variable sizes are:
x_0 -> 1x1
t_points -> 1x153
myfunc -> 1x1
I tried transposing the t_points vector
using #my_func instead of the str2func function
I tried adding multiple variables as the starting point (instead of x_0 I entered [x_0; x_1])
Tried changing my function header from my_func(x, y) to my_func(y, x)
Read the documentation and confirmed that my_func allows x to be a vector and returns a vector (whenever x is a vector).
EDIT: T points is the following 1x153 matrix (with -1 and 2 added to the beggining and the end respectively):
-4.9451e-01
-4.9139e-01
-4.7649e-01
-4.8026e-01
-4.6177e-01
-4.5412e-01
-4.4789e-01
-4.2746e-01
-4.1859e-01
-4.0983e-01
-4.0667e-01
-3.8436e-01
-3.7825e-01
-3.7150e-01
-3.5989e-01
-3.5131e-01
-3.4875e-01
-3.3143e-01
-3.2416e-01
-3.1490e-01
-3.0578e-01
-2.9267e-01
-2.9001e-01
-2.6518e-01
-2.5740e-01
-2.5010e-01
-2.4017e-01
-2.3399e-01
-2.1491e-01
-2.1067e-01
-2.0357e-01
-1.8324e-01
-1.8112e-01
-1.7295e-01
-1.6147e-01
-1.5424e-01
-1.4560e-01
-1.1737e-01
-1.1172e-01
-1.0846e-01
-1.0629e-01
-9.4327e-02
-8.0883e-02
-6.6043e-02
-6.6660e-02
-6.1649e-02
-4.7245e-02
-2.8332e-02
-1.8043e-02
-7.7416e-03
-6.5142e-04
1.0918e-02
1.7619e-02
3.4310e-02
3.3192e-02
5.2275e-02
5.5756e-02
6.8326e-02
8.2764e-02
9.5195e-02
9.4412e-02
1.1630e-01
1.2330e-01
1.2966e-01
1.3902e-01
1.4891e-01
1.5848e-01
1.7012e-01
1.8026e-01
1.9413e-01
2.0763e-01
2.1233e-01
2.1895e-01
2.3313e-01
2.4092e-01
2.4485e-01
2.6475e-01
2.7154e-01
2.8068e-01
2.9258e-01
3.0131e-01
3.0529e-01
3.1919e-01
3.2927e-01
3.3734e-01
3.5841e-01
3.5562e-01
3.6758e-01
3.7644e-01
3.8413e-01
3.9904e-01
4.0863e-01
4.2765e-01
4.2875e-01
4.3468e-01
4.5802e-01
4.6617e-01
4.6885e-01
4.7247e-01
4.8778e-01
4.9922e-01
5.1138e-01
5.1869e-01
5.3222e-01
5.4196e-01
5.4375e-01
5.5526e-01
5.6629e-01
5.7746e-01
5.8840e-01
6.0006e-01
5.9485e-01
6.1771e-01
6.3621e-01
6.3467e-01
6.5467e-01
6.6175e-01
6.6985e-01
6.8091e-01
6.8217e-01
6.9958e-01
7.1802e-01
7.2049e-01
7.3021e-01
7.3633e-01
7.4985e-01
7.6116e-01
7.7213e-01
7.7814e-01
7.8882e-01
8.1012e-01
7.9871e-01
8.3115e-01
8.3169e-01
8.4500e-01
8.4168e-01
8.5705e-01
8.6861e-01
8.8211e-01
8.8165e-01
9.0236e-01
9.0394e-01
9.2033e-01
9.3326e-01
9.4164e-01
9.5541e-01
9.6503e-01
9.6675e-01
9.8129e-01
9.8528e-01
9.9339e-01
Credits to Lutz Lehmann and PierU.
The problem lied in the array t_points not being a monotonous array. Adding a sort(t_points) before doing any calculations fixed the error.

"Undefined operator '*' for input arguments of type 'function_handle'." for matlab

I want to code a FEM programme and here is my code
N1=2; %横向分解成2个元素
N2=2; %纵向分解成2个元素
N=2*N1*N2;
%三角元
top=1;
bottom=0;
left=0;
right=1;
h1=(right-left)/N1;
h2=(top-bottom)/N2;
T=zeros(3,2*N1*N2);%生成初始T矩阵
N10=N1+1;
N20=N2+1;
%先把每个element的行列转化为每个element对应的起始node的坐标,然和把坐标转化为index
for i=1:N2
for j=1:2*N1
if mod(j,2)==1
T(:,2*N1*(i-1)+j)=[(i-1)*N10+ceil(j/2);i*N10+ceil(j/2);(i-1)*N10+ceil(j/2)+1];
else
T(:,2*N1*(i-1)+j)=[(i-1)*N10+j/2+1;i*N10+j/2;i*N10+j/2+1];
end
end
end
P=zeros(2,(N1+1)*(N2+1));
for i=1:N1+1
for j=1:N2+1
P(:,(i-1)*(N1+1)+j)=[left+(i-1)*h1;bottom+(j-1)*h2];
end
end
N1_1= 2*N1;
N2_1= 2*N1;
N_1= 2*N1_1*N2_1;
%三角元
h1_1=(right-left)/N1_1;
h2_1=(top-bottom)/N2_1;
N10_1=N1_1+1;
N20_1=N2_1+1;
P_b=zeros(2,(N1_1+1)*(N2_1+1));
for i=1:N1_1+1
for j=1:N2_1+1
P_b(:,(i-1)*(N1_1+1)+j)=[left+(i-1)*h1_1;bottom+(j-1)*h2_1];
end
end
boundarynodes=zeros(2,N2_1+1+N1_1+N2_1+N1_1-1);
for j=1:2*N2_1+2*N1_1
boundarynodes(1,j)=-1;
end
for i=1:N1_1+1
boundarynodes(2,i)=(i-1)*(N1_1+1)+1;
end
for i=N1_1+2:N1_1+1+N2_1
boundarynodes(2,i)=(N1_1+1-1)*(N1_1+1)+i-(N1_1);
end
for i=N1_1+1+N2_1+N1_1:-1:N1_1+N2_1+2
boundarynodes(2,i)=((-i+2*N1_1+N2_1+2)-1)*(N1_1+1)+N1_1+1;
end
for i=N1_1+N2_1+N2_1+N1_1:-1:N1_1+2+N2_1+N1_1
boundarynodes(2,i)=-i+2*N1_1+2*N2_1+2;
end
%建立坐标到index函数
T_b=zeros(6,2*N1*N2);
c2i=#(i,j) (i-1)*(N1_1+1)+j;
for i=1:N1
for j=1:2*N2
if mod(j,2)==1
i_0=2*i-1;
j_0=2*ceil(j/2)-1;
T_b(:,2*N1*(i-1)+j)=[c2i(i_0,j_0);c2i(i_0+2,j_0);c2i(i_0,j_0+2);c2i(i_0+1,j_0);c2i(i_0+1,j_0+1);c2i(i_0,j_0+1)];
else
i_0=2*i-1;
j_0=2*(j/2+1)-1;
T_b(:,2*N1*(i-1)+j)=[c2i(i_0,j_0);c2i(i_0+2,j_0-2);c2i(i_0+2,j_0);c2i(i_0+1,j_0-1);c2i(i_0+2,j_0-1);c2i(i_0+1,j_0)];
end
end
end
A=sparse((N1_1+1)*(N2_1+1),(N1_1+1)*(N2_1+1));
for n=1:N
y_n2=P_b(2,T_b(2,n));
y_n3=P_b(2,T_b(3,n));
y_n1=P_b(2,T_b(1,n));
y_n4=P_b(2,T_b(4,n));
y_n5=P_b(2,T_b(5,n));
y_n6=P_b(2,T_b(6,n));
x_n3=P_b(1,T_b(3,n));
x_n2=P_b(1,T_b(2,n));
x_n1=P_b(1,T_b(1,n));
x_n4=P_b(1,T_b(4,n));
x_n5=P_b(1,T_b(5,n));
x_n6=P_b(1,T_b(6,n));
Y_n2=P(2,T(2,n));
Y_n3=P(2,T(3,n));
Y_n1=P(2,T(1,n));
X_n3=P(1,T(3,n));
X_n2=P(1,T(2,n));
X_n1=P(1,T(1,n));
xmin=X_n1;
xmax=xmin+h1;
J_n=(X_n2-X_n1)*(Y_n3-Y_n1)-(X_n3-X_n1)*(Y_n2-Y_n1);
x_hat=#(x,y) ((Y_n3-Y_n1)*(x-X_n1)-(X_n3-X_n1)*(y-Y_n1))/J_n;
y_hat=#(x,y) ((X_n2-X_n1)*(y-Y_n1)-(Y_n2-Y_n1)*(x-X_n1))/J_n;
if mod(n,2)==1
ymin= Y_n1;
ymax=#(x) Y_n3+((Y_n3-Y_n2)/(X_n3-X_n2))*(x-X_n3);
else
ymin=#(x) Y_n2+((Y_n2-Y_n1)/(X_n2-X_n1))*(x-x_n2);
ymax= Y_n1;
end
for i=1:6
for j=1:6
fun=#(x,y)...
(p_i_x(x_hat(x,y),y_hat(x,y),i))*((Y_n3-Y_n1)/J_n)+...
(p_i_y(x_hat(x,y),y_hat,i))*((Y_n1-Y_n2)/J_n)*...
(p_i_x(x_hat(x,y),y_hat(x,y),j)*((Y_n3-Y_n1)/J_n)+...
(p_i_y(x_hat(x,y),y_hat(x,y),j))*((Y_n1-Y_n2)/J_n))+...
((p_i_x(x_hat(x,y),y_hat(x,y),i))*((X_n1-X_n3)/J_n)+...
(p_i_y(x_hat(x,y),y_hat(x,y),i))*((X_n2-X_n1)/J_n))*...
((p_i_x(x_hat(x,y),y_hat(x,y),j))*((X_n1-X_n3)/J_n)+...
(p_i_y(x_hat(x,y),y_hat(x,y),j))*(X_n2-X_n1)/J_n);
r=integral2(fun,xmin,xmax,ymin,ymax);
A(T_b(j,n),T_b(i,n))=A(T_b(j,n),T_b(i,n))+r;
end
end
end
please ignore the chinese comment!
the above main programme also use the following two programmes
function r=p_i_x(x,y,i)
i_x=[4*x+4*y-3,4*x-1,0,-8*x-4*y+4,4*y,-4*y];
r=i_x(i);
end
function r=p_i_y(x,y,i)
i_y=[4*x+4*y-3,0,4*y-1,-4*x,4*x,-8*y-4*x+4];
r=i_y(:,i);
end
when I try to execute fun(0,1), "Undefined operator '*' for input arguments of type 'function_handle'." occurs I don't know how to fix this and other answers don't help, how to fix this?

Error on print_usage and fzero while Running Matlab Script in Octave

I am trying to run inputfile_calrel_example1 FERUM Matlab scripts from https://www.sigma-clermont.fr/en/ferum in Octave-5.1.0.0 but run into errors with respect to print_usage and fzero as follows:
error: Invalid call to fzero. Correct usage is:
-- fzero (FUN, X0)
-- fzero (FUN, X0, OPTIONS)
-- [X, FVAL, INFO, OUTPUT] = fzero (...)
error: called from
print_usage at line 91 column 5
fzero at line 133 column 5
drho0_dthetaf_integral at line 75 column 22
mod_corr_solve at line 99 column 54
form at line 90 column 58
ferum at line 129 column 33
>>
Looking through print_usage.m file reveals line 91 as follows:
error ("Octave:invalid-fun-call", msg);
while lines 78 to 92:
if (at_toplev)
error ("Octave:invalid-fun-call",
"Invalid call to %s. Correct usage is:\n\n%s\n%s",
name, usage_string, __additional_help_message__ ());
else
msg = sprintf ("Invalid call to %s. Correct usage is:\n\n%s",
name, usage_string);
## Ensure that the error doesn't end up with a newline, as that disables
## backtraces.
if (msg(end) == "\n")
msg(end) = " ";
endif
error ("Octave:invalid-fun-call", msg);
endif
and fzero lines 132 to 134 are as follows:
if (nargin < 2 | nargin > 3)
print_usage (mfilename ());
end
I would like to have hints as to how to resolve the above error messages.
Best regards
Aliyu Aziz
As stated in the comments, fzero was called with the following arguments.
drho0_dthetafi.mu = fzero( ...
'betadrho0_dthetaf' ...
, 0 ...
, optimset('fzero') ...
, dF_dthetafi.mu ...
, PHI2 ...
, F ...
, dPHI2_dZi ...
, dZi_dthetafi.mu ...
, dPHI2_drho0 ...
, detJ ...
, WIP ...
);
From the documentation (help fzero) you can see that the above call is not a valid fzero call:
-- fzero (FUN, X0, OPTIONS)
Find a zero of a univariate function
FUN is a function handle, inline function, or string containing the
name of the function to evaluate.
X0 should be a two-element vector specifying two points which
bracket a zero. If X0 is a single scalar then several nearby and distant
values are probed in an attempt to obtain a valid bracketing. If this is not
successful, the function fails.
OPTIONS is a structure specifying additional options.
To initialize an options structure with default values for 'fzero'
use 'options = optimset ("fzero")'.
So as you see, the extra arguments after 'optimset' trigger an error.
I'm assuming that the extra arguments were intended to be arguments to the betadrho0_dthetaf function. In general the function FUN expects a single argument (since it is univariate). If your betadrho0_dthetaf function expects a number of other parameters, then instead of using it in fzero via string, wrap it around an anonymous function handle which does only take a single argument, and uses your intended function internally to calculate the intended result, e.g.
drho0_dthetafi.mu = fzero( ...
#(x) betadrho0_dthetaf( ...
x ...
, dF_dthetafi.mu ...
, PHI2 ...
, F ...
, dPHI2_dZi ...
, dZi_dthetafi.mu ...
, dPHI2_drho0 ...
, detJ ...
, WIP ...
) ...
, 0 ...
, optimset('fzero') ...
);
or something along those lines, depending on how you would call that beta function.

Syntax error when running matlab code

I downloaded some matlab code, when trying to run a test file, it ends finding a syntax error, this one here:
parse error near line 151 of file /media/34GB/escola/efficientLBP/pixelwiseLBP.m
syntax error
>>>> [minLBP, ~]=sortNeighbours( binaryWord, weigthVec,...
^
The code it is:
else % if iChan==1 || isChanWiseRot
[minLBP, ~]=sortNeighbours( binaryWord, weigthVec,...
iCircShiftMinLBP(iRow, iCol) );
What is wrong here? Thanks for the time.
P.S: EDITED
Here is the extra code requested:
for iRow=( filtDimsR(1)+1 ):( padImgSize(1)-filtDimsR(1) )
for iCol=( filtDimsR(2)+1 ):( padImgSize(2)-filtDimsR(2) )
subImg=chanImgPad(iRow+( -filtDimsR(1):filtDimsR(1) ),...
iCol+( -filtDimsR(2):filtDimsR(2) ));
% find differences between current pixel, and it's neighours
diffVec=sum(sum( filtR.*repmat(subImg,[1, 1, nNeigh]) ));
diffVec=roundnS(diffVec, nEps);
binaryWord=( diffVec(:)>=0 );
if isRotInv
if iChan==1 || isChanWiseRot % go through all posible binary
% word combination, finding minimal LBP
[minLBP, iCircShiftMinLBP(iRow, iCol)]=...
sortNeighbours(binaryWord, weigthVec);
else % if iChan==1 || isChanWiseRot
[minLBP, ~]=sortNeighbours( binaryWord, weigthVec,...
iCircShiftMinLBP(iRow, iCol) );
end % if iChan==1 || isChanWiseRot
else
minLBP=weigthVec*binaryWord;
end % if isRotInv
currChanLBP(iRow, iCol)=cast( minLBP, outClass); % convert to decimal.
end % for iCol=(1+filtDimsR(2)):(imgSize(2)-filtDimsR(2))
% Present waitbar- a bar with progress, time passed and time remaining
waitbarTimeRemaining(hWaitbar, hTicPixelwiseLBP,...
(( iRow-filtDimsR(1) )+nRows*(iChan-1))/(nClrChans*nRows));
end % for iRow=(1+filtDimsR(1)):(imgSize(1)-filtDimsR(1))

MATLAB: errorn in butter() command

I wrote the following function:
function [output_signal] = AddDirectivityError (bat_loc_index, butter_deg_vector, sound_matrix)
global chirp_initial_freq ;
global chirp_end_freq;
global sampling_rate;
global num_of_mics;
global sound_signal_length;
for (i=1 : num_of_mics)
normalized_co_freq = (chirp_initial_freq + chirp_end_freq)/ (1.6* sampling_rate);
A=sound_matrix ( i, : ) ;
peak_signal=max(A);
B=find(abs(A)>peak_signal/100);
if (butter_deg_vector(i)==0)
butter_deg_vector(i)=2;
end
[num, den] = butter(butter_deg_vector(i), normalized_co_freq, 'low');// HERE!!!
filtered_signal=filter(num,den, A );
output_signal(i, :)=filtered_signal;
end
This functions runs many-many times without any error. However, when I reach the line: [num, den] = butter ( butter_deg_vector(i), normalized_co_freq, 'low');
And the local variables are: i=3, butter_deg_vector(i)=1, normalized_co_freq=5.625000e-001
MATLAB prompts an error says:
??? Error using ==> buttap Expected N to be integer-valued.
"Error in ==> buttap at 15 validateattributes(n,{'numeric'},{'scalar','integer','positive'},'buttap','N');
Error in ==> butter at 70 [z,p,k] = buttap(n);"
I don't understand why this problem occurs especially in this iteration. Why does this function prompt an error especially in this case?
Try to change the code line for:
[num, den] = butter (round(butter_deg_vector(i)), normalized_co_freq, 'low');