Summation with (Dependent summation)

I need to write a code in Matlab for the following equation:
Σ i=0 to 100 ( (f(x). Σ j=0 to k f(y) )
where f(x), f(y) and k returns constant values. I have tried to write this code. Kindly tell me if this code is logically correct or if we can avoid loops; it would be better.
fx=0.08092;
fy=0.01932;
k=300
YY1 = [];
sum2=0;
for i=1:100
Y2 = [];
for j=1:k
Y1=sum(f2);
end
Y2=sum(f1*Y1)
YY1 = [YY1 Y2];
end
Final=sum(YY1)

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 10 de Mayo de 2013
This line will slow it down a lot:
YY1 = [YY1 Y1];
It resizes YY! on each iteration. Instead, rpeallocate YY1 and then fill it in:
YY1 = zeros(100*jj,1);
then
YY1(ii+jj-1) = Y1;
Of course if you don't need YY1 for anything other than the sum, then just add it on each iteration:
YY1 = YY1+Y1;
Also, your function is not dependent on the for loop variables, so the same answer will be returned on each iteration. This you could also just multiple
sum2 = sum(fx*sum(fy))*100*jj

Más respuestas (1)

Usman Khalid
Usman Khalid el 13 de Mayo de 2013

0 votos

Thanks for your answer. Please tell me why did you write jj instead of j? and there is another error at YY1(ii+jj-1) = Y1; Error: Attempted to access YY1(-1); index must be a positive integer or logical

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by