function scoreout = pivotgolf(course,pivotstrat)
% PIVOTGOLF Pivot Pickin' Golf.
% Your goal is to use LUGUI to compute the LU decompositions of nine
% matrices with as little roundoff error as possible. Your score for
% each hole is norm(R,inf)+norm(Leps,inf)+norm(Ueps,inf) where
% R = L*U - A(p,q) is the residual and Leps and Ueps are the nonzeros
% that should be zero in L and U.
%
% The six golf courses include:
% magic -- magic squares and Golub matrices, some are rank deficient.
% testmats -- various test matrices, Pascal, Hilbert, gallery, etc.
% rand #s -- random integer matrices initialized by rand('state',s).
%
% The four pivot strategies are:
% pick -- use the mouse to pick the pivots.
% diagonal -- pivot on the diagonal, it is possible to divide by zero.
% partial -- pivot is the largest element in the current column.
% complete -- pivot is the largest element in the unreduced matrix.
%
% PIVOTGOLF(course,pivotstrat) bypasses the initial screen.
% course = 'magic', 'testmats', or a numeric s to set rand('state',s).
% pivotstrat = 'pick','diagonal','partial', or 'complete'.
%
% Try to pick pivot elements that divide into the other elements exactly.
% If you can choose a pivot element that is a power of two, there will
% be no roundoff error at that step. But if the pivot is small, the
% elements in the unreduced matrix might grow larger and subsequent steps
% might have larger roundoff errors.
%
% You should know that "mulligan" is golf terminology for "OOPS, I want
% to take that shot over". Good luck!
%
% See also LUGUI. if nargin == 0 % Initial screen shg
clf
axes('pos',[0 0 1 1]);
axis off
set(gcf,'double','on','name','Pivot Pickin'' Golf', ...
'menu','none','numbertitle','off','color','white')
text('units','norm','pos',[.25,.75],'color',[0 0.65 0], ...
'fontweight','bold','fontsize',16, ...
'string',sprintf('Welcome to Pivot Pickin'' Golf'))
text('units','norm','pos',[.03 .60],'fontweight','bold','string','Course:')
for k = 1:6
switch k
case 1,
str = 'magic';
case 2,
str = 'testmats';
otherwise,
str = ['rand #' int2str(k-2)];
end
b(k) = uicontrol('units','norm','pos',[.14*k .57 .12 .06], ...
'style','toggle','string',str,'back','w');
end
text('units','norm','pos',[.03 .45],'fontweight','bold','string','Strategy:')
str = {'pick','diagonal','partial','complete'};
for k = 1:4
b(k+6) = uicontrol('units','norm','pos',[.14*k .42 .12 .06], ...
'style','toggle','string',str{k},'back','w');
end
stop = uicontrol('style','toggle','string','X','fontweight','bold', ...
'back','white','units','norm','pos',[.96 .96 .04 .04]);
uicontrol('units','norm','pos',[.02 .02 .10 .05], ...
'string','help','back','white','callback','helpwin pivotgolf')
bvals = [];
while sum(bvals) < 2
if get(stop,'val'), break, end
pause(.05)
bvals = cell2mat(get(b,'val'));
end
if get(stop,'val')
close(gcf)
return
end
for k = find(bvals)'
switch k
case 1, course = 'magic';
case 2, course = 'testmats';
case {3,4,5,6}, course = k-2;
otherwise, pivotstrat = get(b(k),'string');
end
end
elseif nargin == 1
pivotstrat = 'pick';
end if isnumeric(course)
rand('state',course)
else
course = lower(course);
end % Loop over nine holes (matrices). score = 0;
h = 1;
A = [];
while h <= 9
if isempty(A) % Generate the matrix. switch course
case 'magic'
n = 2+ceil(2/3*h);
switch h
case {3,6,9}
A = golub(n);
otherwise
A = magic(n);
end
case 'testmats'
n = h;
switch h
case 1
n = 7;
e = ones(n,1);
A = full(spdiags([e (-3:3)' e],[-1 0 1],n,n));
case 2
n = 5;
A = vander((-2:2)');
case 3
A = gallery(n);
case 4
A = hadamard(n);
case 5
A = gallery(n);
case 6
A = pascal(n);
case 7
n = 6;
A = pascal(n);
A(n,n) = A(n,n)-1;
case 8
n = 5;
A = 27720*hilb(n);
case 9
n = 6;
U = eye(n,n) - triu(ones(n,n),1);
A = U'*U;
end
otherwise
n = ceil(2+3*rand);
A = round(10*(2*rand(n,n)-1));
end
end % Use LUGUI to compute the LU decomposition [L,U,p,q] = lugui(A,pivotstrat);
pause(2); % Score R = (L*U - A(p,q));
Leps = L.*(abs(L)<1000*norm(L,1)*eps);
Ueps = U.*(abs(U)<1000*norm(U,1)*eps);
show(abs(R)+abs(Leps)+abs(Ueps));
if all(isfinite(R(:)))
s = ceil(4*(norm(R(:),1)+norm(Leps(:),1)+norm(Ueps(:),1))/eps)/4;
else
s = Inf;
end % Report the score and decide what to do next. set(gcf,'name','Pivot Pickin'' Golf')
text('units','pixels','pos',[20+50*n,20+(n+2)*30], ...
'fontweight','bold','fontsize',12, ...
'color',[0 0.65 0],'string',sprintf('hole #%d',h))
text('units','pixels','pos',[50*(n-1)+25,55], ...
'fontweight','bold','fontsize',12,'color',[0 0 0.90], ...
'string',sprintf('score = %s, total = %s',sph(s),sph(score+s)))
stop = uicontrol('style','toggle','string','X','fontweight','bold', ...
'back','w','pos',[100*n+75 30*n+65 25 25]);
if isequal(pivotstrat,'pick')
next = uicontrol('units','pixels','pos',[50*(n-1) 10 90 20], ...
'style','toggle','fontweight','bold', ...
'background','white','string','next');
if h==9, set(next,'string','finish'), end
mulligan = uicontrol('units','pixels','pos',[50*(n+1) 10 90 20], ...
'style','toggle','fontweight','bold', ...
'background','white','string','mulligan');
uics = [stop next mulligan];
while all(cell2mat(get(uics,'val'))==0)
drawnow
end
if get(mulligan,'val'), continue, end
else
pause(3)
end
score = score + s;
if get(stop,'val'), break, end
h = h + 1;
A = [];
end % Final screen clf
set(gcf,'double','on','name','Pivot Pickin'' Golf', ...
'pos','default','menu','none','numbertitle','off','color','white')
axes('pos',[0 0 1 1]);
axis off
text('units','norm','pos',[.15,.60], ...
'fontweight','bold','fontsize',16,'color',[0 0.65 0], ...
'string',sprintf('Thanks for playing Pivot Pickin'' Golf.'))
if score == 0
text('units','norm','pos',[.15,.50],...
'fontweight','bold','fontsize',16,'color',[0 0.65 0], ...
'string','Perfect score. Congratulations!')
elseif score == Inf
text('units','norm','pos',[.15,.50], ...
'fontweight','bold','fontsize',16,'color',[0 0.65 0], ...
'color',[0 0.65 0],'string','Your score was infinite.')
text('units','norm','pos',[.15,.40], ...
'fontweight','bold','fontsize',16,'color',[0 0.65 0], ...
'color',[0 0.65 0],'string','You have to avoid dividing by zero.')
else
text('units','norm','pos',[.15,.50], ...
'fontweight','bold','fontsize',16,'color',[0 0.65 0], ...
'string',sprintf('Your score was %s eps.',sph(score)))
if score > 100
text('units','norm','pos',[.15,.40], ...
'fontweight','bold','fontsize',16,'color',[0 0.65 0], ...
'string','Better luck next time.')
end
end
uicontrol('style','toggle','units','norm','pos',[.96 .96 .04 .04], ...
'string','X','fontweight','bold','callback','close(gcf)')
if nargout > 0
scoreout = score;
pause(3)
end %------------------------------------------------------------ function show(A)
% Same code as LUGUI.
clf
axes('pos',[0 0 1 1]);
axis off
[m,n] = size(A);
dx = 100;
dy = 30;
Acolor = [0 0 0];
for j = 1:n
for i = 1:m
t(i,j) = text('units','pixels','string',spf(A(i,j)), ...
'fontname','courier','fontweight','bold','fontsize',14, ...
'horiz','right','color',Acolor, ...
'pos',[20+j*dx 20+(m+2-i)*dy]);
end
end %------------------------------------------------------------ function s = spf(aij)
% Subfunction to format text strings
if aij == 0
f = '%10.0f';
elseif (abs(aij) < 1.e-4) | (abs(aij) >= 1.e4)
f = '%10.1e';
else
f = '%10.4f';
end
s = sprintf(f,aij); %------------------------------------------------------------ function s = sph(x)
% Format text strings that are integer multiples of 1/4.
if x == 0
f = '%d';
elseif x == round(x);
f = '%1.0f';
elseif x == round(2*x)/2;
f = '%2.1f';
else
f = '%3.2f';
end
s = sprintf(f,x);

Matlab pivotgolf的更多相关文章

  1. Matlab lugui

    function [L,U,pv,qv] = lugui(A,pivotstrat) %LUGUI Gaussian elimination demonstration. % % LUGUI(A) s ...

  2. Matlab 绘制三维立体图(以地质异常体为例)

    前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...

  3. Matlab slice方法和包络法绘制三维立体图

    前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...

  4. Matlab 高斯_拉普拉斯滤波器处理医学图像

    前言:本程序是我去年实现论文算法时所做.主要功能为标记切割肝脏区域.时间有点久,很多细节已经模糊加上代码做了很多注释,因此在博客中不再详述. NOTE: 程序分几大段功能模块,仔细阅读,对解决医学图像 ...

  5. MATLAB中绘制质点轨迹动图并保存成GIF

    工作需要在MATLAB中绘制质点轨迹并保存成GIF以便展示. 绘制质点轨迹动图可用comet和comet3命令,使用例子如下: t = 0:.01:2*pi;x = cos(2*t).*(cos(t) ...

  6. linux下配置matlab运行环境(MCR)

    在安装好的matlab下有MCR(MatlabCompilerRuntime)在matlab2011/toolbox/compiler/deploy/glnxa64下找到MCRInstaller.zi ...

  7. EMD分析 Matlab 精华总结 附开源工具箱(全)

    前言: 本贴写于2016年12与15日,UK.最近在学习EMD(Empirical Mode Decomposition)和HHT(Hilbert-Huang Transform)多分辨信号处理,FQ ...

  8. Atitit MATLAB 图像处理 经典书籍attilax总结

    Atitit MATLAB 图像处理 经典书籍attilax总结 1.1. MATLAB数字图像处理1 1.2. <MATLAB实用教程(第二版)>((美)穆尔 著)[简介_书评_在线阅读 ...

  9. Atitit MATLAB 图像处理attilax总结

    Atitit MATLAB 图像处理attilax总结 1.1. 下载 Matlab7.0官方下载_Matlab2012 v7.0 官方简体中文版-办公软件-系统大全.html1 1.2. Matla ...

随机推荐

  1. Android 修改host文件的3种方法

    Android修改hosts文件的方法介绍 本文介绍三种Android手机修改hosts文 件的方法,但修改hosts文件一定要谨慎:Android手机hosts文件的换行符必须是n而不是window ...

  2. php小数点后取两位的三种实现方法

    php小数点后取两位的方法. 方法一.经常用到小数点后取几位,但不能进位的情况. 比如3.149569取小数点后两位,最后两位不能四舍五入.结果:3.14. 可以使用函数floor. 该函数是舍去取整 ...

  3. python网络编程-01

    python网络编程 1.socket模块介绍 ①在网络编程中的一个基本组件就是套接字(socket),socket是两个程序之间的“信息通道”. ②套接字包括两个部分:服务器套接字.客户机套接字 ③ ...

  4. 设置cmd的codepage的方法

    设置cmd的codepage的方法 有时候,我们的cmd.exe的codepage和字体等会变化,比如突然由中文变成英文的codepage(因为一些sh程序的干扰). 下面是修正方法: [HKEY_C ...

  5. 把WinXP装进内存 性能飚升秒杀固态硬盘

    现在用户新配置的电脑,内存很少有小于2GB的,配置4GB内存的朋友也有不少.容量如此大的内存,我们在使用电脑的日常操作中绝对用不完.而目前制约系统性能最大的瓶颈就是硬盘的传输速度,所以,这里教你怎么把 ...

  6. linux下安装python3.3.4

    下载安装包 # wget http://www.python.org/ftp/python/3.3.4/Python-3.3.4.tgz 解压 # tar -xzvf Python-3.3.4.tgz ...

  7. stringstream字符串流

    例题详解 題目:输入的第一行有一个数字 N 代表接下來有 N 行資料,每一行資料里有不固定個數的整數(最多 20 個,每行最大 200 個字元),請你寫一個程式將每行的总和印出來. 輸入: 3 1 2 ...

  8. 关于cvScalar的那些事

    CvScalar 可存放在1-,2-,3-,4-TUPLE类型的捆绑数据的容器 该函数包含4个浮点成员,可以用来表示B(Blue),G(Green),R(Red),Alpha(表示图像的透明度) ty ...

  9. ANDROID自己定义视图——onLayout源代码 流程 思路具体解释

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候.事实上非常easy.仅仅须要知道3步骤: 1.測量- ...

  10. 记userscripts.org

    发现一些Firefox用户脚本不起作用,userscripts.org访问不能有一个很长的一段时间,我还以为出了什么问题没出去检查.前几天有时间检查脚本,在路上,然后返回到userscripts.or ...