我目前正在编写一个模块,该模块应该获取二维函数(3 x N 矩阵)的一些数据点,并根据这些点绘制近似等值线图(用于拟合的函数和变量由用户提供)。 “标题”如下所示:
project4[dataPoints_, functionList_, fittingVarsList_, plotArgs___] :=
Module[{fitFunc, functionContourPlot, dataPointsXY, pointsPlot,
xList, yList},
使用示例:
project4[data, {1, x, y, x y, x^2, y^2}, {x, y}]
(其中数据 = {{x1,y1,f1}...})
检查参数是否有效后,我这样做:
fitFunc = Fit[dataPoints, functionList, fittingVarsList];
获得近似值。 然后我想通过执行以下操作来获取它的情节:
functionContourPlot = ContourPlot[fitFunc, {fittingVarsList[[1]], xMin, xMax},{fittingVarsList[[2]],yMin, yMax};
这会导致错误:
ContourPlot::write: Tag Part in {x,y}[[1]] is Protected. Show::gcomb: "Could not combine the graphics objects in Show[ContourPlot[fitFunc$2187,{{x,y}[[1]],xMin,xMax},{{x,y}[[2]],yMin,yMax}],"
我做错了什么?
最佳答案
问题是 ContourPlot
具有属性 HoldAll
,这会阻止 Part
评估。
Attributes@ContourPlot
你可以像这样修复它。
data = {{6, 4, 7.92}, {6, 5, 9.31}, {6, 6, 9.74},
{7, 4, 11.24}, {7, 5, 12.09}, {7, 6, 12.62},
{8, 4, 14.31}, {8, 5, 14.58}, {8, 6, 16.16}};
fittingVarsList = {x, y};
{xMin, xMax} = Through[{Min, Max}@data[[All, 1]]];
{yMin, yMax} = Through[{Min, Max}@data[[All, 2]]];
fitFunc = Fit[data, {1, x, y}, {x, y}]
这重现了问题:-
functionContourPlot = ContourPlot[fitFunc,
{fittingVarsList[[1]], xMin, xMax},
{fittingVarsList[[2]], yMin, yMax}];
可以通过使用 With
创建局部变量来解决该问题:-
functionContourPlot =
With[{a = fittingVarsList[[1]], b = fittingVarsList[[2]]},
ContourPlot[fitFunc, {a, xMin, xMax}, {b, yMin, yMax}]]
如果从 ContourPlot
的属性中删除 HoldAll
,则第一个版本可以工作...
Unprotect@ContourPlot;
ClearAttributes[ContourPlot, HoldAll]
...但这将是鲁莽的编程。
关于wolfram-mathematica - "Tag Part in (...) is Protected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17307633/