It sounds like you want to keep a running sum and then divide by the # of values. You could try this...
plot(x, y);
hold on;
plot(x, cumsum(y) ./ (1:length(y)), 'r');
Question
I am trying to average my plot(X,Y) data so that I get a smoother curve
the concept is of this fashion, which I can execute in the command window.
command window:
plot(X,Y)
yold=y
plot(X,(Y+yold)/2.)
yold2=y
plot(X,(Y+yold+yold2)/3.)
I can do this from the comand window to get my curve smooth, but it would be very time consuming. Here is my code.
code:
plot(X,Y);
yoldy=0;
yold=0;
for av=1:100
yoldy=yoldy+yold;
plot(X,(Y+yoldy)/av);
yold=Y;
end
plot(X,(Y+sum(yoldy)/101))
For some reason the graph looks the same, that is the plot un-averaged. Is it that my yoldy is not storing all the Y values at each stage?
This question was to address averaging values X in the program. This could simply be accomplished by adding a loop with the number times you want the program to be run, store each X values, and average them after each run. Then a smoother curve could be obtained. Sorry for not explaining properly.
Solution
It sounds like you want to keep a running sum and then divide by the # of values. You could try this...
plot(x, y);
hold on;
plot(x, cumsum(y) ./ (1:length(y)), 'r');