I'm learning Mathematica and meet a problem. Here is a simple example.

x = {0, 0.5, 1}
y = {a-3,b,10}
yl= {b,a,5}

I want to find the value of a and b when it minimum of (y-x)^2+(yl-x)^2

Nminimize[(y - x)^2 + (yl - x)^2, {a,b}]

But it returns

Minimize[{(-3. + a)^2 + (0. + b)^2, (-0.5 + a)^2 + (-0.5 + b)^2, 97.}, {a, b}]

What's the right way to solve this problem? Thanks

  • 1
    $\begingroup$ I don't think I understand this at all. When a is 1, y1 - y is zero right? $\endgroup$ – Abhay Hegde Apr 9 at 18:11
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, [by clicking the checkmark sign](tinyurl.com/4srwe26 $\endgroup$ – Dunlop Apr 9 at 18:48
  • $\begingroup$ Can you give a minimal example of what you are trying to do? This is not clear enough for people to help you. $\endgroup$ – Dunlop Apr 9 at 18:49
  • $\begingroup$ Also note that NMinimize is case sensitive and you are showing Nminimize. $\endgroup$ – Tim Laska Apr 9 at 18:53
  • $\begingroup$ I rewrite the example, I think it is clear now, thank you guys $\endgroup$ – YzWu Apr 9 at 18:59

You have a typo in NMinimize and it will require the objective function to be a scalar and not a vector. You could minimize the Norm of the vector like so.

x = {0, 0.5, 1};
y = {a - 3, b, 10};
yl = {b, a, 5};
NMinimize[(y - x)^2 + (yl - x)^2 // Norm, {a, b}]
(* {97.0272, {a -> 1.75, b -> 0.25}} *)
| improve this answer | |
  • $\begingroup$ Got it. Thank you! $\endgroup$ – YzWu Apr 9 at 19:34

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.