Sunday, 5 April 2015

matlab - Let $sum_{n=1}^infty frac{a_n}{3^n}.$ Determine (numerically or not) the limit of the infinite series by choosing $a_n=0$ or $2$ randomly.




The problem I have is essentially in the title.



What I'm trying to do in Matlab is to have a set which has two elements $0$ and $2$ and to choose either $0$ or $2$ randomly and plug it into the series at some $n$.



To just generate either $0$ or $2$ I've used the following code:



r=randi([1 2],10,1)
if r==1
r=r-1
end



This was how I figured I would get either $0$ or $2$, yet I still output a $1$ as well. Is my approach even correct using the randi function or is there a more specific function to use for my purpose?



Also, if there's a way to do this without actually having to run any calculations that would be great too.



Thanks for any help or feedback!


Answer



So





r = randi([1,2], 10, 1)



generates a $10 \times 1$-vector of random entries, if you now compare $r$ to $1$ by r==1 this will never be true, as $r$ is a vector, not the scalar $1$, what you can do is find all 1s in $r$ and replace them by zeros doing




r(find(r==1)) = 0;




or apply a map to $r$ witch fixes 2 and maps 1 to 0 e. g.




r = 2*r - 2;


No comments:

Post a Comment

real analysis - How to find $lim_{hrightarrow 0}frac{sin(ha)}{h}$

How to find $\lim_{h\rightarrow 0}\frac{\sin(ha)}{h}$ without lhopital rule? I know when I use lhopital I easy get $$ \lim_{h\rightarrow 0}...