summation in matlab help

1 view (last 30 days)
mutah
mutah on 14 Sep 2013
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 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 15 Sep 2013
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 on 15 Sep 2013
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.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 14 Sep 2013
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 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 14 Sep 2013
sum has the same length then x
Image Analyst
Image Analyst on 14 Sep 2013
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.

Sign in to comment.


mutah
mutah on 15 Sep 2013
any body can correct my code or try to plot

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!