I have a vector and want to compute the average value

1 回表示 (過去 30 日間)
reem
reem 2011 年 6 月 4 日
[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 件のコメント
reem
reem 2011 年 6 月 4 日
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 2011 年 6 月 4 日
@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.

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2011 年 6 月 4 日
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 件のコメント
reem
reem 2011 年 6 月 4 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by