# how to graph using loop?

1 view (last 30 days)
Khang Nguyen on 21 Nov 2020
Commented: Khang Nguyen on 21 Nov 2020
I want to calculate a problem and graph it
I want to graph i and area but it only show one point on my graph
% question1
clear all
a = 1
vectori = []
areavec = []
figure;
for i = 0:20
a = a + a
area = a *(1/8).^i
scatter(i,area)
end

the cyclist on 21 Nov 2020
Edited: the cyclist on 21 Nov 2020
Put the command
hold on
on the line after figure.

### More Answers (1)

Edited: Bastian Arturo Romero Mardones on 21 Nov 2020
You must change this:
clear all
a = 1
vectori = []% ??
areavec = []
figure;
for i = 0:20
a = a + a
areavec(i+1) = a *(1/8).^i %area --> areavec and areavec is a vector, not a scalar
end
i = 0:20;
scatter(i,areavec) %area --> areavec and graph must be out of cycle

Show 1 older comment
the cyclist on 21 Nov 2020
vectori is not used in the rest of the code. I commented it out completely, and the code runs.
I edited my answer, now this works :D
Khang Nguyen on 21 Nov 2020
lovely: thanks