Incorrect number of arguments in IDL procedure - idl-programming-language

There is a short script that computes a histogram of the input data. data, dmin, dmax, bin are inputs and defined before calling the script; h, x, xmean are outputs.
I checked that histo.pro is in the right location..
Calling the script causes error (in idl85p):
IDL> .r histo.pro
IDL> histo, data, dmin, dmax, bin, h, x, xmean
% HISTO: Incorrect number of arguments.
% Execution halted at: $MAIN$
I would appreciate some help with it. Thanks
The script:
PRO histo, data, dmin, dmax, bin, h, x, xmean
on_error, 2
; compute histogram
h = histogram(float(data), BINSIZE = float(bin), MIN = float(dmin), $
MAX = float(dmax), REVERSE_INDICES = r)
; compute center of each bin
range = float(dmax - dmin) & nbin = long(range/bin) + 1
x = findgen(nbin) * bin + dmin + bin/2.
; compute mean data value for each bin
n_el = n_elements(h) & xmean = fltarr(n_el)
for n = 0L, n_el - 1 do begin
lo = r[n] & up = r[n+1] - 1
if lo lt up then $
xmean[n] = mean(data[r[lo:up]]) else xmean[n] = x[n]
endfor
return
end

Related

Double integration with a differentiation inside in R

I need to integrate the following function where there is a differentiation term inside. Unfortunately, that term is not easily differentiable.
Is this possible to do something like numerical integration to evaluate this in R?
You can assume 30,50,0.5,1,50,30 for l, tau, a, b, F and P respectively.
UPDATE: What I tried
InnerFunc4 <- function(t,x){digamma(gamma(a*t*(LF-LP)*b)/gamma(a*t))*(x-t)}
InnerIntegral4 <- Vectorize(function(x) { integrate(InnerFunc4, 1, x, x = x)$value})
integrate(InnerIntegral4, 30, 80)$value
It shows the following error:
Error in integrate(InnerFunc4, 1, x, x = x) : non-finite function value
UPDATE2:
InnerFunc4 <- function(t,L){digamma(gamma(a*t*(LF-LP)*b)/gamma(a*t))*(L-t)}
t_lower_bound = 0
t_upper_bound = 30
L_lower_bound = 30
L_upper_bound = 80
step_size = 0.5
integral = 0
t <- t_lower_bound + 0.5*step_size
while (t < t_upper_bound){
L = L_lower_bound + 0.5*step_size
while (L < L_upper_bound){
volume = InnerFunc4(t,L)*step_size**2
integral = integral + volume
L = L + step_size
}
t = t + step_size
}
Since It seems that your problem is only the derivative, you can get rid of it by means of partial integration:
Edit
Not applicable solution for lower integration bound 0.

Julia - MethodError: no method matching current_axis(::Nothing)

I am trying to test a linear approximation function and I am getting the error "no method matching current_axis(::Nothing)".
Here is my linear approximation function:
function linear_approx(A,b,c,p0)
p0 = [i for i in p0]
y(p) = p'*A*p .+ b'*p .+ c .-1
e = y(p0)
d = 2*A*p0 + b
(; d, e)
end
Here is the function that attempts to plot and throws an exception. I also included that value of the parameter when I tried to call it:
pts = [(1,1), (3,2), (4,4)]
function visualize_approx(pts)
# Use this function to inspect your solution, and
# ensure that the three points lie on one of
# the level-sets of your quadratic approximation.
(; A, b, c) = constant_curvature_approx(pts)
min_val = Inf
max_val = -Inf
for pt in pts
(; d, e) = linear_approx(A,b,c,pt)
P = LinRange(pt[1] - 0.2, pt[1]+0.2, 100)
Q = linear_segment(pt, d, e, P)
# the error arises here
plot!(P, Q)
plot!([pt[1]], [pt[2]])
end
delta = max_val - min_val
min_val -= 0.25*delta
max_val += 0.25*delta
X = Y = LinRange(min_val,max_val, 100)
Z = zeros(100,100)
for i = 1:100
for j = 1:100
pt = [X[i]; Y[j]]
Z[i,j] = pt'*A*pt + pt'*b + c
end
end
contour(X,Y,Z,levels=[-1,0,1,2,3])
for pt in pts
plot!([pt[1]], [pt[2]])
end
current_figure()
end
Does anyone know why this error arises?
plot! modifies a previously created plot object. It seems like you did not create a plot before calling it. This is why you get the error. Use plot when creating the plot and plot! when modifying it.

Comparing SAS and R results after resolving a system of differential equations

