summation in matlab help

1 回表示 (過去 30 日間)
mutah
mutah 2013 年 9 月 14 日
this is my code to plot summation of 10 normpdf but when i plot figure the plot not like normpdf what the reason ??? plz answer me
w=[0.1139;0.0087;0.6147;0.0437 ; 0.1752;0.0115 ;0.0017;0.0198;0.0034;0.0074 ];
sigma=[0.0398;0.4716;0.4448;0.2984;0.1466;0.2828;1.3330;0.3010;0.0415;0.3592];
mu=[0.0074 ;4.9530;0.9306;2.8815;0.5280;4.0052;6.9460;2.4941;0.2566;3.4896];
x=0:20:80;
sum=0;
for i=1:10
y=w(i)*normpdf(x, mu(i), sigma(i));
sum=sum+y;
end
plot(x,sum);
grid on
  4 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 15 日
Ok, but that's what your code is doing. If the result is not what you are expecting, then tell us what is the wanted result
Walter Roberson
Walter Roberson 2013 年 9 月 15 日
If I understand correctly, you are expecting that normpdf() will return a vector, since you pass in a vector for x.
I do not have that toolbox, so I cannot experiment to be sure, but it does look to me as if it should be returning a vector.
You should put a breakpoint in on the line after you calculate y, run the program, and examine y to see what result you get.

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

回答 (2 件)

Image Analyst
Image Analyst 2013 年 9 月 14 日
First of all, DON'T use sum as the name of your variable because you'll no longer have access to the built-in function by that name. Call it "theSum" or something else.
Secondly, all you're doing is adding up a bunch of numbers to get a single number out, then you plot it (or try to). What do you expect to see? sum is not an array and does not have different values for different i - it's just a single number, not an array.
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 14 日
sum has the same length then x
Image Analyst
Image Analyst 2013 年 9 月 14 日
I think that either y or theSum might want to take an index: y(i) or theSum(i). Or he could just calculate theSum after the loop if he indexes y: theSum = sum(y). Unfortunately I don't have the normpdf() function so I can't really fix the code, plus lack of comments make it unclear what he wants to do.

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


mutah
mutah 2013 年 9 月 15 日
any body can correct my code or try to plot

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by