% scripts and functions written in class on Sept 12 % make a function that plots N random points function plot_rand_pts(N) x=rand(1,N); y=rand(1,N); figure; hold on; title([num2str(N), ' Random points drawn on ', date) scatter(x,y,'k','filled'); return % add input that determines color function plot_rand_pts_2(N,color) x=rand(1,N); y=rand(1,N); figure; hold on; title([num2str(N), ' Random points drawn on ', date) scatter(x,y,color,'filled'); return % If N is even use dots. If N is odd use crosses function plot_rand_pts_3(N,color) x=rand(1,N); y=rand(1,N); figure; hold on; title([num2str(N), ' Random points drawn on ', date]) if mod(N,2)==0 scatter(x,y,'filled'); else scatter(x,y,'k+'); end grid on; return % make a function that adds up the first n primes function out = sum_primes(n) counter=0; k=1; out=0; while counter < n k=k+1; if isprime out=out+k; counter=counter+1; end end out return; % function to iterate f(x) = 4x(1-x) n times function out = iter(x,n) out=x; for k=1:n out=4*out*(1-out); end return % Now draw first 50 points using red dots % and second 50 using black +'s. Add grid % and title including numner of points and % today's date function plot_rand_pts(N) x=rand(1,N); y=rand(1,N); figure; hold on; title([num2str(N), ' Random points drawn on ', date]) scatter(x(1:N/2),y(1:N/2),'r','filled'); scatter(x(1+N/2:N),y(1+N/2:N),'k+'); grid on; return; % plot a random function with uniform step % sizes in [-.5, 5] x=rand(1,1000)-.5; y=cumsum(x); figure; hold on; grid on; title('Random Walk, 1000 steps'); plot(y) % a function to sum elements of an array to the pth power function out = sum_array(n,p) out = sum([1:n].^p); return % this function sums the first n primes function out = sum_primes(n) out=0; count=0; k=1; while count < n k=k+1; if isprime(k) out=out+k; count=count+1; end end return % function to sum all perfect % squares less than n with last digit k function out =sum_squares(n,k) x=[1:sqrt(n)].^2; y=x(mod(x,10)==k); sum(y) return