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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:52:12+00:00 2026-05-22T15:52:12+00:00

I just tried to create my first function in octave, it looks as follows:

  • 0

I just tried to create my first function in octave, it looks as follows:

function hui(x)
if(0 <= x && x <2)
    retval = (1.5 * x + 2)
elseif(2<= x && x <4)
    retval = (-x + 5)
elseif(4<= x && x < 6)
    retval = (0.5 * x)
elseif(6<= x && x < 8)
    retval = (x - 3)
elseif(8<= x && x <=10)
    retval = (2 * x - 11)
endif
endfunction

but if I try to plot it using: x=0:0.1:10; plot(x, hui(x));

It shows a plot witch seems a little bit strange.
enter image description here

What did I wrong?

Thanks in advance
John

  • 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-05-22T15:52:13+00:00Added an answer on May 22, 2026 at 3:52 pm

    You’ll have to pardon my rustiness with the package, but you need to change the code around a bit. Notably, the notation 0<=x is incorrect, and must be x>=0. Since hui is operating on a vector, I believe you need to take that into account when constructing your return value.

    I’m sure there are more effective ways of vectorizing this, but basically, While stepping over the input vector, I added the latest value onto the return vector, and at the end lopping off the initial 0 that I had put in. I put in a sentinel value in case the input didn’t fulfill one of the criteria (it was always taking the “else” path in your code, so putting something there could have alerted you to something being wrong).

    function [retval] = hui(x)
    retval = 0
    for i=1:size(x,2)
        if(x(i)>=0 && x(i) <2)
             retval = [retval (1.5 * x(i) + 2)];
    
        elseif( x(i)>=2 && x(i) <4)
             retval = [retval (-1*x(i) + 5)];
    
        elseif(x(i)>=4 && x(i) < 6)
             retval = [retval (0.5 * x(i))];
    
        elseif(x(i)>=6 && x(i) < 8)
             retval = [retval (x(i) - 3)];
    
        elseif(x(i)>=8 && x(i) <=10)
             retval = [retval (2 * x(i) - 11)];
    
        else
             retval = -999;
    
        endif
    
    endfor 
        retval = retval(2:size(retval,2));
    endfunction
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just tried to create my first plpgsql function. When executing the script, I
I just started with the Poco library and tried to create an email program
I just tried my first steps with grid . I would like to set
I just tried the following code, but the result seems a little strange. It
I just tried the tooltip like this Easy image preview ... But i cant
i tried to understand the 2D & 3D plotting function in Matlab, regarding to
I just tried my first use of generics in Delphi 2009 and am perplexed
Just tried some small graphics application of mine on Windows 7, and I'm getting
I just tried to install git-flow, however, it does not seem to be integrated
I just tried to run my test app and I got this error: 2012-06-16

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.