Guest User

Untitled

a guest
Jan 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. N=2; % Starting/current matrix size
  2. M=1; % Number of trials per matrix
  3. incr=2; %matrix size increment
  4. reps=20; %number of increments
  5. all_errs=zeros(reps,1); %vector of mean erros for each N
  6. used_N=zeros(reps,1); %values of N used for plot
  7.  
  8. %find M such that max margin of error <0.01 for confidence interval
  9. desired_margin=0.01; %desired margin of error
  10. conf=0.98; %value for confidence interval
  11. curr_margin=conf/sqrt(M); %current margin of error
  12.  
  13. while curr_margin>=desired_margin
  14. M=M+1;
  15. curr_margin=conf/sqrt(M);
  16. end
  17.  
  18. for h=1:reps
  19. used_N(h)=N; %add current value of N to list of used N values
  20. errs=zeros(M,1); % Vector of errors
  21. x=ones(N,1); % exact solution vector
  22.  
  23. for i=1:M
  24. A=spdiags(rand(N,3), -1:1, N,N); %constructs tridiagonal matrx with random entries
  25. b=A*x; % Compute the right-hand side vector
  26. z=Ab; % Solve the linear system
  27. errs(i)=max(abs(z-x)); % Compute the error
  28. end
  29.  
  30. mean_err=mean(errs);
  31. all_errs(h)=mean(errs);
  32. N=N+incr; %increments N
  33.  
  34. end
  35. %disp(all_errs)
  36.  
  37. p=polyfit(log10(used_N), log10(all_errs), 1); %fits line to data
  38. fit=exp(polyval(p,log10(used_N))); %y-coordinates of best fit line
  39. loglog(used_N, all_errs, 'o',used_N,fit,'-'); %plots the error versus N in a loglog plot w/best fit line
  40. title(['Log-Log Plot for Matrix Size vs. Mean Error'],'fontsize',14)
  41. xlabel('log_{10}(Size of Matrix)','fontsize',12)
  42. ylabel('log_{10}(Mean Error)','fontsize',12)
  43.  
  44. fit=10.^(polyval(p,log10(used_N)));
Add Comment
Please, Sign In to add comment