Wolfram mathematica 理解蛛网代码
我正在尝试运行Mathematica中的蛛网代码,需要以下脚本:Wolfram mathematica 理解蛛网代码,wolfram-mathematica,Wolfram Mathematica,我正在尝试运行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],
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}]
您能帮助我吗?请参见,或其他语言所称的,或lambda函数
举个可爱的例子,假设你有这个函数
doItTwice[x_,f_] := f[f[x]];
现在假设你想用这个函数将数字7平方两次。一种方法是定义如下的平方函数:
square[x_] := x^2;
doItTwice[7, square]
好的,有一种更简洁的方法可以做到这一点,只需将平方函数写成纯函数,它看起来像(#^2)和。#
是纯函数的参数,&
正好表示它是纯函数。实际上,括号甚至不是必需的,因此您可以编写^2&
。无论如何,下面的代码现在是两次平方七的更干净的方法:
doItTwice[7, (#^2)&]