sqrt function fortran 90 error - fortran90

I am trying to handle sqrt but somehow I cannot understand why when compiling this program, there goes a segmentation fault error. I have detected that it is because of the sqrt. But how can I manage to use sqrt in these type of formulations without mistaking?
SUBROUTINE constructImages(image,w,w_b,x_w)
USE cellConst
USE simParam, ONLY: xDim, yDim, obstX, obstY, obstR,L_max
USE D2Q9Const, ONLY: v
implicit none
integer, INTENT(INOUT):: image(yDim,xDim),w_b(yDim,xDim),w(yDim,xDim)
double precision, dimension(L_max,0:1), INTENT(INOUT):: x_w
integer:: x,y,i,cont_L
double precision::x2,y2
!Disk Shape
do x = 1, xDim
do y = 1, yDim
if (((x-obstX)**2.0d0 + (y-obstY)**2.0d0) <= (obstR**2.0d0) ) then
image(y,x) = wall
w(y,x) = wall
end if
end do
end do
do x = 1, xDim
do y = 3, yDim-2
do i= 1,8
if ((w(y,x) == fluid) .and. (w(y+v(i,1),x+v(i,0)) == wall)) then
w_b(y,x) = 2
end if
end do
end do
end do
do x = 1,xDim
do y = 3, yDim-2
if (w_b(y,x) == 2) then
w_b(y,x) = wall
w(y,x) = wall
image(y,x) = wall
end if
end do
end do
x_w = 0.0d0 !Lagrangian vector for boundary exact position
cont_L = 0
do x = 1, xDim
do y = 1, yDim
do i = 1, 8
if ((w(y+v(i,1),x+v(i,0)) == fluid) .and. (w_b(y,x) == wall)) then
cont_L = cont_L +1
. x_w(cont_L,0) = x2 - ((x-obstX)^2.0d0 + (y-obstY)^2.0d0)^0.5d0 - obstR
. x_w(cont_L,1) = y2 - ((x-obstX)^2.0d0 + (y-obstY)^2.0d0)^0.5d0 - obstR
! write(*,*) x2,y2
! write(*,*) x_w(cont_L,0),x_w(cont_L,1)
end if
end do
end do
end do
END SUBROUTINE constructImages
Please, let me know if more information is required,
sincerely,
Albert P
PD, the 3 integer eulerian meshes are 0 / 1 2D meshes where 1 is assigned to a wall, which is delimited by a rounded disk, w_b(y,x) are the boundary points, w(y,x) are the whole disk points and x_w I want to set a langrangian vector of the discretized exact position for the obstacle. Don't really need to understand this.

The problem was actually an array index which was under 1 in w, and there were multiple problems that somehow derived to that error when setting sqrt.
do x = 1, xDim
do y = 1, yDim
do i = 1, 8
if ((w(y,x) == fluid) .and. (w(y+v(i,1),x+v(i,0)) == wall))
.....
here I state that w goes >1, and then it gets out of the bounds.
I have solved the problem not letting the integers go below 1 and over yDim, and this with all the routines, and I have detected that problem with a check-bounce while executing the code.
Thank you all of you anyway. That was a tough one!!
Albert P

Related

Index in position __ exceeds array bounds

