help-octave
[Top][All Lists]

## Re: vectorize inline function

 From: James Sherman Jr. Subject: Re: vectorize inline function Date: Sat, 2 Aug 2014 15:00:50 -0400

On Sat, Aug 2, 2014 at 12:48 PM, isotrex wrote:
here's the error:

octave-3.6.4.exe:15> secant
Enter Function:x^2*cos(x)-1
error: isinf: not defined for inline function
error: evaluating argument list element number 1
error: evaluating argument list element number 1
error: called from:
error:
D:\Software\Octave-3.6.4\share\octave\3.6.4\m\plot\private\__plt__.m at
line 219, column 3
error:
D:\Software\Octave-3.6.4\share\octave\3.6.4\m\plot\private\__plt__.m at
line 99, column 17
error:   D:\Software\Octave-3.6.4\share\octave\3.6.4\m\plot\plot.m at line
196,
column 9
error:   D:\Software\Octave-3.6.4\numericals\secant.m at line 11, column 2
octave-3.6.4.exe:15>

here's my code:

function ret = secant()
a = input('Enter Function:','s');
f = inline(a);

t=-5:0.0001:3;
#x = t.^2.*cos(t)-1; #<--- this works. just commented it out to try between
the two.
x = vectorize(f);       #<---- this code does not
plot(t,x);
axis([-6 4 -15 15])
title('Plot of f(x)=x^2*cos(x)-1')
xlabel('time(t)')
ylabel('f(x)')
grid on
endfunction

I maybe missing something here. I don't know what isinf error either and
according to the manual...
isinf (x) [Mapping Function]
Return a logical array which is true where the elements of x are are
infinite and false
where they are not. For example:
isinf ([13, Inf, NA, NaN])

Also, is there a comment block for octave. It seems %{ %} or #{ #} doesn't
work. I'm using Octave 3.6.4 by the way. Thanks for taking your time in
helping me.

--
View this message in context: http://octave.1599824.n4.nabble.com/vectorize-inline-function-tp4665884p4665891.html
Sent from the Octave - General mailing list archive at Nabble.com.

_______________________________________________
Help-octave mailing list
https://lists.gnu.org/mailman/listinfo/help-octave

The problem is that there is a difference between:
x = t.^2.*cos(t)-1;
and
x = vectorize(f);

What happens with the first option (that you have commented out in the code), is that x is a vector calculated from the values in t.  In the second option, you have that x is a function not a vector.  The fact that you used "t" as your variable in the string "a" has no relation to the t vector that you declare later on.    Note that in the example from the previous email, to evaluate the inline function at particular values, you need to call it like a function, x([0 .5]). So, all you need to do fix your code is to replace:
plot(t,x)
with
plot(t,x(t))

The error you get comes from inside the plot function and has to do with passing a vector and a function into plot and not two vectors (which is what its expecting).

As far as I know, there is no symbol in Octave to comment out a block of code.

Hope this helps.

James Sherman