summation in matlab help
1 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!