Having Trouble with this Error "Index in position 2 is invalid. Array indices must be positive integers or logical values." It only occurs when I enter certain values for the translation. Such as -25 x-direction and +50 in the y-direction. But it will work if I enter in any decimal values such as (12.75,-19.3). Please help me out.
Note: A is an imported image
if (delta_x == 0 && delta_y == 0) % Allows the Orginal Image to be displayed
figure(1)
image(0:ynew-1,0:xnew-1,A) % display a matrix as an image
axis equal
axis ij % put 0,0 in UL corner
axis tight
colormap(gray(L)) % use an 8-bit grayscale
xlabel('y')
ylabel('x')
title('Original Image')
return
end
%Translation Matrix 3x3
Trans = [1 0 delta_x; 0 1 delta_y; 0 0 1];
InverseTrans = (inv(Trans)); %Inverse Mapping Begins
%Allocate Space
E = zeros(xnew, ynew,k);
for i=1:xold
for j=1:yold
% matrix math assumes origin is (0,0) not (1,1)
vw=InverseTrans*[i; j; 1]; % vw will be [v; w; 1]
v = vw(1);
w = vw(2);
if (v < 0 || v >(xold-1)) % Testing for Out of Bounds
E(i,j) = 0;
elseif (w < 0 || w >(yold-1)) % Testing for out of Bounds
E(i,j) = 0;
else
%Takes the Real INTEGER Part
v_fix = fix(v);
w_fix = fix(w);
v_fix1 = v_fix +1;%Indexing
w_fix1 = w_fix +1;
%Change in the Values
dX = v - v_fix;
dY = w - w_fix;
%Indexing Purposes **This is where I'm getting my error**
D1(i,j) = A(v_fix1, w_fix1);
D2(i,j) = A(v_fix1 + 1, w_fix1);
D3(i,j) = A(v_fix1, w_fix1 + 1);
D4(i,j) = A(v_fix1 + 1, w_fix1 + 1);
end
end
end```

MATLAB: data points won't connect?

I only have about two weeks of experience with programming/Matlab, so I'm just a beginner. In my code I would like to plot mu as a function of alpha. When I display mu it shows the 10 values of mu for each value of alpha. However, when I plot the graph it gives the values of mu as seperate points. But I want the points to be connected with just one line. How can I solve this problem?
n=40; %number of months
p=0.23; %probability of success
num_of_simulations=100;
s=rng; x = rand(1,n)<p;
rng(s);
hold on;
for alpha=0.01:0.01:0.1;
for i=1:num_of_simulations
x = rand(1,n)<p;
S0=5000; %initial value
Y(1)=S0*alpha; %deposit
for k=1
if x(1,1)==1;
S(1, i)=S0+2*Y(1);
else
S(1, i)=S0-Y(1);
end
end
for k=2:n
Y(k)=S(k-1, i)*alpha;
if x(1,k)==1;
S(k, i)=S(k-1, i)+2*Y(k);
else
S(k, i)=S(k-1, i)-Y(k);
end
end
Sn(i)=S(n,i); %end value for each simulations
end
mu=mean(Sn);
disp(mu);
plot(alpha,mu);
end
The reason your points aren't connected is because you plot each point separately. If we take a different approach and take alpha = 0.01:0.01:0.1; out of the for loop definition and then change the for loop definition to for j=1:numel(alpha) we can still loop over every element of alpha. Now we need to change each use of alpha in the loop to alpha(j) so that we are referring to the current element of alpha and not every element. Following on from this we need to change mu to mu(j). What this means is that when the entire loop has finished we have all of the values of alpha and mu stored and 1 call to plot(alpha, mu) will plot the data with the points connected as in
This also enables us to remove hold on; too as we only plot once.
I've included the complete edited code here for you to see. The changes are minuscule and should make sense.
clear all
close all
n = 40; %number of months
p = 0.23; %probability of success
num_of_simulations = 100;
s = rng;
x = rand(1, n) < p;
rng(s);
alpha = 0.01:0.01:0.1;
for j = 1:numel(alpha)
for i = 1:num_of_simulations
x = rand(1, n) < p;
S0 = 5000; %initial value
Y(1) = S0*alpha(j); %deposit
for k = 1
if x(1, 1) == 1;
S(1, i) = S0 + 2*Y(1);
else
S(1, i) = S0 - Y(1);
end
end
for k = 2:n
Y(k) = S(k-1, i)*alpha(j);
if x(1, k) == 1;
S(k, i) = S(k-1, i) + 2*Y(k);
else
S(k, i) = S(k-1, i) - Y(k);
end
end
Sn(i) = S(n, i); %end value for each simulations
end
mu(j) = mean(Sn);
disp(mu(j));
end
plot(alpha, mu);

Find Specified Color In Each Frame Of Real-time Video

I am newbie in Matlab.
My Boss gave me a task and i stuck in last step.
the task is:
user should specify a rectangle in snapshot of real-time video and then i should detect the mean color of that rectangle in each frame of video.
first user specify the boundaries of rectangle in a snapshot of video with this code:
test = getsnapshot(vid);
imwrite(test,'mytest.png','png');
testsnap = imread('mytest.png');
Rectpos=getrect;
then I calculate mean degree of each R , G , B :
bbx=boundingboxPixels(testsnap,Rectpos(1),Rectpos(2),Rectpos(3),Rectpos(4));
rRect=mean(bbx(:,:,1));
gRect=mean(bbx(:,:,2));
bRect=mean(bbx(:,:,3));
where boundingboxPixels method is like this :
function pixel_vals = boundingboxPixels(img, x_init, y_init, x_width, y_width)
if x_init > size(img,2)
error('x_init lies outside the bounds of the image.'); end
if y_init > size(img,1)
error('y_init lies outside the bounds of the image.'); end
if y_init+y_width > size(img,1) || x_init+x_width > size(img,2) || ...
x_init < 1 || y_init < 1
warning([...
'Given rectangle partially falls outside image. ',...
'Resizing rectangle...']);
end
x_min = max(1, uint16(x_init));
y_min = max(1, uint16(y_init));
x_max = min(size(img,2), x_min+uint16(x_width));
y_max = min(size(img,1), y_min+uint16(y_width));
x_range = x_min : x_max;
y_range = y_min : y_max;
Upper = img( x_range, y_min , :);
Left = img( x_min, y_range, :);
Right = img( x_max, y_range, :);
Lower = img( x_range, y_max , :);
pixel_vals = [...
Upper
permute(Left, [2 1 3])
permute(Right, [2 1 3])
Lower];
end
then get the calculated Mean of RGB color with a threshold from each frame of video:
tv=getdata(vid,1);//vid is real-time video
r=tv(:,:,1,1);
g=tv(:,:,2,1);
b=tv(:,:,3,1);
redVal = (r >= rRect-thr) & (r <= rRect+thr);
greenVal = (g >= gRect-thr) & (g <= gRect+thr);
blueVal = (b >= bRect-thr) & (b <= bRect+thr);
Now How Should I use the redVal , greenVal , blueVal to detect this color?
as Steffen said , problem solved by adding & between arrays

Matlab - Failures of function to detect collisions between line segments and circle

Many questions exist already covering how to detect collisions between a line segment and a circle.
In my code, I am using Matlab's linecirc function, then comparing the intersection points it returns with the ends of my line segments, to check that the points are within the line (linecirc assumes an infinite line, which I don't have/want).
Copying and adding some sprintf calls to the linecirc function shows that it is calculating points as intended. These seem to be being lost by my function.
My code is below:
function cutCount = getCutCountHex(R_g, centre)
clf;
cutCount = 0;
% Generate a hex grid
Dg = R_g*2;
L_b = 62;
range = L_b*8;
dx = Dg*cosd(30);
dy = 3*R_g;
xMax = ceil(range/dx); yMax = ceil(range/dy);
d1 = #(xc, yc) [dx*xc dy*yc];
d2 = #(xc, yc) [dx*(xc+0.5) dy*(yc+0.5)];
centres = zeros((xMax*yMax),2);
count = 1;
for yc = 0:yMax-1
for xc = 0:xMax-1
centres(count,:) = d1(xc, yc);
count = count + 1;
centres(count, :) = d2(xc, yc);
count = count + 1;
end
end
for i=1:size(centres,1)
centres(i,:) = centres(i,:) - [xMax/2 * dx, yMax/2 * dy];
end
hold on
axis equal
% Get counter for intersected lines
[VertexX, VertexY] = voronoi(centres(:,1), centres(:,2));
numLines = size(VertexX, 2);
for lc = 1:numLines
segStartPt = [VertexX(1,lc) VertexY(1,lc)];
segEndPt = [VertexX(2,lc) VertexY(2,lc)];
slope = (segEndPt(2) - segStartPt(2))/(segEndPt(1) - segStartPt(1));
intercept = segEndPt(2) - (slope*segEndPt(1));
testSlope = isinf(slope);
if (testSlope(1)==1)
% Pass the x-axis intercept instead
intercept = segStartPt(1);
end
[xInterceptionPoints, yInterceptionPoints] = ...
linecirc(slope, intercept, centre(1), centre(2), L_b);
testArr = isnan(xInterceptionPoints);
if (testArr(1) == 0) % Line intersects. Line segment may not.
interceptionPoint1 = [xInterceptionPoints(1), yInterceptionPoints(1)];
interceptionPoint2 = [xInterceptionPoints(2), yInterceptionPoints(2)];
% Test if first intersection is on the line segment
p1OnSeg = onSeg(segStartPt, segEndPt, interceptionPoint1);
p2OnSeg = onSeg(segStartPt, segEndPt, interceptionPoint2);
if (p1OnSeg == 1)
cutCount = cutCount + 1;
scatter(interceptionPoint1(1), interceptionPoint1(2), 60, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'k');
end
% Test if second intersection point is on the line segment
if (interceptionPoint1(1) ~= interceptionPoint2(1) || interceptionPoint1(2) ~= interceptionPoint2(2)) % Don't double count touching points
if (p2OnSeg == 1)
cutCount = cutCount + 1;
scatter(interceptionPoint2(1), interceptionPoint2(2), 60, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'k');
end
end
end
end
% Plot circle
viscircles(centre, L_b, 'EdgeColor', 'b');
H = voronoi(centres(:,1), centres(:,2));
for i = 1:size(H)
set(H(i), 'Color', 'g');
end
end
function boolVal = onSeg(segStart, segEnd, testPoint)
bvX = isBetweenOrEq(segStart(1), segEnd(1), testPoint(1));
bvY = isBetweenOrEq(segStart(2), segEnd(2), testPoint(2));
if (bvX == 1 && bvY == 1)
boolVal = 1;
else
boolVal = 0;
end
end
function boolVal = isBetweenOrEq(end1, end2, test)
if ((test <= end1 && test >= end2) || (test >= end1 && test <= end2))
boolVal = 1;
else
boolVal = 0;
end
end
It creates a hexagonal grid, then calculates the number of crossings between a circle drawn with a fixed radius (62 in this case) and a specified centre.
The scatter calls show the locations that the function counts.
Implementing sprintf calls within the if(p1OnSeg == 1) block indicates that my function has chosen fictitious intersection points (although it then deals with them correctly)
if (interceptionPoint1(1) > -26 && interceptionPoint1(1) < -25)
sprintf('p1 = [%f, %f]. Vx = [%f, %f], Vy = [%f, %f].\nxint = [%f, %f], yint = [%f, %f]',...
interceptionPoint1(1), interceptionPoint1(2), VertexX(1,lc), VertexX(2,lc), VertexY(1,lc), VertexY(2,lc),...
xInterceptionPoints(1), xInterceptionPoints(2), yInterceptionPoints(1), yInterceptionPoints(2))
end
Outputs
p1 = [-25.980762, 0.000000]. Vx = [-25.980762, -25.980762], Vy = [-15.000000, 15.000000].
xint = [-25.980762, -25.980762], yint = [0.000000, 0.000000]
A picture shows the strange points.
Sorry for the very long question but - why are these being detected. They don't lie on the circle (displaying values within a mylinecirc function detects the intersections at around (-25, 55) and (-25, -55) or so (as an infinite line would expect).
Moving the circle can remove these points, but sometimes this leads to other problems with detection. What's the deal?
Edit: Rotating my grid pattern created by [Vx, Vy] = voronoi(...) and then removing points with very large values (ie those going close to infinity etc) appears to have fixed this problem. The removal of 'large' value points seems to be necessary to avoid NaN values appearing in 'slope' and 'intercept'. My guess is this is related to a possible slight inclination due to rotation, coupled with then overflow of the expected intercept.
Example code added is below. I also edited in Jan de Gier's code, but that made no difference to the problem and so is not changed in the question code.
%Rotate slightly
RotAngle = 8;
RotMat = [cosd(RotAngle), -sind(RotAngle); sind(RotAngle), cosd(RotAngle)];
for i=1:size(centres,1)
centres(i,:) = centres(i,:) - [floor(xMax/2) * dx, floor(yMax/2) * dy]; %Translation
centres(i,:) = ( RotMat * centres(i,:)' ); %Rotation
end
% Get counter for intersected lines
[VertexX, VertexY] = voronoi(centres(:,1), centres(:,2));
% Filter vertices
numLines = size(VertexX, 2);
newVx = [];
newVy = [];
for lc = 1:numLines
testVec = [VertexX(:,lc) VertexY(:,lc)];
if ~any(abs(testVec) > range*1.5)
newVx = [newVx; VertexX(:,lc)'];
newVy = [newVy; VertexY(:,lc)'];
end
end
VertexX = newVx';
VertexY = newVy';
numLines = size(VertexX, 2);
Still appreciating answers or suggestions to clear up why this is/was occuring.
Example values that cause this are getCutCountHex(30, [0,0]) and ...(35, [0,0])
I cant reproduce your problem, but the thing I did notice is that your onSeg() function might be wrong: it returns true if the testpoint lies in the rectangle with two of the four corner points being segStart and segEnd.
A function that returns true iff a point is on (or more accurate: close enough to) the line segment (segStart,segEnd) could be:
function boolVal = onSeg(segStart, segEnd, testPoint)
tolerance = .5;
AB = sqrt((segEnd(1)-segStart(1))*(segEnd(1)-segStart(1))+(segEnd(2)-segStart(2))*(segEnd(2)-segStart(2)));
AP = sqrt((testPoint(1)-segEnd(1))*(testPoint(1)-segEnd(1))+(testPoint(2)-segEnd(2))*(testPoint(2)-segEnd(2)));
PB = sqrt((segStart(1)-testPoint(1))*(segStart(1)-testPoint(1))+(segStart(2)-testPoint(2))*(segStart(2)-testPoint(2)));
boolVal = abs(AB - (AP + PB)) < tolerance;
end
an approach that I found in one of the anwers here: Find if point lays on line segment. I hope that solves your problem.

Indexes and values of surrounding cells in 3d matrix

I'd like to return the indexes and values of the 8 cells surrounding a cell in a 3d matrix.
mat = rand(5,5,5);
% Cell of interest
pos = [3 3 3]
pos_val = mat(pos(1), pos(2), pos(3))
% Surrounding cells
surrounding_pos = [pos(1)-1:pos(1)+1; pos(2)-1:pos(2)+1; pos(2)-1:pos(2)+1]
surrounding_val = mat(surrounding_pos(1,:), surrounding_pos(2,:), surrounding_pos(3,:))
This works fine for values in the centre of a matrix, but it breaks if pos is on the edge. (E.g. if pos was [3,4,5], surrounding_pos would include [3,4,6], which is out of bounds)
I could obviously remove surrounding_pos values <0 or >size(mat), but this doesn't seem like a terribly MATLABian method. Any ideas?
Same solution as discussed here, but extended to multiple (any) dimensions:
mat = randi(10,5,5,5);
siz = size(mat );
N = numel(siz); % number of dimensions
M = 1; % surrounding region size
pos = [3 3 3];
pos_val = mat(pos(1), pos(2), pos(3));
surrounding_pos = cell(N,1);
for ii=1:N
surrounding_pos{ii} = max(1,pos(ii)-M):min(siz(ii),pos(ii)+M);
end
surrounding_val2 = mat(surrounding_pos{:});
The important part is the last four lines, it avoids having to c/p the max, min thing for every dimension..
Or if you like short code, the loop changed to an arrayfun:
surrounding_pos = arrayfun(#(ii) max(1,pos(ii)-M):min(siz(ii),pos(ii)+M), 1:N,'uni',false);
surrounding_val2 = mat(surrounding_pos{:});
Here is a tidied up version. Cheers.
mat = rand(5,5,5);
N = size(mat)
if length(N) < 3 || length(N) > 3; error('Input must be 3 dimensional'); end;
pos = [1 3 5]
surrounding_val = mat(max(pos(1)-1, 1):min(pos(1)+1, N(1)), max(pos(2)-1, 1):min(pos(2)+1, N(2)), max(pos(3)-1, 1):min(pos(3)+1, N(3)))
EDIT: Added an error trap.
I found this post because I needed to grab the surrounding indices of a chosen point in a Matrix. It seems like the answers here are returning a matrix of the surrounding values, but the question is also interested in the surrounding indices. I was able to do this with "try/catch" statements, which I previously did not know existed in MATLAB. For a 2D Matrix, Z:
%Select a node in the matrix
Current = Start_Node;
%Grab its x, y, values (these feel reversed for me...)
C_x = rem(Start_Node, length(Z));
if C_x ==0
C_x =length(Z);
end
C_y = ceil(Start_Node/length(Z));
C_C = [C_x, C_y];
%Grab the node's surrounding nodes.
try
TRY = Z(C_x - 1, C_y);
Top = Current -1;
catch
Top = Inf;
end
try
TRY = Z(C_x + 1, C_y);
Bottom = Current +1;
catch
Bottom = Inf;
end
try
TRY = Z(C_x, C_y + 1);
Right = Current + length(Z);
catch
Right = Inf;
end
try
TRY = Z(C_x, C_y - 1);
Left = Current - length(Z);
catch
Left = Inf;
end
surround = [Top, Bottom, Left, Right];
m = numel(surround == inf);
k = 1;
%Eliminate infinites.
surround(surround==inf) =[];
I hope someone finds this information relevant.