help-octave
[Top][All Lists]

 From: Jaroslav Hajek Subject: Re: Integration with quad Date: Thu, 10 Dec 2009 07:22:12 +0100

```On Wed, Dec 9, 2009 at 8:56 PM,  <address@hidden> wrote:
> -----Original Message-----
>
> ons, 09 12 2009 kl. 20:07 +0100, skrev Thomas Göbel:
>>> i want to integrate the following function:
>>>
>>> function y = rect(t)
>>> if t<=0.5
>>>     rect=1
>>> else
>>>     rect=0
>>> end
>>> endfunction
>
>> Your return value is called 'y', but you never define this in your
>> function. Try something like
>>
>> function y = rect(t)
>>  if t<=0.5
>>    y=1
>>  else
>>    y=0
>>  endif
>> endfunction
>
> You can also write:
>
> function y = rect(t)
>  y = (t<=0.5);
> endfunction
>
> This function will work on a vector.

Beware of this, though. The above produces a *logical* result, not a
numeric one. In most contexts logicals are auto-converted to reals
(e.g. when used in arithmetics) but there may be some cases where this
might make a difference. It's bit safer is to ensure the conversion
yourself:
y = double (t <= 0.5);

> Are there methods to "vectorize" other functions with less tractable
> conditional statements?
>

Yes. Best shown on examples, I suppose.

hth

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

```