2013-06-25 132 views
2

我目前正在編寫一個模塊,該模塊應該採用二維函數的某些數據點(一個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 ::寫:在{X,Y}標籤結果第[1]是受保護的。 Show :: gcomb: 「無法合併 中的圖形對象顯示[ContourPlot [fitFunc $ 2187,{{x,y} [[1]],xMin,xMax},{{x,y} [[2]] ,yMin,yMax}],「

我在做什麼錯?

+0

您的帖子的標題應該是一個問題。 –

回答

2

問題是ContourPlot的屬性爲HoldAll,這可以防止Part評估。

[email protected] 

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 ...

​​

...但是這將是魯莽的編程。

+1

你是我的救星。 – user2521472

相關問題