# Graphing a For Loop Array

조회 수: 23(최근 30일)
Owen McKim 2022년 2월 9일
편집: Turlough Hughes 2022년 2월 9일
for i = 0:5:100
y(i) = i
x(i) = i^2;
end
figure(1)
plot(y,x)
I just want to create a simple plot graph of an exponentional curve, from 0 to 100 with a step size of 5. However I'm getting an error "Array indices must be positive integers or logical values" so I entere 1 instead of 0. This now works but it outputs 1,6,11... so on. Not starting from zero. This other issue I am having is my array places 0 between each value
"1 0 0 0 0 6 0 0 0 0 11 0 0 0 0 16 0 0 0 0 21 0 0 0 0 26"
How do I stop this? As this is affecting how my graph looks
I just want the array to output 1,6,11,16,21... 댓글을 달려면 로그인하십시오.

### 답변(1개)

Turlough Hughes 2022년 2월 9일
편집: Turlough Hughes 2022년 2월 9일
i can either be the index or a value that you use on the RHS of your equations but typically not both. To fix your code you could add a counter, c, for indexing the positions in x and y
c = 1;
for i = 0:5:100
x(c) = i;
y(c) = i^2;
c = c + 1;
end
figure()
plot(x,y) Here are two other, preferable, ways you could plot it:
1.
figure(), fplot(@(x) x.^2, [0 100]) 2.
x = 0:5:100;
y = x.^2;
figure(), plot(x,y) 댓글을 달려면 로그인하십시오.

### 범주

Find more on 2-D and 3-D Plots in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!