# I need to know how to use the for command to calculate y=2/x

조회 수: 1(최근 30일)
Saif 2022년 12월 1일
댓글: Walter Roberson 2022년 12월 4일
I am just now learning how to use for loops but I am unsure how to use it to solve an equation with two unknowns. I need to know how to use the for command to calculate y/2/x.

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

### 답변(2개)

Benjamin Thompson 2022년 12월 1일
x = 1:10;
y = 2 ./ x;
##### 댓글 수: 0표시 이전 댓글 수: -1숨기기 이전 댓글 수: -1

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

Walter Roberson 2022년 12월 4일
You should learn this pattern:
xvals = 1:0.5:10;
num_x = length(xvals);
y = zeros(num_x, 1);
for xindex = 1 : num_x
x = xvals(xindex);
y(xindex) = 2./x;
end
plot(xvals, y)
##### 댓글 수: 1표시 없음숨기기 없음
Walter Roberson 2022년 12월 4일
If you use this pattern where you loop over indices rather than over content then the content does not need to be in any particular order, and can contain duplicates. For example,
xvals = rand(1,20) * 2 - 1;
num_x = length(xvals);
y = zeros(num_x, 1);
for xindex = 1 : num_x
x = xvals(xindex);
y(xindex) = 2./x;
end
plot(xvals, y, '*')

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

### 범주

Find more on Correlation and Convolution 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