# Plot3D and NMinimize do not work correctly?

See my code:

ref = Rasterize[Graphics3D[Sphere[]], ImageResolution -> 16,
Background -> LightYellow]
Model[{x_, y_}] :=
Rasterize[Graphics3D[Sphere[], ViewPoint -> {x, y, 1}],
ImageResolution -> 16, Background -> LightYellow]
CostFunction[{x_, y_}] :=
Total[Flatten[ImageData[ImageSubtract[ref, Model[{x, y}]]]]]
Plot3D[CostFunction[{x, y}], {x, -1, 3}, {y, -1, 3}].// My problem is here?????


But When I use

list = ParallelTable[{x, y, CostFunction[{x, y}]}, {x, -10,
10}, {y, -10, 10}];
list = ParallelTable[{x, y, CostFunction[{x, y}]}, {x, -10,
10}, {y, -10, 10}];


I get the function graphics! What is the problem?

In the other hand:

I want to minimize the cost function using NMinimize[]

NMinimize[CostFunction[{x, y}], {x, y}]
{22083.2,{x->0,y->0}}


But When I calculate

CostFunction[{0,0}]


I obtain 8758.65? What is the issue please?

• This question appears to be off-topic because it is too localized; i.e, it applies only to local situation and needs of its poster, and answers will not benefit others. Oct 21 '13 at 12:02

The problem is the you CostFunction which is extremely slow because in each call it has to render a graphics, rasterize it and compute the difference of the two images. You can see that Plot3D basically works by restricting the number of points if computes. With 10 plot points, which results in 10x10 values to compute, the graphic needs about 20 seconds here

Plot3D[CostFunction[{x, y}], {x, -1, 3}, {y, -1, 3},
MaxRecursion -> 0, PlotPoints -> 10]


In default mode, Plot3D will divide each interval further if the surface is *too rough**. I have suppressed that by setting MaxRecursion to 0.

# Update

NMinimize[CostFunction[{x, y}], {x, y}]


I obtain

{6493.44, {x -> 0.500146, y -> -0.500021}}


When I calculate CostFunction[{.5, -.5}] then I get the same value.

• I have a MacBook Pro Core i7 and 8G of RAM and it takes 70s??? What is the characteristic of your computer? please! Oct 20 '13 at 4:34
• @phdstudent Intel Core i7 Extreme, 32 GB RAM. I'm on Linux here, which might make (in this special case) a difference too. Oct 20 '13 at 4:39
• And What about the NMinimize Function? Oct 20 '13 at 4:48
• @phdstudent I get a position of about {0.5,-0.5} where CostFunction has the exact same value as pointed out by NMinimize. Oct 20 '13 at 4:59
• Sorry, but I do not understand you. Oct 20 '13 at 5:02