1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – m_goldberg Oct 21 '13 at 12:02
4
$\begingroup$

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]

Mathematica graphics

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

About your second question. I cannot reproduce your behavior, because when I compute on my machine

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.