<texit info> author=Roman Putanowicz title=Solution to exercise 4.1.1 </texit> back Solution to exercise 4.1.1 Firstly we should note that our expression can be transformed to an equivalent form: \begin{equation*} \sum_{j=1}^{j=N}b_j\left(\sum_{i=1}^{i=j}a_i\right) \end{equation*} Then we should also note that there is no need to calculate the inner sum from scratch for each new “j” – we can update it from the previous j-iteration. Thus the expression value can be calculated with a single loop. For a convenience in the solution presented below we implement the sequences “a” and “b” with separate functions. <sxh c> N = input(“Give N : ”); function av = a(i) av = sin(2*pi*i+1); endfunction function bv = b(i) bv = 1/(1+i); endfunction totalsum = 0; asum = 0; for j=1:N asum = asum + a(j); totalsum = totalsum + b(j) * asum; endfor printf(“Expression value = %g\n”,totalsum); </sxh> <texit> \begin{lstlisting} N = input(“Give N : ”); function av = a(i) av = sin(2*pi*i+1); endfunction function bv = b(i) bv = 1/(1+i); endfunction totalsum = 0; asum = 0; for j=1:N asum = asum + a(j); totalsum = totalsum + b(j) * asum; endfor printf(“Expression value = %g\n”,totalsum); \end{lstlisting} </texit> en/teaching/subjects/it/labs/sol_4_1_1.txt Last modified: 2017/10/02 15:54(external edit)