wolfram-mathematica - "Tag Part in (...) is Protected"

标签 wolfram-mathematica

我目前正在编写一个模块,该模块应该获取二维函数(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

enter image description here

你可以像这样修复它。

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}]

enter image description here

这重现了问题:-

functionContourPlot = ContourPlot[fitFunc,
   {fittingVarsList[[1]], xMin, xMax},
   {fittingVarsList[[2]], yMin, yMax}];

enter image description here

可以通过使用 With 创建局部变量来解决该问题:-

functionContourPlot = 
 With[{a = fittingVarsList[[1]], b = fittingVarsList[[2]]},
  ContourPlot[fitFunc, {a, xMin, xMax}, {b, yMin, yMax}]]

enter image description here

如果从 ContourPlot 的属性中删除 HoldAll,则第一个版本可以工作...

Unprotect@ContourPlot;
ClearAttributes[ContourPlot, HoldAll]

...但这将是鲁莽的编程。

关于wolfram-mathematica - "Tag Part in (...) is Protected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17307633/

相关文章:

wolfram-mathematica - 如何运行调色板的初始化代码?

wolfram-mathematica - Plot3D : Drawing Points at Mesh Intersections

pdf - 将图形导出为 PDF 时,可以在绘图标签中导出特殊符号/西里尔字母吗?

arrays - 有没有什么有效的简单方法可以用 Mathematica 比较两个长度相同的列表?

algorithm - Mathematica 在 NonlinearModelFit[] 中使用什么拟合算法?

wolfram-mathematica - Mathematica 二维极限计算

algorithm - 在 Mathematica 中查找(重复)列表周期的最佳方法是什么?

wolfram-mathematica - Mathematica,比较日期的有效方法

wolfram-mathematica - Mathematica 执行时错误 : symbol names

wolfram-mathematica - 如何在 Mathematica 中找到一个选项的所有可能值?