I my main objectif is to obtain the same results on SAS and on R. Somethimes and depending on the case, it is very easy. Otherwise it is difficult, specially when we want to compute something more complicated than the usual.
So, in ored to understand my case, I have the following differential equation system :
y' = z
z' = b* y'+c*y
Let :
b = - 2 , c = - 4, y(0) = 0 and z(0) = 1
In order to resolve this system, in SAS we use the command PROC MODEL :
data t;
do time=0 to 40;
output;
end;
run;
proc model data=t ;
dependent y 0 z 1;
parm b -2 c -4;
dert.y = z;
dert.z = b * dert.y + c * y;
solve y z / dynamic solveprint out=out1;
run;
In R, we could write the following solution using the lsoda function of the deSolve package:
library(deSolve)
b <- -2;
c <- -4;
rigidode <- function(t, y, parms) {
with(as.list(y), {
dert.y <- z
dert.z <- b * dert.y + c * y
list(c(dert.y, dert.z))
})
}
yini <- c(y = 0, z = 1)
times <- seq(from=0,to=40,by=1)
out_ode <- ode (times = times, y = yini, func = rigidode, parms = NULL)
out_lsoda <- lsoda (times = times, y = yini, func = rigidode, parms = NULL)
Here are the results :
SAS
R
For time t=0,..,10 , we obtain similar results. But for t=10,...,40, we start to have differences. For me, these differences are important.
In order to correct these differences, I fixed on R the error truncation term on 1E-9 in stead of 1E-6. I also verified if the numerical integration methods and the hypothesis used by default are the same.
Do you have any idea how to deal with this problem?
Sincerely yours,
Mily

Octave - Mark zero crossings with an red X mark

Hi have made this code to plot a function.
I need to mark with an red X all the crossings between x = 0 and the blue wave line in the graph.
I have made some tries but with '-xr' in the plot function but it places X marks out of the crossings.
Anyone knows how to do it. Many thanks.
Code:
% entrada
a = input('Introduza o valor de a: ');
% ficheiro fonte para a função
raizes;
% chamada à função
x = 0:.1:50;
or = x;
or(:) = 0;
h = #(x) cos(x);
g = #(x) exp(a*x)-1;
f = #(x) h(x) - g(x);
zeros = fzero(f,0);
plot(x,f(x));
hold on
plot(zeros,f(zeros),'-xr')
hold off
Graph (it only marks one zero, i need all the zero crossings):
As mentioned in the comments above, you need to look for the zeros of your function before you can plot them. You can do this mathematically (in this case set f(x) = g(x) and solve for x) or you can do this analytically with something like fsolve.
If you read the documentation for fsolve, you will see that it searches for the zero closest to the provided x0 if passed a scalar or the first zero if passed an interval. What we can do for a quick attempt at a solution is to pass our x values into fsolve as initial guesses and filter out the unique values.
% Set up sample data
a = .05;
x = 0:.1:50;
% Set up equations
h = #(x) cos(x);
g = #(x) exp(a*x)-1;
f = #(x) h(x) - g(x);
% Find zeros of f(x)
crossingpoints = zeros(length(x), 1); % Initialize array
for ii = 1:length(x) % Use x data points as guesses for fzero
try
crossingpoints(ii) = fzero(f, x(ii)); % Find zero closest to guess
end
end
crossingpoints(crossingpoints < 0) = []; % Throw out zeros where x < 0
% Find unique zeros
tol = 10^-8;
crossingpoints = sort(crossingpoints(:)); % Sort data for easier diff
temp = false(size(crossingpoints)); % Initialize testing array
% Find where the difference between 'zeros' is less than or equal to the
% tolerance and throw them out
temp(1:end-1) = abs(diff(crossingpoints)) <= tol;
crossingpoints(temp) = [];
% Sometimes catches beginning of the data set, filter it out if this happens
if abs(f(crossingpoints(1))) >= (0 + tol)
crossingpoints(1) = [];
end
% Plot data
plot(x, f(x))
hold on
plot(crossingpoints, f(crossingpoints), 'rx')
hold off
grid on
axis([0 20 -2 2]);
Which gives us the following:
Note that due to errors arising from floating point arithmetic we have to utilize a tolerance to filter our zeros rather than utilizing a function like unique.

idl/gdl ERROR: function not found or scalar subscript out of range

