How to use if, if I want to locate something in a matrix

조회 수: 1 (최근 30일)
Eric Daiber
Eric Daiber 2016년 9월 27일
답변: Eric Daiber 2016년 9월 29일
I am trying to model a streamline with a pumping well. I have an equation describing the position of the streamline, however, the analytical solution fails at the well location. What I want is to locate the cells that misbehave in matrix U, which equal (xi+or-2) and (yi+or-2) and make them equal to xi and yi. I tried if (..) function but I havent been successful. Any aid is appreciated. Thanks, Eric
theta = 135; %degrees
Q = 10000; %ft^3/day
xi = 10; %Well X Position
yi = -10;%Well Y position
q = 1.5
B= 300
Psi = @(m,d) q*sin(theta)*m - q*cos(theta)*d - Q./(2*pi*B)*atan(((m-xi)./(d-yi))); %
D = []; %matrix to contain results from Psi(x,y)
m = 9;%starting x position of particle
d = -9;%starting y position of particle
E= Psi(m,d); %evaluation of Psi for initial position
D = [D;E,m,d];%store Psi in D
%Psi2 equation used in the fzero equation
Psi2 = @(x,y) q*sin(theta)*x - q*cos(theta)*y - Q./(2*pi*B)*atan(((x-xi)./(y-yi))) - D(1,1);% where D(1,1) is the initial Psi value obtained from above.
U = [];%matrix created to contain results of fzero(Psi2).
for y = (d-25):(abs(d)+25); %Y values of particles
x0 = [-5000 5000]; %initial guess for fzero
[x1]= fzero(@(x) Psi2(x,y),x0); %fzero calculation
U=[U;x1, y];
end
% if U(:,1) == (xi-2) && U(:,2) == (yi-2)
% U(:,1) = xi
% elseif U(:,1)>x1;
% x1=x1;
% else U(:,1)<x1;
% x1=x1;
% end
plot(U(:,1), U(:,2), 'k')
xlabel 'x (ft)'
ylabel 'y (ft)'
hold on
plot (xi, yi,'r X')
plot (m,d,'g O')
  댓글 수: 1
KSSV
KSSV 2016년 9월 28일
Plot gives a straight line with a kink close to xi,yi...you want to remove that kink.Is it? Can you be bit more clear about the task.

댓글을 달려면 로그인하십시오.

채택된 답변

Mudambi Srivatsa
Mudambi Srivatsa 2016년 9월 29일
I understand that you want to use "if" to find the cells that misbehave in matrix U which equal (xi+or-2) and (yi+or-2) and replace them with 'xi' and 'yi' respectively. The issue is because of the '&&' operator that works for scalar arguments. The operations U(:,1) == (xi-2) and U(:,2) == (yi-2) will each result in a vector and you cannot use '&&' operator. So, you should use '&' operator that works for vector operands.
It is not clear what you exactly meant by (xi+or-2). I am assuming (xi+or-2) means either xi+2 or xi-2 but not the range [xi-2 xi+2] from the commented code. The following code should work for replacing the values with 'xi', 'yi' when matrix values are (xi+or-2) and (yi+or-2).
% find indices where x and y elements are both (+or-2)
ind = (abs(U(:,1) - xi) == 2) & (abs(U(:,2) - yi) == 2)
% replace matching x elements with new value
U(ind,1) = xi;
% replace matching y elements with new value
U(ind,2) = yi;
If (xi+or-2) meant the range [xi-2 xi+2], you can modify the code as follows:
% find indices where x and y elements are both (+or-2)
ind = (abs(U(:,1) - xi) <= 2) & (abs(U(:,2) - yi) <= 2)
% replace matching x elements with new value
U(ind,1) = xi;
% replace matching y elements with new value
U(ind,2) = yi;

추가 답변 (1개)

Eric Daiber
Eric Daiber 2016년 9월 29일
Thanks, I used the following code where z = U. Where if the inverse slope increased over 10, I truncated the remaining values.
for i = 2:(numel(z)/2);
if abs((z(i,1)-z(i-1,1))/(z(i,2)-z(i-1,2))) > 10 ;
z(i:(numel(z)/2),1) = z(i-1,1);
z(i:(numel(z)/2),2) = z(i-1,2);
break
end
end

카테고리

Help CenterFile Exchange에서 Manage Products에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by