2017-04-01 62 views
0

我试图运行的Mathematica蛛网代码,我需要下面的脚本:了解一个蛛网代码

ClearAll[CobwebPlot] 
Options[CobwebPlot]=Join[{CobStyle->Automatic},Options[Graphics]]; 
CobwebPlot[f_,start_?NumericQ,n_,xrange:{xmin_,xmax_},opts:OptionsPattern[]]:=Module[{cob,x,g1,coor}, 
cob=NestList[f,N[start],n]; 
coor = Partition[Riffle[cob,cob],2,1]; 
coor[[1,2]]=0; 
cobstyle=OptionValue[CobwebPlot,CobStyle]; 
cobstyle=If[cobstyle===Automatic,Red,cobstyle]; 
g1=Graphics[{cobstyle,Line[coor]}]; 
Show[{Plot[{x,f[x]},{x,xmin,xmax},PlotStyle->{{Thick,Black},Black}],g1},FilterRules[{opts},Options[Graphics]]] 
] 

Manipulate[CobwebPlot[Sqrt[3#-1]&,\[Alpha],40,{0,5},PlotRange->{{0,4.5},{0,3.65}},Frame->True,Axes->False,CobStyle->Directive[Dashed,Red],PlotRangePadding->None],{\[Alpha],0.5,4.375}] 

我发现剧本在线,但我不明白的一些功能,比如什么是以下字符的目的,&,在操纵[]分段的代码的:

Manipulate[CobwebPlot[Sqrt[3#-1]&,\[Alpha],40,{0,5},PlotRange->{{0,4.5},{0,3.65}},Frame->True,Axes->False,CobStyle->Directive[Dashed,Red],PlotRangePadding->None],{\[Alpha],0.5,4.375}] 

你能帮我吗?

回答

1

请参阅this Mathematica documentation page on pure functions或其他语言称为anonymous functions或lambda函数。


举一个可爱的例子,假设你具备的功能

doItTwice[x_,f_] := f[f[x]]; 

现在说你想要使用此功能,方七这个数字的两倍。这样做的一个方法是这样定义的平方函数:

square[x_] := x^2; 
doItTwice[7, square] 

那么,有通过简单地写平方函数作为一个纯函数,它看起来像(#^2)&这样做一个更清洁的方式。 #是纯函数的参数,&就是表示它是纯函数。真的圆括号甚至没有必要,所以你可以写#^2&。无论如何,下面的代码现在是一个更简洁的方法来平方七次:

doItTwice[7, (#^2)&] 
+0

非常感谢你!这非常有帮助。 –