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 9203863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:34:03+00:00 2026-06-17T23:34:03+00:00

I need to create a script in MATLAB that will look similar to this:

  • 0

I need to create a script in MATLAB that will look similar to this:

The shape I have to make

But the code I have is not working, and it is giving me the inverse, in that, the squares grow in size rather than shrink.

x = [0 0 2 2];
y = [0 2 2 0];
fill(x,y,'r');
hold on
for i = 1:10
x = [(x(3)) (x(3)) (x(3)/2) (x(3)/2)];
y = [(y(3)) (y(2)/2) (y(2)/2) (y(3))];
fill(x,y,'r');
end

Please provide an explanation along with the answer as I want to learn what I did wrong.

  • 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-17T23:34:05+00:00Added an answer on June 17, 2026 at 11:34 pm

    Have a look at the two values of x alone to simplify this investigation. After your first iteration, x will be 2 and 1, in the second iteration 1 and 0.5. This means you are approaching zero with squares that are getting smaller and smaller, the opposite of what you intended to do.

    How about you start big close to the origin and shrink as you go further away? You could initialize x = [0, 2]; and y = [0, 2];. We’re using only two elements here because for a square that’s aligned with your axes, that’s all we need. The first iteration may start with a shift by the edge length of the previous square as in x = x + x(2) - x(1);. The square will have to shrink though as well, so you could move the left corners by some small fraction of the edge length, e.g. x(1) = x(1) + (x(2) - x(1)) * 0.1;. To summarize, your loop would look like

    close all, clear all;
    x = [0, 2];
    y = [0, 2];
    hold on;
    for k = 0 : 9
        edge_len = x(2) - x(1);
        x = x + edge_len;  % shift
        x(1) = x(1) + 0.2 * edge_len;  % slightly shift right to shrink
        y = y + edge_len;
        y(1) = y(1) + 0.2 * edge_len;
        fill([x(1), x(1), x(2), x(2)], [y(1), y(2), y(2), y(1)], 'r');
    end
    

    Note that we replaced x(2) - x(1) by edge_len. Then we another problem of setting your color. You could use a color vector c = [1, k / 10, k / 10] to create a gradient from red to almost white. Then instead of fill(..., 'r'); you’d use fill(..., c);

    With this, there won’t be any fill outside the loop. That used to cover all your interesting graphing in the code block you show in the question.

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

Sidebar

Related Questions

I'm writing a script and I need to create a loop that will execute
I need to have script.sh , that would create files f1.txt and f2.txt with
I need to create a script that will log into an authenticated page and
We need to create a script (ajax or php or javascript) that will clean
I have complex code blocks, in a Matlab script, that act on large, non-sparse
I'm no javascript master, but I need to create a script that accomploshies the
I need to create a script that will be usable on 10 tables with
I need to create a script in jQuery and PHP that will show a
I need to create a script in Python which will look to a directory
I have written a code in matlab script. I need to finish it up

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.