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

  • SEARCH
  • Home
  • 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 8703333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:50:58+00:00 2026-06-13T02:50:58+00:00

I am trying to plot the function f(x, y) = (x – 3).^2 –

  • 0

I am trying to plot the function

f(x, y) = (x – 3).^2 – (y – 2).^2.

x is a vector from 2 to 4, and y is a vector from 1 to 3, both with increments of 0.2. However, I am getting the error:

“Subscript indices must either be real positive integers or logicals”.

What do I do to fix this error?

  • 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-13T02:50:58+00:00Added an answer on June 13, 2026 at 2:50 am

    I (think) I see what you are trying to achieve. You are writing your syntax like a mathematical function definition. Matlab is interpreting f as a 2-dimensional data type and trying to assign the value of the expression to data indexed at x,y. The values of x and y are not integers, so Matlab complains.

    If you want to plot the output of the function (we’ll call it z) as a function of x and y, you need to define the function quite differently . . .

    f = @(x,y)(x-3).^2 - (y-2).^2;
    x=2:.2:4;
    y=1:.2:3; 
    z = f(  repmat(x(:)',numel(y),1)  , repmat(y(:),1,numel(x) ) );
    
    surf(x,y,z); 
    xlabel('X'); ylabel('Y'); zlabel('Z');
    

    This will give you an output like this . . .
    enter image description here

    The f = @(x,y) part of the first line states you want to define a function called f taking variables x and y. The rest of the line is the definition of that function.

    If you want to plot z as a function of both x and y, then you need to supply all possible combinations in your range. This is what the line containing the repmat commands is for.

    EDIT

    There is a neat Matlab function meshgrid that can replace the repmat version of the script as suggested by @bas (welcome bas, please scroll to bas’ answer and +1 it!) …

    f = @(x,y)(x-3).^2 - (y-2).^2;
    x=2:.2:4;
    y=1:.2:3;
    [X,Y] = meshgrid(x,y);
    surf(x,y,f(X,Y)); 
    xlabel('x'); ylabel('y'); zlabel('z');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to plot this using matplotlib's step function from matplotlib.pyplot import
I am trying to plot Banknote data frame from alr3 package.My pairs function throws
I'm trying to plot a logarithmic scale, but I keep on getting this error:
I'm trying to plot values as a function of the date (only hh:mm:ss, without
I'm trying to plot the Dirac delta function in Matlab using plot , but
I am trying to plot some data using pylab scatter function. I'm using pylab.scatter(X,
I'm trying to plot a function that contains a definite integral. My code uses
Trying to plot a signal function of 0 and 1, much like the solution
I am trying to plot roots of a function that is composed of multiple
I'm trying to plot some cities on Google's Geochart. This function works perfectly fine

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.