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 have tried with the following, but it just says & was unexpected at
Just tried to execute a small Lua script, but unfortunately I'm doing something wrong.
I just tried (for the first time, I might add) a port upgrade installed
I just started programming in C++, and I've tried to create 2 classes where
I just don't get it. Tried on VC++ 2008 and G++ 4.3.2 #include <map>
I've tried msysGit and Git on Cygwin. Both work just fine in and of
I just got Java5 project that has this error, i tried using Java5 and
Just looking for the first step basic solution here that keeps the honest people
Originally, I just want to verify that session_start locks on session. So, I create

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.