Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
writing functions

Subject: writing functions

From: James

Date: 3 Feb, 2012 05:44:12

Message: 1 of 2

i have to creat a function of

f(x) ={ 3-|2x| if|x|<=1.5
           0 if x<-1
         2x-2 if x >1

this is what i did but not why it is not working

function out = myfun1(in)
if in<= abs(1.5)
    out =3-abs(2x);
else if in <-1

Subject: writing functions

From: Roger Stafford

Date: 3 Feb, 2012 07:25:10

Message: 2 of 2

"James " <> wrote in message <jgfs7c$28h$>...
> f(x) ={ 3-|2x| if|x|<=1.5
> 0 if x<-1
> 2x-2 if x >1
- - - - - - - - -
  Your trouble is principally caused by the poor function definition. If for example x is equal to 1.25 it satisfies both the requirement that abs(x)<1.5 and that x>1, or if x = -1.25 it satisfies both abs(x)<1.5 and x<-1. What is 'out' supposed to be in these cases? You had better straighten that out before you attempt to write code for it.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us