Assume given a continuous function $f$ with 2 variables, say $x$ and $y$. $x$ takes values on the interval $I$ and $y$ takes values on the interval $J$. I would like to know how to plot the function $$g(y):=\sup_{x\in I}f(x,y)$$ using Mathematica. For example, $f(x,y)=e^y\sin(x^2)$ with $x\in[0,1], y\in[0,1]$. Many thanks.
mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification
I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...
Comments
Post a Comment