# two variables for same coordinate

조회 수: 1(최근 30일)
pooja sudha 2021년 5월 16일
댓글: pooja sudha 2021년 5월 27일
Hey,
I have this potential V=1/4*pi*epsilon*sqrt(((r1-r2)^2)+const.^2).
I solved for V =1/4*pi*epsilon*r using finite difference method but I'm unable to understand how to vary two variables for same axis. I have tried to do using different different loop for both parameters but can't find the solutions.

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

### 채택된 답변

Image Analyst 2021년 5월 16일
Pooja: You can use either meshgrid() or for loops. Below I show you both ways.
const = 2;
epsilon = 3;
maxR1 = 5.5;
maxR2 = 7.4;
% Define size of output matrix.
rows = 5;
columns = 4;
% Get x and y coordinates at each (y, x) location.
R1 = linspace(1, maxR1, columns); % x
R2 = linspace(1, maxR2, rows); % y
% Method 1 : vectorized using meshgrid()
[r1, r2] = meshgrid(R1, R2)
V = (1/4) * pi * epsilon * sqrt(((r1-r2).^2)+const ^ 2)
% Method 2 : for loops
V = zeros(rows, columns);
for col = 1 : columns
r1 = R1(col);
for row = 1 : rows
r2 = R2(row);
V(row, col) = (1/4) * pi * epsilon * sqrt(((r1-r2).^2)+const ^ 2);
end
end
V
fprintf('Done running %s.m ...\n', mfilename);
##### 댓글 수: 4표시숨기기 이전 댓글 수: 3
pooja sudha 2021년 5월 27일
Hey Thankyou. It worked

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

### 추가 답변(1개)

pooja sudha 2021년 5월 19일
here i computed for I-dimension & for 1-particle by using the potential V=1/sqrt(r^2+constant^2) ,now same thing I'm trying to do for 2-particle by adding the coordinate for second particle in the potential like V=1/sqrt((r1-r2)^2+constant^2).
Thank you

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

### 범주

Find more on Surface and Mesh Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by