Message Boards Message Boards

Create and minimize a function f(x,y) using a list of data

Posted 11 years ago
Hi!
I'm new to Mathematica and need some help. I have a 3D surface (text file with x y z numbers). I'm able to read it with ReadList and plot it
with ListPlot3D, but I'd like to define the surface as a function:
z=f(x,y)
So I can then use NMinimize to search for local minina on the surface: NMinimize[z,{x,y}]
I hope this makes sense and someone can help me.
Regards
J.
POSTED BY: jeomana
3 Replies
Posted 11 years ago
Dear Gregory and Vitality, thanks a lot for your answer!
I was able to do what I needed, and now I can test different optimization algorithms.

Regards
Jose
POSTED BY: jeomana
Gregory, welcome to the site! It is really great to see your contribution here. I think your answer is spot on, I will extend it by giving an example.

Create a list of multidimensional data:
data = Flatten[Table[{{x, y}, LCM[x, y]}, {x, 4}, {y, 4}], 1]
Create an approximate interpolating function:
f = Interpolation[data]
Plot the interpolating function:
ContourPlot[f[x, y], {x, 1, 4}, {y, 1, 4}, ColorFunction -> "Rainbow"]



Now find minimum
 In[4]:= min = NMinimize[{f[x, y], 1 < x < 4 && 1 < y < 4}, {x, y}]
 Out[4]= {-0.609654, {x -> 1.49554, y -> 1.49554}}
 
 In[5]:= pt = {x, y, min[[1]]} /. min[[2]]
 Out[5]= {1.49554, 1.49554, -0.609654}
 
 Show[
 Plot3D[f[x, y], {x, 1, 4}, {y, 1, 4}, ColorFunction -> "Rainbow",
 PlotStyle -> Opacity[.8], MeshStyle -> Opacity[.5],
MeshFunctions -> {Sqrt[#1^2 + #2^2 + #3^2] &}, Mesh -> 15],
Graphics3D[{Red, PointSize[.08], Point[pt]}]
, PlotRange -> All]

POSTED BY: Vitaliy Kaurov
Why don't you just interpolate your data using Interpolation function if you have a regular grid? Say, you've read your data in pts variable. Then
Clear[x,y,z]
z[x_, y_] = Interpolation[pts][x, y]
Hopefully it'll help. Another way to find a minima is quite naive and approximate: to find a point with the smallest z:
First@SortBy[pts, Last]
Anyway, details depend on what data you have (regular or irregular grid, one or many minima, etc.)
POSTED BY: Gregory Fridman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract