Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:45:01+00:00 2026-06-11T05:45:01+00:00

I am trying to find the minimum value of a function of two variables,

  • 0

I am trying to find the minimum value of a function of two variables, and then find the value of the variables.

My method is to iterate the function through several values of the variables and then use the min function to find the lowest value.

minval = -10;
maxval = 10;
n = 1;

for x = minval:maxval
    for y = minval:maxval
        f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
        n=n+1;
    end
end
f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
fmin = min(f)

The problem is with the last line:

fmin = min(f)

I am getting the error

??? Index exceeds matrix dimensions.

Error in ==> Lab2 at 65
fmin = min(f)

Why is this? Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:45:03+00:00Added an answer on June 11, 2026 at 5:45 am

    It does indeed look like you have declared a variable called min and so Matlab now treats it like a variable and not a function so it thinks you are trying to index the variable min with the vector f.

    But just a comment on your code, leaving off whatever f(442) is you could achieve the same thing in a much more matlabesque fashion without loops like this:

    minval = -10;
    maxval = 10;
    
    X = minval:maxval;
    Y = X;
    
    [xx, yy] = meshgrid(X, Y);
    F = abs(xx-1) + abs(yy-1) + abs(xx-3) + abs(yy-5) +abs(xx-8) + abs(yy-3);
    

    Your f is now equivalent to F(:)' (without the final value…), prove it to yourself like this: sum(f(1:end-1) == F(:)')

    F as a matrix probably makes more sense than f as a flat vector anyway and you can find the min of F like this: min(F(:))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Oracle version : 10g I'm trying to use the MIN function to find the
I'm trying to find a 2d array that represents the minimum values of the
I am trying to find the minimum value of a list (as a learning
I'm trying to extend a function I wrote to find the first 3 values
I'm trying to find the min of a function in N parameters using gradient
I am trying to find the minimum and maximum values for the continuous while
I am trying to use MPI_REDUCE in C (or c++) to find the minimum
I'm trying to write a program that would find the minimum spanning tree. But
Trying to find out if it's possible to use SQLAlchemy on Heroku. Thanks.
Trying to find a good name for a method swapping each coordinate X and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.