i try to solve this problem with my code. When i compile i have the follow error message:
% POINCARE: Ambiguous: POINCARE: Function not found: XT or: POINCARE: Scalar subscript out of range [>].e
% Execution halted at: POINCARE 38 poincare.pro
% $MAIN$
It's very simple:
1) i OPEN THE FILE AND COUNT THE NUMBER OF ROWS AND COLUMNS,
2) save the fle in a matrix of ROWSxCOLUMNS,
3) take the rows that i want and save them as vectors,
Now i want to modify the columns as follow:
A) translate each element of first and second column (x and y) by a costant factor (xc, yc ....)
B) apply some manipulation of each new element of this two new columns (xn ,yn ...)
C) if the value pyn is greater than 0. then save the rows with the four value of xn ,pxn.
Here the code:
pro poincare
file = "orbitm.txt"
rows =File_Lines(file) ; per le righe
openr,lun,file,/Get_lun ; per le colonne
line=""
readf,lun,line
cols = n_elements(StrSplit(line, /RegEx, /extract))
openr,1,"orbitm.txt"
data = dblarr(cols,rows)
readf,1,data
close,1
x = data(0,*) ; colonne e righe
y = data(1,*)
px = data(2,*)
py = data(3,*)
mu =0.001
xc = 0.5-mu
yc = 0.5*sqrt(3.)
openw,3,"section.txt"
for i=1, rows-2 do begin
xt = x(i)-xc
yt = y(i)-yc
pxt = px(i)-yc
pyt = py(i)+xc
tau = y(i)/(y(i)-y(i+1))
xn = xt(i) + (xt(i+1)-xt(i))*tau
yn = yt(i) + (yt(i+1)-yt(i))*tau
pxn = pxt(i) + (pxt(i+1)-pxt(i))*tau
pyn = pyt(i) + (pyt(i+1)-pyt(i))*tau
if (pyt(i) GT 0.) then begin
printf,3, xt(i), pxt(i)
endif
endfor
close,3
end
I attach also the first rows of my input orbitm.txt:
0.73634 0.66957 0.66062 -0.73503
0.86769 0.54316 0.51413 -0.82823
0.82106 0.66553 0.60353 -0.74436
0.59526 0.88356 0.79569 -0.52813
0.28631 1.0193 0.92796 -0.24641
-0.29229E-02 1.0458 0.96862 0.21874E-01
-0.21583 1.0090 0.95142 0.22650
-0.33994 0.96091 0.92099 0.35144
-0.38121 0.93413 0.90831 0.39745
-0.34462 0.93959 0.92534 0.36561
-0.22744 0.96833 0.96431 0.25054
-0.24560E-01 0.99010 0.99480 0.45173E-01
0.25324 0.95506 0.96459 -0.24000
0.55393 0.81943 0.82584 -0.54830
0.78756 0.61644 0.61023 -0.77367
0.88695 0.53076 0.50350 -0.82814
I can see a few issues that are immediately obvious. The first is that you define the variables XT, YT, PXT, and PYT inside your FOR loop as scalars. Shortly after, you try to index them as if they are arrays with multiple elements. Either your definition for these variables needs to change, or you need to change your definition of XN, YN, PXN, and PYN. Otherwise, this will not work as written. I have attached a modified version of your code with some suggestions and comments included.
pro poincare
file = "orbitm.txt"
rows =File_Lines(file) ; per le righe
openr,lun,file,/Get_lun ; per le colonne
line=""
readf,lun,line
cols = n_elements(StrSplit(line, /RegEx, /extract))
free_lun,lun ;; need to close this LUN
;; define data array
data = dblarr(cols,rows)
;;openr,1,"orbitm.txt"
;;readf,1,data
;; data = dblarr(cols,rows)
;;close,1
openr,lun,"orbitm.txt",/get_lun
readf,lun,data
free_lun,lun ;; close this LUN
;;x = data(0,*) ; colonne e righe
;;y = data(1,*)
;;px = data(2,*)
;;py = data(3,*)
x = data[0,*] ;; use []'s instead of ()'s in IDL
y = data[1,*]
px = data[2,*]
py = data[3,*]
mu = 0.001
xc = 0.5 - mu ;; currently a scalar
yc = 0.5*sqrt(3.) ;; currently a scalar
;; Perhaps you want to define XT, YT, PXT, and PYT as:
;; xt = x - xc[0]
;; yt = y - yc[0]
;; pxt = px - yc[0]
;; pyt = py + xc[0]
;; Then you could index these inside the FOR loop and
;; remove their definitions therein.
;;openw,3,"section.txt"
openw,lun,"section.txt",/get_lun
for i=1L, rows[0] - 2L do begin
xt = x[i] - xc ;; currently a scalar
yt = y[i] - yc ;; currently a scalar
pxt = px[i] - yc ;; currently a scalar
pyt = py[i] + xc ;; currently a scalar
tau = y[i]/(y[i] - y[i+1]) ;; currently a scalar
;; In the following you are trying to index XT, YT, PXT, and PYT but
;; each are scalars, not arrays!
xn = xt[i] + (xt[i+1] - xt[i])*tau
yn = yt[i] + (yt[i+1] - yt[i])*tau
pxn = pxt[i] + (pxt[i+1] - pxt[i])*tau
pyn = pyt[i] + (pyt[i+1] - pyt[i])*tau
if (pyt[i] GT 0.) then begin
printf,lun, xt[i], pxt[i]
endif
endfor
free_lun,lun ;; close this LUN
;;close,3
;; Return
return
end
General IDL Notes: You should use []'s instead of ()'s to index arrays to avoid confusion with functions. It is generally better to let IDL define a logical unit number (LUN) and then free the LUN than use CLOSE.

Resources