I have a vector and want to compute the average value

5 views (last 30 days)
[EDIT: 20110604 17:36 CDT - reformat - WDR]
Good afternoon,
This is my program and teacher Matt helped me but the result of my program should appear like the result of the main program This is my program but my graph result is different from the main program because the mean value must be zero or near to zero
Please, anyone help me
The Main program and I want my graph result appear like the graph result of main program
k=-1;
for n=1:400
k=k+1;
x(k+1)=k*(-2*pi/400);
end
x=x(:)';
s=ones(1,301);
z=0;
for j=1:400
z=z+1;
y(z,:)=5*cos(2*pi*100*(0:1/300:1)+x(1,z).*s(1,:));
end
r=0;
for i=1:301
r=r+1;
mean(r)=sum(y(:,i))/400;
end
plot(mean,'o'),grid
title('The Mean of the signal')
ylabel('Mean')
xlabel('Number of points')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
My program
theta = linspace(-2*pi,2*pi,400); % 400 equally spaced points
t = linspace(0,1,301); % 301 equally spaced points between 0 and 1.
w = 2*pi*100;
A = 5;
X = A*cos(bsxfun(@plus,theta.',w*t));
r=0;
for i=1:301
r=r+1;
mean(r)=sum(X(:,i))/400;
end
plot(mean,'o'),grid
title('The Mean of the signal')
ylabel('Mean')
xlabel('Number of points')
  3 Comments
reem
reem on 4 Jun 2011
Thanks a lot jan
but I do not have any problem in my program,I put two programs here,the first program is ((Main program))it and t((my program))it is the second program,and these two programs have the same ideas and the result of the graph must be appear similar,so my question is why my program result not appear like the result of the first program((main program)).
Jan
Jan on 4 Jun 2011
@Reem: It would be polite to make the reading of your question as easy as possible.
It is obvious that the two versions calculate different numbers. But due to the strange formatted code, the comparison of the two programs is difficult. If you concentrate to the essential lines, we could see both versions without scrolling and the difference would be *much* easier to see - and therefore to find.

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 4 Jun 2011
First of all, don't name a variable mean! This will mess up your use of the MATLAB function MEAN. This produces a plot similar to your main program, but there will be slight differences due to different approaches.
clear mean % Get rid of this bad variable name!!
theta = linspace(-2*pi*399/400,0,400); % 400 equally spaced points
t = linspace(0,1,301); % 301 equally spaced points between 0 and 1.
w = 2*pi*100;
A = 5;
X = A*cos(bsxfun(@plus,theta.',w*t));
mn2(r) = mean(X); % No need for a loop, use the MATLAB function
figure
plot(mn2,'o'),grid
title('The Mean of the signal')
ylabel('Mean')
xlabel('Number of points')
  1 Comment
reem
reem on 4 Jun 2011
Thanks a lot My best teacher Matt
you are very good teacher for us,I respect you a lot.
once again,Thank you so much for your help

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands 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!